Q:

localization in ios swift. how can i change application language at run time

i am creating app in ios swift. now i want to make app in dual language so from editor menu i just export and import for localization. its create two file for me.
now i want to put two button on first page of my app. and when user press button 1 i want to load english language. and if user press button 2 i want to load turkish language. so how can i do this in swift?

A:

Instead of using NSLocalizedString method to localize your App, load the localizable strings according to the language selected and use it from your code.
Loading the data from the bundle :
// Exemple with english language
if let path = NSBundle(forClass:object_getClass(self)).URLForResource("Localizable", withExtension: "strings", subdirectory: nil, localization: "en")?.path {
    if NSFileManager.defaultManager().fileExistsAtPath(path)     {
        // Keep a reference to this dictionary
        dicoLocalisation = NSDictionary(contentsOfFile: path)
    }
}

Replacing NSLocalizedString :
func localizedStringForKey(key:String) -> String { 
    // First we check if the dictionary exists       
    if let dico = dicoLocalisation {

        if let localizedString = dico[key] as? String {
            // If the localization exists, return it
            return localizedString
        }  else {
            // Returns the key if no String was found
            return key
        }
    } else {
        // If not, we can use the NSLocalizedString function
        return NSLocalizedString(key, comment: key)
    }
}

If you want to handle this quickly, I've made a custom localisator class available on Github, which allow you to switch language from anywhere in the app and even save if for further launches of the app. The implementation is pretty much the same as what I've explained.

