Setting "Applelanguages" Doesn't Change App Language

Setting AppleLanguages doesn't change app language

Yes you can change the app language immediately like,

var language = "de"
let path = NSBundle.mainBundle().pathForResource(language, ofType: "lproj")
let bundle = NSBundle(path: path!)
let string = bundle?.localizedStringForKey("key", value: nil, table: nil)

use your NSUserDefaults value to language.

iOS App supporting specific languages: language setting is persistent but shouldn't

Found the solution for myself by googling a bit more.

After we've done the language juggling and setting it in the NSUserDefaults (as shown above) we'd like to "reset" the AppleLanguages object in the NSUserDefaults standardUserDefaults.

This is achieved with the following command:

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AppleLanguages"]

Where to put this command?

In your delegates didFinishLaunchingWithOptions.

It's safe to remove at this point because all the bundle initialisation has been completed (in the main method). The AppleLanguages object is being restored and then reflects the system settings again.

Thanks to: iPhone App Localization - English problems?

How to change iPhone app language during runtime?

I doubt you can do this, even the Settings app cannot do it.

(When you change the language in the Settings app, the screen goes black, and displays "setting language..." and a progress wheel. After a long wait, you are back in Springboard. It almost looks like the phone reboots.)

Set default language before the app starts for the first time in iOS/Swift

Well, there was not one single legitimate way to do it. So I had to use a nasty trick.

  1. Added a language that we're not using (in this case fr)
    Sample Image
  2. In the process of creation, chose english (yy-YY as in my question) as the reference language (So that I wouldn't have to do any translation/localization again)
    Sample Image

At this point, my development language kicked in, and xx-XX (in this case fa-IR) was my default. And you know the rest, setting the AppleLanguages user default.

Again, this is not the best and a clean way to do it, and if you are early in the development process, go with a custom implementation of localizing your app, or use L10n-Swift. That is of course, if you can't stick with what iOS chooses for you based on the device locale selected by the user.

Localization in Swift - Restrict app to one language even if the app has more languages

Here's a method that do what you want. I don't think there's another way to do that in the code.

// You can change 'Base' to 'en' if you don't have Base.lproj folder 
func localizedString(_ key: String) -> String? {
if let path = Bundle.main.path(forResource: "Base", ofType: ".lproj"),
let baseBundle = Bundle(path: path) {
return baseBundle.localizedString(forKey: key, value: nil, table: nil)
}
return nil
}

EDIT: I found another way to do it based on this answer.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UserDefaults.standard.set(["Base"], forKey: "AppleLanguages")
UserDefaults.standard.synchronize()
// Rest of your init code
}

EDIT 2:

What you could do for the first launch of the app in the viewDidLoad of your initial ViewController:

    if let languageArray = UserDefaults.standard.object(forKey: "AppleLanguages") as? [String],
languageArray.count >= 1,
languageArray[0] != "Base" && languageArray.count == 1 {
UserDefaults.standard.set(["Base"], forKey: "AppleLanguages")
UserDefaults.standard.synchronize()
}


Related Topics



Leave a reply



Submit