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.
- Added a language that we're not using (in this case
fr
) - 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)
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
Find Attributes from Attributed String That User Typed
iOS 6.X Open Command Line on Jailbreak
How to Implement Login/Logout Navigation Using Userdefaults in Swift
How to Programmatically Get iOS Status Bar Height
Formatting Phone Number in Swift
How to Make Return Key on iPhone Make Keyboard Disappear
Xcode 6 Storyboard Unwind Segue with Swift Not Connecting to Exit
iOS 13 Uibarbuttonitem Not Clickable and Overlapping Uinavigationbars When Using Uisearchcontroller
Deploying an iOS Application Using Apple Enterprise Developer Program
Why Can't I Call the Default Super.Init() on Uiviewcontroller in Swift
Uitextfield -Webview No Longer Supported
Button Action in Mkannotation View Not Working
Swift - Error 'Expected ',' Separator' and 'Expected Expression in List of Expressions'
"Too Many Symbol Files" After Successfully Submitting My Apps
iOS Uialertview Button to Go to Setting App
Uifont - How to Get System Thin Font
Exc_Bad_Instruction (Code=Exc_I386_Invop, Subcode=0X0) on Dispatch_Semaphore_Dispose