Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions Sources/HTMLKitVapor/Extensions/Vapor+HTMLKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,8 @@ extension Application {

extension Request {

/// The accept language header of the request
private var acceptLanguage: String? {

if let languageHeader = headers.first(name: .acceptLanguage) {
return languageHeader.components(separatedBy: ",").first
}

return nil
}

/// Access to the view renderer
public var htmlkit: ViewRenderer {

if let acceptLanguage = acceptLanguage {
application.htmlkit.environment.upsert(HTMLKit.Locale(tag: acceptLanguage), for: \HTMLKit.EnvironmentKeys.locale)
}

return .init(eventLoop: eventLoop, configuration: application.htmlkit.configuration, logger: logger)
}
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/HTMLKitVaporTests/Localization/de-DE/web.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* A string key with a namespace pattern */
"hello.world" = "Hallo Welt";
86 changes: 81 additions & 5 deletions Tests/HTMLKitVaporTests/ProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ final class ProviderTests: XCTestCase {
try await app.asyncShutdown()
}

/// Tests the localization behavior based on the accept language of the client
/// Tests the localization behavior based on the accept languages of the client.
///
/// The environment locale is expected to be changed according to the language given by the provider.
/// The renderer is expected to localize correctly the content based on the updated environment locale.
func testLocalizationByAcceptingHeaders() async throws {
/// The environment locale is expected to be changed according to the language. The renderer
/// is expected to localize correctly the view based on the updated environment locale.
func testHeaderBasedLocalization() async throws {

guard let source = Bundle.module.url(forResource: "Localization", withExtension: nil) else {
return
Expand All @@ -236,10 +236,86 @@ final class ProviderTests: XCTestCase {
app.htmlkit.localization.set(locale: "en-GB")

app.get("test") { request async throws -> Vapor.View in

if let languages = request.headers.first(name: .acceptLanguage) {

if let language = languages.components(separatedBy: ",").first {
app.htmlkit.environment.upsert(HTMLKit.Locale(tag: language), for: \EnvironmentKeys.locale)
}
}

return try await request.htmlkit.render(TestPage.ChildView())
}

let languages = ["fr": "Bonjour le monde", "en-GB": "Hello World", "de-DE": "Hallo Welt"]

for language in languages {

try await app.test(.GET, "test", headers: ["accept-language": language.key]) { response async in
XCTAssertEqual(response.status, .ok)
XCTAssertEqual(response.body.string,
"""
<!DOCTYPE html>\
<html>\
<head>\
<title>TestPage</title>\
</head>\
<body>\
<p>\(language.value)</p>\
</body>\
</html>
"""
)
}
}

try await app.asyncShutdown()
}

/// Tests the localization behavior based on the routing.
func testRoutingBasedLocalization() async throws {

guard let source = Bundle.module.url(forResource: "Localization", withExtension: nil) else {
return
}

let app = try await Application.make(.testing)

app.htmlkit.localization.set(source: source)
app.htmlkit.localization.set(locale: "en-GB")

app.get("test", "de") { request async throws -> Vapor.View in

request.application.htmlkit.environment.upsert(HTMLKit.Locale(tag: "de-DE"), for: \EnvironmentKeys.locale)

return try await request.htmlkit.render(TestPage.ChildView())
}

app.get("test", "fr") { request async throws -> Vapor.View in

request.application.htmlkit.environment.upsert(HTMLKit.Locale(tag: "fr"), for: \EnvironmentKeys.locale)

return try await request.htmlkit.render(TestPage.ChildView())
}

try await app.test(.GET, "test", headers: ["accept-language": "fr"]) { response async in
try await app.test(.GET, "test/de", headers: ["accept-language": "en-US"]) { response async in
XCTAssertEqual(response.status, .ok)
XCTAssertEqual(response.body.string,
"""
<!DOCTYPE html>\
<html>\
<head>\
<title>TestPage</title>\
</head>\
<body>\
<p>Hallo Welt</p>\
</body>\
</html>
"""
)
}

try await app.test(.GET, "test/fr", headers: ["accept-language": "en-US"]) { response async in
XCTAssertEqual(response.status, .ok)
XCTAssertEqual(response.body.string,
"""
Expand Down
Loading