Skip to content

Instantly share code, notes, and snippets.

@A1iAshoor
Created August 20, 2016 13:29
Show Gist options
  • Save A1iAshoor/fefa4b505d4d2691f36a7d95b9ef4e3f to your computer and use it in GitHub Desktop.
Save A1iAshoor/fefa4b505d4d2691f36a7d95b9ef4e3f to your computer and use it in GitHub Desktop.
Global Locale Language Manager in Swift 2.3 [Supports Localized Storyboards]
class Locale {
static var language: String {
set{
defaults.setObject(newValue, forKey: "currentLanguage")
defaults.synchronize()
}
get{
if let currentLanguage: String = defaults.stringForKey("currentLanguage"){
return currentLanguage
}
return NSLocale.getTopPreferredLanguage()
}
}
static func currentLanguageBundle() -> NSBundle {
if let path = NSBundle.mainBundle().pathForResource(language, ofType: "lproj") {
return NSBundle(path: path)!
} else {
language = "en"
return NSBundle(path: NSBundle.mainBundle().pathForResource(language, ofType: "lproj")!)!
}
}
}
extension NSLocale {
static func getTopPreferredLanguage() -> String {
return NSLocale.preferredLanguages()[0]
}
static func getTopPreferredCountryCode() -> String {
let locale = NSLocale(localeIdentifier: self.getTopPreferredLanguage())
return locale.objectForKey(NSLocaleCountryCode) as! String
}
}
extension String {
var localized: String {
return Locale.currentLanguageBundle().localizedStringForKey(self, value: "", table: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment