[英]iOS storyBoard localization is not working without restart of the application
I want to change the language of the application.我想更改应用程序的语言。 I have created two files for Main.strings in English and Hindi.我用英语和印地语为 Main.strings 创建了两个文件。 Also Localizable.strings for both Hindi and English.还有用于印地语和英语的 Localizable.strings。 Here is how I change the language :这是我更改语言的方法:
LocalizationSystem.sharedInstance.setLanguage(languageCode: "hi")
UIView.appearance().semanticContentAttribute = .forceLeftToRight
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SwipeViewController")as! SwipeViewController
let navigationController = UINavigationController(rootViewController: vc)
navigationController.modalPresentationStyle = .overFullScreen
UIApplication.shared.keyWindow?.rootViewController = navigationController
Here is the LocalizationSystem class这是 LocalizationSystem 类
class LocalizationSystem : NSObject {
var bundle: Bundle!
class var sharedInstance: LocalizationSystem {
struct Singleton {
static let instance: LocalizationSystem = LocalizationSystem()
}
return Singleton.instance
}
override init() {
super.init()
bundle = Bundle.main
}
func localizedStringForKey(key:String, comment:String) -> String {
return bundle.localizedString(forKey: key, value: comment, table: nil)
}
func localizedImagePathForImg(imagename:String, type:String) -> String {
guard let imagePath = bundle.path(forResource: imagename, ofType: type) else {
return ""
}
return imagePath
}
func setLanguage(languageCode:String) {
var appleLanguages = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
appleLanguages.remove(at: 0)
appleLanguages.insert(languageCode, at: 0)
UserDefaults.standard.set(appleLanguages, forKey: "AppleLanguages")
UserDefaults.standard.synchronize() //needs restrat
if let languageDirectoryPath = Bundle.main.path(forResource: languageCode, ofType: "lproj") {
bundle = Bundle.init(path: languageDirectoryPath)
} else {
resetLocalization()
}
}
func resetLocalization() {
bundle = Bundle.main
}
func getLanguage() -> String {
let appleLanguages = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
let preferredLanguage = appleLanguages[0]
if preferredLanguage.contains("-") {
let array = preferredLanguage.components(separatedBy: "-")
return array[0]
}
return preferredLanguage
}
}
Localizable.strings
file is changing but Main.Strings
file doesn't changes without restart of the application. Localizable.strings
文件正在更改,但Main.Strings
文件不会在不重新启动应用程序的情况下更改。
I don't know what I am missing here.我不知道我在这里缺少什么。
Thanks in advance提前致谢
Try this may Helps you:试试这个可能会帮助你:
if LocalizationSystem.sharedInstance.getLanguage() == "en" {
Bundle.main.path(forResource: "en", ofType: "lproj")
} else if LocalizationSystem.sharedInstance.getLanguage() == "hi" {
Bundle.main.path(forResource: "hi", ofType: "lproj")
}
and you can add many more languages to your app and switch between them without restarting the app.并且您可以向您的应用程序添加更多语言并在它们之间切换,而无需重新启动应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.