I tried to build LocalizedKeyString using String's new Initializer.
String(localized: "hello",locale: locale)
When I change the language setting of the device, everything works as expected.
However, when I try to get the string in a specific language using the specified Locale, I can only ever get the string in .current.
String(localized: "hello",locale: Locale(identifier: "zh-cn"))
func getString(locale:Locale) -> String{
String(localized: "hello",locale: locale)
}
If you change the display language of the project (or the language setting of the device), the text in Text is always displayed correctly.
Text(getString(locale:.current))
However, the code in onAppear
print(getString(locale:Locale(identifier: "zh-cn")))
It only displays the same content as Text, but not the specified Chinese.
Is it my fault that the new Initializer The understanding is not correct, or the bug of String
init(localized keyAndValue: String.LocalizationValue, table: String? = nil, bundle: Bundle? = nil, locale: Locale = .current, comment: StaticString? = nil)
FB number: FB9675845
Hi,
The locale
parameter of String(localized:locale:)
helps specifying a locale for interpolated values in the string.
For instance, this would display a date in the English (UK) format:
String(localized: "Next Meeting: \(date, format: Date.FormatStyle(date: .numeric))", locale: Locale(identifier: "en_GB"))
// Localizable string is: Next Meeting: %@
// Returns: Next Meeting: 25/12/2020
Any reason why you'd need to load strings in a specific locale instead of the current user’s locale? Users can change their device language, but also for each app.