Setting "AppleLanguages" doesn't change app language

You have to set the AppleLanguages key with an array, not a string:

UserDefaults.standard.set(["de"], forKey: "AppleLanguages")

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.


Here is how you change the language prior to launch in swift -

Lets say i want to force Hebrew localization:

1. Create your own Main.swift class

import Foundation
import UIKit

// Your initialization code here
let langCultureCode: String = "he_IL"
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject([langCultureCode], forKey: "AppleLanguages")
defaults.synchronize()

UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate))

2. Remove your AppDelegate.swift "main" responsibility

//@UIApplicationMain <-- COMMENT THIS OUT
class AppDelegate: UIResponder, UIApplicationDelegate 

This will make your app forced to a locale WITHOUT needing any launch

Tags:

Ios

Swift

Ios8