Get available locales of Swift Package

Hey everybody,

I'm building a Swift Package, that has a Resource folder with multiple $lang.lproj folders containing .strings files for multiple locales. I created those according to Apple Developer Documentation.

My first question is if it is possible - at runtime - to get all of those locales (e.g. Array of ["en", "de", "fr"]) which are derived from the $lang.lproj folders available.

My second question:
To my understanding, it seems that at runtime, the Swift Package figures out the best fit based on the set device language and the locales available in the Resource folder. Is it possible to override this? For example, the Swift Package contains en.lproj and de.lproj and the device is set to English, but the implementing App of the Swift Package would like to override it to German.

Kind regards,
Felix

Bundle’s localizations will get you the list. You can pull a resource from in a specific localization with url(forResource:withExtension:subdirectory:localization:).

As for the .strings format, localizedString(forKey:value:table:) lacks a localization parameter, so you will have to load the file as a dictionary and look up the key yourself. NSDictionary’s init(contentsOf:error:) can parse the .strings format.

@SDGGiesbrecht Thank you very much, exactly what I was looking for.

Terms of Service

Privacy Policy

Cookie Policy