Skip to content

Commit

Permalink
Use iOS localization handling for strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave committed Apr 14, 2023
1 parent 3d0d883 commit e02d356
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 125 deletions.
2 changes: 0 additions & 2 deletions ElementX/Sources/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ class AppCoordinator: AppCoordinatorProtocol {
ServiceLocator.shared.settings.lastVersionLaunched = currentVersion.description

setupStateMachine()

Bundle.elementFallbackLanguage = "en"

observeApplicationState()
observeNetworkState()
Expand Down
7 changes: 1 addition & 6 deletions ElementX/Sources/Generated/Strings+Untranslated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,10 @@ public enum UntranslatedL10n {
extension UntranslatedL10n {
static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
// No need to check languages, we always default to en for untranslated strings
guard let bundle = Bundle(for: BundleToken.self).lprojBundle(for: "en") else {
// no translations for the desired language
return key
}
guard let bundle = Bundle.lprojBundle(for: "en") else { return key }
let format = NSLocalizedString(key, tableName: table, bundle: bundle, comment: "")
return String(format: format, locale: Locale(identifier: "en"), arguments: args)
}
}

private final class BundleToken {}

// swiftlint:enable all
23 changes: 8 additions & 15 deletions ElementX/Sources/Generated/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -800,31 +800,24 @@ public enum L10n {

extension L10n {
static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
let languages = Bundle.preferredLanguages
// Use preferredLocalizations to get a language that is in the bundle and the user's preferred list of languages.
let languages = Bundle.overrideLocalizations ?? Bundle.app.preferredLocalizations

for language in languages {
if let translation = trIn(language, table, key, args) {
return translation
// If we can't find a translation for this language
// we check if we can find one by stripping the region
} else if let langCode = Locale(identifier: language).language.languageCode?.identifier,
let translation = trIn(langCode, table, key, args) {
return translation
}
}
return key
}
return Bundle.app.developmentLocalization.flatMap { trIn($0, table, key, args) } ?? key
}

private static func trIn(_ language: String, _ table: String, _ key: String, _ args: CVarArg...) -> String? {
guard let bundle = Bundle(for: BundleToken.self).lprojBundle(for: language) else {
// no translations for the desired language
return nil
}
guard let bundle = Bundle.lprojBundle(for: language) else { return nil }
let format = NSLocalizedString(key, tableName: table, bundle: bundle, comment: "")
return String(format: format, locale: Locale(identifier: language), arguments: args)
let translation = String(format: format, locale: Locale(identifier: language), arguments: args)
guard translation != key else { return nil }
return translation
}
}

private final class BundleToken {}

// swiftlint:enable all
63 changes: 20 additions & 43 deletions ElementX/Sources/Other/Extensions/Bundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,28 @@
import Foundation

public extension Bundle {
/// The top-level bundle that contains the entire app.
static var app: Bundle {
var bundle = Bundle.main
if bundle.bundleURL.pathExtension == "appex" {
// Peel off two directory levels - MY_APP.app/PlugIns/MY_APP_EXTENSION.appex
let url = bundle.bundleURL.deletingLastPathComponent().deletingLastPathComponent()
if let otherBundle = Bundle(url: url) {
bundle = otherBundle
}
}
return bundle
}

// MARK: - Localisation

private static var cachedLocalizationBundles = [String: Bundle]()

/// Get an lproj language bundle from the receiver bundle.
/// - Parameter language: The language to try to load.
/// - Returns: The lproj bundle if found otherwise nil.
func lprojBundle(for language: String) -> Bundle? {
if let bundle = Self.cachedLocalizationBundles[language] {
static func lprojBundle(for language: String) -> Bundle? {
if let bundle = cachedLocalizationBundles[language] {
return bundle
}

Expand All @@ -32,49 +47,11 @@ public extension Bundle {
}

let bundle = Bundle(url: lprojURL)
Self.cachedLocalizationBundles[language] = bundle
cachedLocalizationBundles[language] = bundle

return bundle
}

/// Preferred app language for translations. Takes the highest priority in translations. The priority list for translations:
/// - `Bundle.elementLanguage`
/// - `Locale.preferredLanguages`
/// - `Bundle.elementFallbackLanguage`
static var elementLanguage: String? {
didSet {
preferredLanguages = calculatePreferredLanguages()
}
}

/// Preferred fallback language for translations. Only used for strings not translated neither to `elementLanguage` nor to one of the user's preferred languages.
static var elementFallbackLanguage: String? {
didSet {
preferredLanguages = calculatePreferredLanguages()
}
}

static var app: Bundle {
var bundle = Bundle.main
if bundle.bundleURL.pathExtension == "appex" {
// Peel off two directory levels - MY_APP.app/PlugIns/MY_APP_EXTENSION.appex
let url = bundle.bundleURL.deletingLastPathComponent().deletingLastPathComponent()
if let otherBundle = Bundle(url: url) {
bundle = otherBundle
}
}
return bundle
}

/// Preferred languages in the priority order.
private(set) static var preferredLanguages: [String] = calculatePreferredLanguages()

private static func calculatePreferredLanguages() -> [String] {
var set = Set<String>()
return ([Bundle.elementLanguage] +
Locale.preferredLanguages +
[Bundle.elementFallbackLanguage])
.compactMap { $0 }
.filter { set.insert($0).inserted }
}
/// Overrides `Bundle.app.preferredLocalizations` for testing translations.
static var overrideLocalizations: [String]?
}
6 changes: 3 additions & 3 deletions ElementX/Sources/Services/BugReport/BugReportService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ class BugReportService: NSObject, BugReportServiceProtocol {
MultipartFormData(key: "version", type: .text(value: InfoPlistReader.main.bundleShortVersionString)),
MultipartFormData(key: "build", type: .text(value: InfoPlistReader.main.bundleVersion)),
MultipartFormData(key: "os", type: .text(value: os)),
MultipartFormData(key: "resolved_language", type: .text(value: Bundle.preferredLanguages[0])),
MultipartFormData(key: "user_language", type: .text(value: Bundle.elementLanguage ?? "null")),
MultipartFormData(key: "fallback_language", type: .text(value: Bundle.elementFallbackLanguage ?? "null")),
MultipartFormData(key: "resolved_language", type: .text(value: Bundle.app.preferredLocalizations[0])),
MultipartFormData(key: "user_language", type: .text(value: Locale.preferredLanguages.first ?? "null")),
MultipartFormData(key: "fallback_language", type: .text(value: Bundle.app.developmentLocalization ?? "null")),
MultipartFormData(key: "local_time", type: .text(value: localTime)),
MultipartFormData(key: "utc_time", type: .text(value: utcTime)),
MultipartFormData(key: "base_bundle_identifier", type: .text(value: InfoPlistReader.main.baseBundleIdentifier))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
appDisplayName: "\(InfoPlistReader.main.bundleDisplayName) (iOS)",
deviceDisplayName: UIDevice.current.name,
profileTag: pusherProfileTag(),
lang: Bundle.preferredLanguages.first ?? "en")
lang: Bundle.app.preferredLocalizations.first ?? "en")
try await clientProxy.setPusher(with: configuration)
MXLog.info("[NotificationManager] set pusher succeeded")
return true
Expand Down
2 changes: 0 additions & 2 deletions ElementX/Sources/UITests/UITestsAppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class UITestsAppCoordinator: AppCoordinatorProtocol {
}

func start() {
Bundle.elementFallbackLanguage = "en"

guard let screenID = Tests.screenID else { fatalError("Unable to launch with unknown screen.") }

let mockScreen = MockScreen(id: screenID)
Expand Down
7 changes: 0 additions & 7 deletions NSE/Sources/NotificationServiceExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
var handler: ((UNNotificationContent) -> Void)?
var modifiedContent: UNMutableNotificationContent?

override init() {
// Use `en` as fallback language
Bundle.elementFallbackLanguage = "en"

super.init()
}

override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
guard !DataProtectionManager.isDeviceLockedAfterReboot(containerURL: URL.appGroupContainerDirectory),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,12 @@ import Foundation
extension {{enumName}} {
static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
// No need to check languages, we always default to en for untranslated strings
guard let bundle = Bundle(for: BundleToken.self).lprojBundle(for: "en") else {
// no translations for the desired language
return key
}
guard let bundle = Bundle.lprojBundle(for: "en") else { return key }
let format = NSLocalizedString(key, tableName: table, bundle: bundle, comment: "")
return String(format: format, locale: Locale(identifier: "en"), arguments: args)
}
}

private final class BundleToken {}

{% else %}
// No string found
{% endif %}
Expand Down
23 changes: 8 additions & 15 deletions Tools/SwiftGen/Templates/Strings/structured-swift5-element.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,26 @@ import Foundation

extension {{enumName}} {
static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
let languages = Bundle.preferredLanguages
// Use preferredLocalizations to get a language that is in the bundle and the user's preferred list of languages.
let languages = Bundle.overrideLocalizations ?? Bundle.app.preferredLocalizations

for language in languages {
if let translation = trIn(language, table, key, args) {
return translation
// If we can't find a translation for this language
// we check if we can find one by stripping the region
} else if let langCode = Locale(identifier: language).language.languageCode?.identifier,
let translation = trIn(langCode, table, key, args) {
return translation
}
}
return key
}
return Bundle.app.developmentLocalization.flatMap { trIn($0, table, key, args) } ?? key
}

private static func trIn(_ language: String, _ table: String, _ key: String, _ args: CVarArg...) -> String? {
guard let bundle = Bundle(for: BundleToken.self).lprojBundle(for: language) else {
// no translations for the desired language
return nil
}
guard let bundle = Bundle.lprojBundle(for: language) else { return nil }
let format = NSLocalizedString(key, tableName: table, bundle: bundle, comment: "")
return String(format: format, locale: Locale(identifier: language), arguments: args)
let translation = String(format: format, locale: Locale(identifier: language), arguments: args)
guard translation != key else { return nil }
return translation
}
}

private final class BundleToken {}

{% else %}
// No string found
{% endif %}
Expand Down
3 changes: 0 additions & 3 deletions UITests/Sources/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ struct Application {
"UI_TESTS_SCREEN": identifier.rawValue
]

// Use the same fallback language as the real app so translation comparison works
Bundle.elementFallbackLanguage = "en"

app.launch()
return app
}
Expand Down
31 changes: 10 additions & 21 deletions UnitTests/Sources/LocalizationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,29 @@ import XCTest
class LocalizationTests: XCTestCase {
/// Test ElementL10n considers app language changes
func testAppLanguage() {
// set app language to English
Bundle.elementLanguage = "en"
// set app language to English
Bundle.overrideLocalizations = ["en"]

XCTAssertEqual(L10n.testLanguageIdentifier, "en")

// set app language to Italian
Bundle.elementLanguage = "it"

XCTAssertEqual(L10n.testLanguageIdentifier, "it")
}

/// Test fallback language for a language not supported at all
func testStripRegionIfRegionalTranslationIsNotAvailable() {
// set app language to something that includes also a region (it-IT)
Bundle.elementLanguage = "it-IT"
// set app language to Italian
Bundle.overrideLocalizations = ["it"]

XCTAssertEqual(L10n.testLanguageIdentifier, "it")
}

/// Test fallback language for a language not supported at all
func testFallbackOnNotSupportedLanguage() {
// set app language to something Element don't support at all (chose non existing identifier)
Bundle.elementLanguage = "xx"
Bundle.elementFallbackLanguage = "en"
Bundle.overrideLocalizations = ["xx"]

XCTAssertEqual(L10n.testLanguageIdentifier, "en")
}

/// Test fallback language for a language supported but poorly translated
func testFallbackOnNotTranslatedKey() {
// set app language to something Element supports but use a key that is not translated (we have a key that should never be translated)
Bundle.elementLanguage = "it"
Bundle.elementFallbackLanguage = "en"
Bundle.overrideLocalizations = ["it"]

XCTAssertEqual(L10n.testLanguageIdentifier, "it")
XCTAssertEqual(L10n.testUntranslatedDefaultLanguageIdentifier, "en")
Expand All @@ -61,19 +51,19 @@ class LocalizationTests: XCTestCase {
/// Test plurals that ElementL10n considers app language changes
func testPlurals() {
// set app language to English
Bundle.elementLanguage = "en"
Bundle.overrideLocalizations = ["en"]

XCTAssertEqual(L10n.commonMemberCount(1), "1 member")
XCTAssertEqual(L10n.commonMemberCount(2), "2 members")

// set app language to Italian
Bundle.elementLanguage = "it"
Bundle.overrideLocalizations = ["it"]

XCTAssertEqual(L10n.commonMemberCount(1), "1 membro")
XCTAssertEqual(L10n.commonMemberCount(2), "2 membri")

// // set app language to Polish
// Bundle.elementLanguage = "pl"
// Bundle.overrideLocalizations = ["pl"]
//
// XCTAssertEqual(L10n.commonMemberCount(1), "1 sekunda") // one
// XCTAssertEqual(L10n.commonMemberCount(2), "2 sekundy") // few
Expand All @@ -83,8 +73,7 @@ class LocalizationTests: XCTestCase {
/// Test plurals fallback language for a language not supported at all
func testPluralsFallbackOnNotSupportedLanguage() {
// set app language to something Element don't support at all ("invalid identifier")
Bundle.elementLanguage = "xx"
Bundle.elementFallbackLanguage = "en"
Bundle.overrideLocalizations = ["xx"]

XCTAssertEqual(L10n.commonMemberCount(1), "1 member")
XCTAssertEqual(L10n.commonMemberCount(2), "2 members")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class NotificationManagerTests: XCTestCase {
XCTAssertEqual(clientProxy.setPusherArgument?.appDisplayName, "\(InfoPlistReader.main.bundleDisplayName) (iOS)")
XCTAssertEqual(clientProxy.setPusherArgument?.deviceDisplayName, UIDevice.current.name)
XCTAssertNotNil(clientProxy.setPusherArgument?.profileTag)
XCTAssertEqual(clientProxy.setPusherArgument?.lang, Bundle.preferredLanguages.first)
XCTAssertEqual(clientProxy.setPusherArgument?.lang, Bundle.app.preferredLocalizations.first)
guard case let .http(data) = clientProxy.setPusherArgument?.kind else {
XCTFail("Http kind expected")
return
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-803.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use iOS localization handling for strings.

0 comments on commit e02d356

Please sign in to comment.