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

Demonstrate use of language overrides in documentation #47

Open
VadimPavlov opened this issue Mar 28, 2024 · 3 comments
Open

Demonstrate use of language overrides in documentation #47

VadimPavlov opened this issue Mar 28, 2024 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@VadimPavlov
Copy link

I know it's not a best practice, but it's common to change language inside application on the fly.
It will be nice to have ability to set Bundle.current, to override it.

@liamnichols
Copy link
Owner

Could you please share how you override language today when using LocalizedStringResource? The API has improved a bit since NSLocalizedString so you shouldn't need to do any bundle overrides anymore.

For example, in SwiftUI, you can just set the locale in the environment to override:

struct MyView: View {
    var body: some View {
        VStack {
            Text(.localizable.foo)
            Text(.localizable.bar)
        }
        .environment(\.locale, Locale(identifier: "fr"))
    }
}

For Foundation/UIKit, you can do something like this:

extension String {
    init(french resource: LocalizedStringResource) {
        var resource = resource
        resource.locale = Locale(identifier: "fr")

        self.init(localized: resource)
    }
}

// String(french: .localizable.foo)

@liamnichols liamnichols added the enhancement New feature or request label Mar 28, 2024
@VadimPavlov
Copy link
Author

Ah good point, yes, overriding environment works fine for SwiftUI view.
And for everything else it's good that we have an option to set locale on resource.
Probably this issue can be closed, but I would love to see this as example in README, thanks!

@liamnichols liamnichols added documentation Improvements or additions to documentation and removed enhancement New feature or request labels Mar 28, 2024
@liamnichols
Copy link
Owner

Thanks for confirming! Great to hear that this can help 🚀 I'll keep the ticket open to track updating some docs.

@liamnichols liamnichols changed the title Setting Bundle Demonstrate use of language overrides in documentation Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants