How to Force Nslocalizedstring to Use a Specific Language

Force NSLocalizedString to use a specific language using Swift

It's not possible to change app's language immediately by changing the value of AppleLanguages. It requires restarting the app before the change takes effect.

It seems that your problem is accessing the localization strings of different languages rather than changing the app's language, right? If you want your app to support multiple languages, you can just provide the translations and rely on settings.app for the actual change.

If you want to access the localization strings from other than currently used localization, you need to get access to the proper translations bundle. And then just query that bundle for the translations. The following piece of code should do the trick.

let language = "en"
let path = Bundle.main.path(forResource: language, ofType: "lproj")
let bundle = Bundle(path: path!)
let string = bundle?.localizedStringForKey("key", value: nil, table: nil)

How to force NSLocalizedString to use a specific language

NSLocalizedString() (and variants thereof) access the "AppleLanguages" key in NSUserDefaults to determine what the user's settings for preferred languages are. This returns an array of language codes, with the first one being the one set by the user for their phone, and the subsequent ones used as fallbacks if a resource is not available in the preferred language. (on the desktop, the user can specify multiple languages with a custom ordering in System Preferences)

You can override the global setting for your own application if you wish by using the setObject:forKey: method to set your own language list. This will take precedence over the globally set value and be returned to any code in your application that is performing localization. The code for this would look something like:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", @"en", @"fr", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate

This would make German the preferred language for your application, with English and French as fallbacks. You would want to call this sometime early in your application's startup. You can read more about language/locale preferences here: Internationalization Programming Topics: Getting the Current Language and Locale

How to obtain NSLocalizedString for specific locale, or how to get non-localized string

You can simply call this method by sending language code & key string.

func localizestring(for languageCode : String , keyString : String) -> String {
//language code like --> en/ef/da
let path = Bundle.main.path(forResource: languageCode, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(keyString , tableName: nil, bundle: bundle!, value: "", comment: "")
}

NSLocalizedString not defaulting to Base language

the order of default languages is a user setting on OSX and not editable (AFAIK) on iOS

BUT still adhered to!

the app is passed the array AppleLanguages (or so..) that specifies the languages to try. The NSLocalizedString macro will try load each language in the array in the order they appear UNTIL it finds a working one and then it uses that

compare: How to force NSLocalizedString to use a specific language

NSLocalizedString, change language based on application preference

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"ru", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

Apparently this will do it if you put it in the init method of your application.



Related Topics



Leave a reply



Submit