mac-cain13 / R.swift

Strong typed, autocompleted resources like images, fonts and segues in Swift projects
MIT License
9.49k stars 763 forks source link

Use current locale when overrideLocale is missing #839

Closed Lumisilk closed 1 year ago

Lumisilk commented 1 year ago

As described in issue #812, when localizing strings containing arguments like "My number: %d", Rswift does not use the locale of the current environment when the locale argument is missing (which is the default behavior). So the string cannot be formatted correctly.

For example, in Localizable.strings in Japanese, when using %d, a comma should be inserted every three digits:

"My number: %d" = "数値:%d";

Current Rswift gives us this:

R.string.localizable.myNumberD(1234567890)
// 数値:1234567890

R.string(locale: Locale(identifier: "ja")).localizable.myNumberD(1234567890)
// 数値:1,234,567,890

After PR:

R.string.localizable.myNumberD(1234567890)
// 数値:1,234,567,890

R.string(locale: Locale(identifier: "ja")).localizable.myNumberD(1234567890)
// 数値:1,234,567,890

As comparison, iOS's behaviour is

let num = 1234567890

Text("Text: \(num)")
// Text: 1,234,567,890

Text(String(format: "String format: %d", num))
// String format: 1234567890

Text(String.localizedStringWithFormat("localizedStringWithFormat: %d", num))
// localizedStringWithFormat: 1,234,567,890
tomlokhorst commented 1 year ago

Thank you for this PR!