Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to change Locale #1095

Open
diamid6 opened this issue Dec 22, 2023 · 6 comments
Open

Ability to change Locale #1095

diamid6 opened this issue Dec 22, 2023 · 6 comments

Comments

@diamid6
Copy link

diamid6 commented Dec 22, 2023

Could you please advise me, how can I change Locale.current here:

`private static func tr(_ table: String, _ key: String, _ args: CVarArg..., fallback value: String) -> String {

let format = BundleToken.bundle.localizedString(forKey: key, value: value, table: table)

return String(format: format, locale: Locale.current, arguments: args)
}`

@BastianKusserow
Copy link

I was just about to ask the same question. If an app has its own sense of how to localize their strings, I wonder if its correct that the String init from above gets the current locale as parameter. Apple is also not very specific about what happens with that locale (considering my format string has some string placeholders, I assume nothing will happen, but not sure about other types than strings?!).

@diamid6
Copy link
Author

diamid6 commented Jan 25, 2024

Locale affects the rule for plurals in Localized.stringdict. You may check it with two locales which has different cases for one, two, many, other. If I change locale in runtime without changing in Settings App, Locale.current is still will be the same. This can lead to incorrect string format as the result because for definition the correct plural case will being use Locale.current but not Locale(identifier: <new_locale_identifier>).

@BastianKusserow
Copy link

Ah yeah, that makes total sense! Our app has its own sense of localization and I assume that will likely break if the OS locale is different from the one of the app, which makes that lookup function a little useless for us now. I know we can always create our own template, but that would be much nicer thb..

@djbe
Copy link
Member

djbe commented Mar 15, 2024

The built-in templates support custom lookupFunctions, so you can do whatever crazy locale stuff you'd like. I would NOT recommend it at all (see @diamid6 comment about inconsistencies), but I know how projects & client requirements work 🤷

Read the template docs here: https://github.com/SwiftGen/SwiftGen/blob/stable/Documentation/templates/strings/structured-swift5.md

@BastianKusserow
Copy link

While that's true, in the end, even when using the lookup function, the last line of the tr function is always return String(format: format, locale: Locale.current, arguments: args). There the result returned from the lookup function is passed to the String(format:locale:) initializer and the locale that's passed there cannot be changed. This can lead to unexpected behavior in some cases.

@djbe
Copy link
Member

djbe commented Mar 15, 2024

Ah right, well in that case you should use custom templates (see docs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants