This PR contains the work done to introduce server-side localization support to the **Website** service so the landing and error pages are served in the visitor's negotiated language, backed by a new reusable Localization package. To provide further details about the work: * Created the _Localization_ package — a bundle-bound `Localize` method and a `LanguageList` type. * Language negotiation — `NegotiateLanguage` method picks the best supported language from the request's _Accept-Language_ header (falling back to the default); the `LocalizationMiddleware` middleware resolves it per request and stores it on a new `LocalizedRequestContext` / `WebsiteRequestContext` context. * Localized responses — `LocalizedHTMLCollectionResponse` pre-renders each page once per language and caches the bytes (with `Content-Language` + `Vary: Accept-Language`), reused by the `RootController` controller and `NotFoundMiddleware` middleware. * The `CachedHTMLResponse` response gained custom-header support. * Localized pages — the `IndexPage` and `ErrorPage` pages now resolve their strings via `Localize` method; * Added `Localizable.xcstrings` catalogs. * Wired the `LocalizationMiddleware` middlewaer into the router. Reviewed-on: rock-n-code/loud-amsterdam#10 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
80 lines
2.1 KiB
Swift
80 lines
2.1 KiB
Swift
import Hummingbird
|
|
|
|
/// Serves the website's root routes.
|
|
///
|
|
/// The controller exposes its routes as a `RouteCollection` so they can be added to a router
|
|
/// (or a sub-group) by the application that composes it:
|
|
///
|
|
/// ```swift
|
|
/// router.addRoutes(RootController<AppRequestContext>().routes)
|
|
/// ```
|
|
///
|
|
/// - Note: `Context` is the request context the routes are resolved against, and must match the
|
|
/// context of the router the routes are added to.
|
|
public struct RootController<Context: LocalizedRequestContext>: Sendable {
|
|
|
|
// MARK: Properties
|
|
|
|
/// The landing page, rendered once per supported language and reused for every request.
|
|
private let responses: LocalizedHTMLCollectionResponse
|
|
|
|
// MARK: Initializers
|
|
|
|
/// Creates a root controller.
|
|
public init() {
|
|
self.responses = .init { IndexPage(locale: $0) }
|
|
}
|
|
|
|
// MARK: Computed
|
|
|
|
/// The routes served by the controller.
|
|
///
|
|
/// Serves a `GET` request for the root path (`/`) by rendering the ``IndexPage`` in the
|
|
/// language negotiated for the request.
|
|
public var routes: RouteCollection<Context> {
|
|
let routes = RouteCollection(context: Context.self)
|
|
|
|
routes.get(
|
|
.Root.index,
|
|
use: index
|
|
)
|
|
|
|
return routes
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private extension RootController {
|
|
|
|
// MARK: Methods
|
|
|
|
/// Handles a request for the landing page.
|
|
///
|
|
/// Renders the ``IndexPage`` in the language stored on the context by ``LocalizationMiddleware``,
|
|
/// falling back to the default language.
|
|
/// - Parameters:
|
|
/// - request: the incoming request.
|
|
/// - context: the context the request is resolved against.
|
|
/// - Returns: the cached ``IndexPage`` response for the context's language.
|
|
@Sendable
|
|
func index(
|
|
request: Request,
|
|
context: Context
|
|
) -> Response {
|
|
responses.response(for: context.language)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Constants
|
|
|
|
private extension RouterPath {
|
|
/// A namespace for the ``RootController`` route paths.
|
|
enum Root {
|
|
/// The path of the landing page.
|
|
static let index: RouterPath = "/"
|
|
}
|
|
}
|