mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Use iOS localization handling for strings. (#803)
This commit is contained in:
@@ -86,8 +86,6 @@ class AppCoordinator: AppCoordinatorProtocol {
|
||||
ServiceLocator.shared.settings.lastVersionLaunched = currentVersion.description
|
||||
|
||||
setupStateMachine()
|
||||
|
||||
Bundle.elementFallbackLanguage = "en"
|
||||
|
||||
observeApplicationState()
|
||||
observeNetworkState()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -802,31 +802,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
|
||||
|
||||
@@ -17,43 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
public extension Bundle {
|
||||
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] {
|
||||
return bundle
|
||||
}
|
||||
|
||||
guard let lprojURL = Bundle.app.url(forResource: language, withExtension: "lproj") else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let bundle = Bundle(url: lprojURL)
|
||||
Self.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()
|
||||
}
|
||||
}
|
||||
|
||||
/// The top-level bundle that contains the entire app.
|
||||
static var app: Bundle {
|
||||
var bundle = Bundle.main
|
||||
if bundle.bundleURL.pathExtension == "appex" {
|
||||
@@ -65,16 +29,29 @@ public extension Bundle {
|
||||
}
|
||||
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 }
|
||||
|
||||
// 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.
|
||||
static func lprojBundle(for language: String) -> Bundle? {
|
||||
if let bundle = cachedLocalizationBundles[language] {
|
||||
return bundle
|
||||
}
|
||||
|
||||
guard let lprojURL = Bundle.app.url(forResource: language, withExtension: "lproj") else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let bundle = Bundle(url: lprojURL)
|
||||
cachedLocalizationBundles[language] = bundle
|
||||
|
||||
return bundle
|
||||
}
|
||||
|
||||
/// Overrides `Bundle.app.preferredLocalizations` for testing translations.
|
||||
static var overrideLocalizations: [String]?
|
||||
}
|
||||
|
||||
@@ -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_languages", type: .text(value: Bundle.app.preferredLocalizations.joined(separator: ", "))),
|
||||
MultipartFormData(key: "user_languages", type: .text(value: Locale.preferredLanguages.joined(separator: ", "))),
|
||||
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))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user