Use case: I'm using the following String extension to change the language/locale in the Swift UI environment, I then pass the locale through to the function to get a localised string. I've made a little Swift UI preview container with an editor panel so we can toggle values in the environment (dark mode, layout direction, locale/language etc) on previews and also within a playbook app - without needing to select a drop-down in the Xcode scheme editor and restarting the app, or needing to change the device settings (although we have to do that when running on iOS 15.)
func localized(_ locale: Locale, bundle: Bundle = .module) -> String {
if #available(iOS 16, *) {
String(
localized: LocalizedStringResource(
String.LocalizationValue(self),
locale: locale,
bundle: .atURL(bundle.bundleURL)
)
)
} else {
String(localized: String.LocalizationValue(self), bundle: bundle)
}
}
Topic:
App & System Services
SubTopic:
General
Tags: