【问题标题】:How can I check the values of NSLocalizedString in multiple locales?如何在多个语言环境中检查 NSLocalizedString 的值?
【发布时间】:2020-08-28 03:07:54
【问题描述】:

我们的 iOS 应用被翻译成 19 种语言,最近我发现屏幕上的两个按钮标题相同的问题 - 这意味着用户不知道该按哪个!

我想编写一个单元测试来检查特定语言环境中的两个 NSLocalizedString 键,并确保它们不会发生冲突。

如何在任意语言环境中查找 NSLocalizedString 的值?

(例如,假设我想查找“Delete”和“Remove”的翻译版本,或类似的东西,并确保这两个翻译的字符串不相同。)

(我猜答案会涉及到Bundle?)

这是我正在寻找的伪代码:

class TranslationTest: XCTest {
  func testNoTranslationCollision() {
    let allLocales = // list of all supported locales in the app
    allLocales.forEach { locale in
      let translatedStringA = "Remove".asTranslatedIn(locale)
      let translatedStringB = "Delete".asTranslatedIn(locale)
      XCTAssertNotEqual(
        translatedStringA, 
        translatedStringB, 
        "The translations for these different strings shouldn't be identical!"
      )
    }
  }
}

【问题讨论】:

    标签: ios localization nsbundle nslocalizedstring


    【解决方案1】:

    现在在 Xcode 11 中,您可以使用测试计划为每个测试定义不同的配置。所以定义不同的配置,每个配置都使用特定的语言启动应用程序。如果应用程序语言中没有显示波斯语等特定语言,请在启动时传递的参数中添加其语言代码,如下所示:

    -AppleLanguages (en)
    

    在您的情况下,首先您需要创建一个支持的语言代码数组并遍历数组并设置语言并比较您的字符串

    let stringA = NSLocalizedString("Remove",comment: "stringA")
    let stringB = NSLocalizedString("Delete",comment: "stringB")
    XCTAssertNotEqual(
        stringA, 
        stringB, 
        "The translations for these different strings shouldn't be identical!"
      )
    

    【讨论】:

    • 啊,这看起来很有希望!我去看看 - 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多