Created
August 20, 2016 13:29
-
-
Save A1iAshoor/fefa4b505d4d2691f36a7d95b9ef4e3f to your computer and use it in GitHub Desktop.
Global Locale Language Manager in Swift 2.3 [Supports Localized Storyboards]
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
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