gpt4 book ai didi

swift - 在 iOS 中动态更改应用程序语言

转载 作者:行者123 更新时间:2023-11-30 13:53:01 28 4
gpt4 key购买 nike

我是 iOS 新手,我知道这是一个非常常见的问题,但我没有找到适合我的查询的答案。

我已经向应用程序的控件添加了文本,如下所示

        let productDescriptionCell = tableView.dequeueReusableCellWithIdentifier("textDescriptionCell") as! TextDescriptionCell
productDescriptionCell.labelForCell.text = "Description"
productDescriptionCell.labelForCell.sizeToFit()
productDescriptionCell.labelForCell.adjustsFontSizeToFitWidth = true

如上面的“描述”文本。

我想在我的应用程序中添加一个按钮,让用户可以即时更改语言。我发现以下代码最相关,但这需要重新启动应用程序。

userDefaults.setObject(["\(cc)"], forKey: "AppleLanguages")
userDefaults.synchronize()

此外,我在 Storyboard Controller 中硬编码了文本,我也希望将它们本地化。我怎样才能实现这个目标?

泰语和英语是我需要本地化的语言

productTitleCell.textFieldForCell.placeholder = SpecProvider.spec.localizedTuv(createLocalizedString("en", tuvEnglish: "Enter Title", tuvThai: "ป้อนชื่อ"))

func localizedTuv(localizedString: LocalizedString) -> String {
var locale = LocaleSelectionService.sharedInstance.getCachedLocale()
if locale.isEmpty {
locale = ""
}
switch(locale) {
case "en": return localizedString.getTuvEnglish()
case "th": return localizedString.getTuvThai()
default: return localizedString.getTuvEnglish()
}
}

我已经尝试过上面的代码,但这可能会变得非常乏味。请帮忙,我真的很困难!

最佳答案

您必须创建字符串文件,并为您想要支持的每种语言添加标签。完成此操作后,您可以使用 NSLocalizedString 作为 ViewController 中使用的文本。

这是教程 http://www.raywenderlich.com/64401/internationalization-tutorial-for-ios-2014

关于swift - 在 iOS 中动态更改应用程序语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34024905/

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