Created
November 23, 2020 16:40
-
-
Save mukyasa/57b777a6c6e898c7331539eb99e96847 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
final class AppLanguageManager { | |
static let shared = AppLanguageManager() | |
private(set) var currentLanguage: String | |
private(set) var currentBundle: Bundle = Bundle.main | |
var bundle: Bundle { | |
return currentBundle | |
} | |
private init() { | |
if let appLanguage = UserDefaults.standard.string(forKey: "AppLanguage") { | |
currentLanguage = appLanguage | |
} else { | |
currentLanguage = Locale.current.languageCode! | |
} | |
} | |
func setAppLanguage(_ languageCode: String) { | |
setCurrentLanguage(languageCode) | |
setCurrentBundlePath(languageCode) | |
} | |
private func setCurrentLanguage(_ languageCode: String) { | |
currentLanguage = languageCode | |
UserDefaults.standard.setValue(languageCode, | |
forKey: "AppLanguage") | |
} | |
private func setCurrentBundlePath(_ languageCode: String) { | |
guard let bundle = Bundle.main.path(forResource: languageCode, | |
ofType: "lproj"), | |
let langBundle = Bundle(path: bundle) else { | |
currentBundle = Bundle.main | |
return | |
} | |
currentBundle = langBundle | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment