-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to customize localization (#6096)
Add the ability to customize localization
- Loading branch information
Showing
22 changed files
with
198 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,7 @@ let package = Package( | |
], | ||
resources: [ | ||
.copy("Fixtures"), | ||
.process("Resources"), | ||
] | ||
), | ||
.target( | ||
|
48 changes: 48 additions & 0 deletions
48
Sources/MapboxNavigationCore/Localization/LocalizationManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import _MapboxNavigationHelpers | ||
import Foundation | ||
|
||
// swiftformat:disable enumNamespaces | ||
/// This class handles the localization of the string inside of the SDK. | ||
public struct LocalizationManager { | ||
/// Set this bundle if you want to provide a custom localization for some string in the SDK. If the provided bundle | ||
/// does not contain the localized version, the string from the default bundle inside the SDK will be used. | ||
public static var customLocalizationBundle: Bundle? { | ||
get { _customLocalizationBundle.read() } | ||
set { _customLocalizationBundle.update(newValue) } | ||
} | ||
|
||
private static let _customLocalizationBundle: NSLocked<Bundle?> = .init(nil) | ||
private static let nonExistentKeyValue = "_nonexistent_key_value_" | ||
|
||
/// Retrieves the localized string for a given key. | ||
/// - Parameters: | ||
/// - key: The key for the string to localize. | ||
/// - tableName: The name of the table containing the localized string identified by `key`. | ||
/// - defaultBundle: The default bundle containing the table's strings file. | ||
/// - value: The value to use if the key is not found (optional). | ||
/// - comment: A note to the translator describing the context where the localized string is presented to the | ||
/// user. | ||
/// - Returns: A localized string. | ||
public static func localizedString( | ||
_ key: String, | ||
tableName: String? = nil, | ||
defaultBundle: Bundle, | ||
value: String, | ||
comment: String = "" | ||
) -> String { | ||
if let customBundle = customLocalizationBundle { | ||
let customString = NSLocalizedString( | ||
key, | ||
tableName: tableName, | ||
bundle: customBundle, | ||
value: nonExistentKeyValue, | ||
comment: comment | ||
) | ||
if customString != nonExistentKeyValue { | ||
return customString | ||
} | ||
} | ||
|
||
return NSLocalizedString(key, tableName: tableName, bundle: defaultBundle, value: value, comment: comment) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Sources/MapboxNavigationCore/Localization/String+Localization.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Foundation | ||
|
||
extension String { | ||
func localizedString( | ||
value: String, | ||
tableName: String? = nil, | ||
defaultBundle: Bundle = .mapboxNavigationUXCore, | ||
comment: String = "" | ||
) -> String { | ||
LocalizationManager.localizedString( | ||
self, | ||
tableName: tableName, | ||
defaultBundle: defaultBundle, | ||
value: value, | ||
comment: comment | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.