gpt4 book ai didi

如果不重新启动应用程序,iOS storyBoard 本地化将无法工作

转载 作者:行者123 更新时间:2023-12-03 20:58:17 25 4
gpt4 key购买 nike

我想更改应用程序的语言。我用英语和印地语为 Main.strings 创建了两个文件。还有用于印地语和英语的 Localizable.strings。这是我更改语言的方法:

 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

这是 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文件正在更改,但 Main.Strings如果不重新启动应用程序,文件不会更改。

我不知道我在这里缺少什么。

提前致谢

最佳答案

试试这个可能会帮助你:

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")

}
并且您可以向您的应用程序添加更多语言并在它们之间切换,而无需重新启动应用程序。

关于如果不重新启动应用程序,iOS storyBoard 本地化将无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59770777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com