'How to reload content text after change language iOS Swift code
I have a problem when I change the application language using swift code. In my case I had to use the xliff file that was automatically generated from the storyboard/xib. My code:
let APPLE_LANGUAGE_KEY = "AppleLanguages"
/// L102Language
class L102Language {
/// get current Apple language
class func currentAppleLanguage() -> String{
let userdef = UserDefaults.standard
let langArray = userdef.object(forKey: APPLE_LANGUAGE_KEY) as! NSArray
let current = langArray.firstObject as! String
return current
}
/// set @lang to be the first in Applelanguages list
class func setAppleLAnguageTo(lang: String) {
let userdef = UserDefaults.standard
userdef.set([lang,currentAppleLanguage()], forKey: APPLE_LANGUAGE_KEY)
userdef.synchronize()
}
}
Use:
if L102Language.currentAppleLanguage() == "en" {
L102Language.setAppleLAnguageTo(lang: "vi")
UIView.appearance().semanticContentAttribute = .forceRightToLeft
} else {
L102Language.setAppleLAnguageTo(lang: "en")
UIView.appearance().semanticContentAttribute = .forceLeftToRight
}
After userdef.synchronize() is executed the application does not change the language. It only really works when I restart the app. I think this way is not good. In this case, what else do I need to do to change the language of the application without restarting.
thanks everyone
Update: I resolved problem with answer https://stackoverflow.com/a/48187049/12429634 Thanks everyone!
Solution 1:[1]
You will need to register for the NSLocale currentLocaleDidChangeNotification, and write code to update your UI when you get notified.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Duncan C |
