【问题标题】:Changing app language on the fly in iOS在 iOS 中即时更改应用程序语言
【发布时间】:2015-12-01 16:15:49
【问题描述】:

我是 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()

此外,我在情节提要控制器中硬编码了文本,我也希望将它们本地化。我怎样才能做到这一点?

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

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()
    }
}

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

【问题讨论】:

    标签: swift nsstring swift2 nsuserdefaults swift2.1


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-09-03
      • 1970-01-01
      • 2013-12-19
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 2014-12-24
      • 2013-03-04
      • 1970-01-01
      相关资源
      最近更新 更多