2

I would like to know if it is possible to use the app language on localization, and not the phone language?

I am talking about:

.configurationDisplayName("widget.name".localized())

and

.description("widget.description".localized())
pawello2222
  • 46,897
  • 22
  • 145
  • 209
Jkrist
  • 748
  • 1
  • 6
  • 24

2 Answers2

1

Yes, you can provide localised strings to both configurationDisplayName and description as they also accept LocalizedStringKey parameters:

/// Sets the localized name shown for a widget when a user adds or edits
/// the widget.
///
/// - Parameter displayName: The key for the localized name to display.
/// - Returns: A widget configuration that includes a descriptive name for
///   the widget.
public func configurationDisplayName(_ displayNameKey: LocalizedStringKey) -> some WidgetConfiguration
/// Sets the localized description shown for a widget when a user adds or
/// edits the widget.
///
/// - Parameters:
///   - descriptionKey: The key for the localized description to display.
/// - Returns: A widget configuration with a description of the widget.
public func description(_ descriptionKey: LocalizedStringKey) -> some WidgetConfiguration

For more information see:

pawello2222
  • 46,897
  • 22
  • 145
  • 209
  • I am talking about description and name. When using these, localized are being done on the device language, and I would need to use the app language. I mean that it works - localization works but localizes based on device, not the app language :) – Jkrist Oct 21 '20 at 10:59
  • Oh, I understand now - I don't think we have this possibility yet. You can inject locale to the view environment but it's not possible for Widgets. Similar thread is here: [Localize iOS 14 dynamic widget configuration](https://stackoverflow.com/q/64175497/8697793) – pawello2222 Oct 21 '20 at 12:06
1
  1. you can get the correct translation you want after initializing the language settings.
var disName: String {
    LoadLocalizeSettings()
    return "disName.key".localize()
}
  1. perform like this: .configurationDisplayName(disName)

  2. If it doesn't work? you also can pod 'Localize'

func LoadLocalizeSettings() {
        Localize.shared.update(provider: .strings)
        Localize.shared.update(bundle: bundle)
        Localize.shared.update(fileName: filename)
        if let language = youWantThis {
            update(language)
        }
        else {
            update(.english)
        }

}
Ran Marciano
  • 1,431
  • 5
  • 13
  • 30
doge
  • 11
  • 1