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
Airpods Not Working as an Input Source for Voice Recorder App
Swift - Json Error: the Data Couldn'T Be Read Because It Isn'T in the Correct Format
Swift: Reload a View Controller
Redirect to Application If Installed, Otherwise to App Store
Update Badge With Push Notification While App in Background
Scrolling Issue on Position Fixed Element on Ios
How to Clear the Entered Textfield When the User Taps on the Button
Force View Controller to Reload to Refresh Uiappearance Changes
Ios: How to Get a Proper Month Name from a Number
Ios: How to Make a Shadow for Uiview on 4 Side (Top,Right,Bottom and Left)
Wkwebview Not Loading Webpage - Renders Blank Screen in Swift
How to Download Files Using Axios
Download Array of Images as Byte Array and Convert to Images
How to Center Horizontally Uicollectionview Cells
Send Array of Request Body in an Alamofire Request