This PR contains the work done to refactor the _Localization_ package and tidy the project a little bit. To provider further details about the work: * The `Negotiate` function was moved to the _Localization_ package. * The `WebsiteRequestContext` context no longer resolves a default language at creation; it starts empty and relies on the `LocalizationMiddleware` middleware to fill it in. * Fixed the local dependency path to Packages/Localization for the **Website** package as it only resolved inside the Xcode workspace before, breaking swift build, the Makefile, and the Docker build). * Updated the `README` file to document language negotiation, the GET /health route, and the full middleware chain; doc comments across the moved/renamed types were brought back in sync with the code. Reviewed-on: rock-n-code/loud-amsterdam#11 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
50 lines
1.4 KiB
Swift
50 lines
1.4 KiB
Swift
import Hummingbird
|
|
|
|
/// A request context that carries the language negotiated for the request.
|
|
///
|
|
/// ``LocalizationMiddleware`` resolves the visitor's preferred language from the `Accept-Language`
|
|
/// header and stores it here, so downstream controllers and middleware can serve the matching
|
|
/// localization without re-reading the header.
|
|
public protocol LocalizedRequestContext: RequestContext {
|
|
|
|
// MARK: Properties
|
|
|
|
/// The language identifier negotiated for the request.
|
|
var language: String { get set }
|
|
|
|
}
|
|
|
|
// MARK: - Context
|
|
|
|
/// The website's request context.
|
|
///
|
|
/// Extends the core request storage with the negotiated language, defaulting to the default
|
|
/// supported language until ``LocalizationMiddleware`` resolves it from the request.
|
|
public struct WebsiteRequestContext: LocalizedRequestContext {
|
|
|
|
// MARK: Properties
|
|
|
|
/// The core request context storage Hummingbird requires.
|
|
public var coreContext: CoreRequestContextStorage
|
|
/// The language identifier negotiated for the request.
|
|
public var language: String
|
|
|
|
// MARK: Initializers
|
|
|
|
/// Creates a request context for the given source.
|
|
/// - Parameter source: the source the context is initialized from.
|
|
public init(
|
|
source: Source,
|
|
) {
|
|
self.coreContext = .init(source: source)
|
|
self.language = .empty
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Constants
|
|
|
|
private extension String {
|
|
static let empty = ""
|
|
}
|