Last active
May 10, 2016 10:56
-
-
Save bmoliveira/9468570951896dd5b06d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Localize: NSObject { | |
/** | |
List available languages | |
- Returns: Array of available languages. | |
*/ | |
public class func availableLanguages() -> [String] { | |
return NSBundle.mainBundle().localizations | |
} | |
/** | |
Current language | |
- Returns: The current language. String. | |
*/ | |
public class func currentLanguage() -> String { | |
if let currentLanguage = NSUserDefaults.standardUserDefaults().objectForKey(LCLCurrentLanguageKey) as? String { | |
return currentLanguage | |
} | |
return defaultLanguage() | |
} | |
/** | |
Change the current language | |
- Parameter language: Desired language. | |
*/ | |
public class func setCurrentLanguage(language: String) { | |
let selectedLanguage = availableLanguages().contains(language) ? language : defaultLanguage() | |
if (selectedLanguage != currentLanguage()){ | |
NSUserDefaults.standardUserDefaults().setObject(selectedLanguage, forKey: LCLCurrentLanguageKey) | |
NSUserDefaults.standardUserDefaults().synchronize() | |
NSNotificationCenter.defaultCenter().postNotificationName(LCLLanguageChangeNotification, object: nil) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment