【问题标题】:Getting wrong device language in swift快速获取错误的设备语言
【发布时间】:2019-12-10 18:49:56
【问题描述】:

我正在尝试快速获取当前的电话语言。我想我知道该怎么做……但我的语言有误。我的应用程序已经完成了 2 个本地化(英语和西班牙语)。但我想获得真正的 iOS 语言,所以我可以说用户,“我们正在准备很快翻译成意大利语”,或者类似的话....

我使用的是 iOS 13.2.2 / XCode 11.2.1

我已经尝试了很多方法来查看我为该代码得到的结果:

    print(NSLocale.current.languageCode as Any)
    print(Locale.current.languageCode as Any)
    print(Locale(identifier: Locale.current.identifier).identifier)
    print(Locale.preferredLanguages)
    print(NSLocale.preferredLanguages)
    print(Locale.current.regionCode as Any)
    print(NSLocale.preferredLanguages[0] )
    print(NSLocale.components(fromLocaleIdentifier:
        NSLocale.preferredLanguages.first!)[NSLocale.Key.languageCode.rawValue])

这些都没有给我正确的结果。

例如,在设置 -> 通用 -> 语言和区域 -> iPhone 语言 -> 意大利语中将 iOS 语言设置为意大利语 我得到了:

    print(NSLocale.current.languageCode as Any)
Optional("es")

    print(Locale.current.languageCode as Any)
Optional("es")

    print(Locale(identifier: Locale.current.identifier).identifier)
es_US

    print(Locale.preferredLanguages)
["es"]

    print(NSLocale.preferredLanguages)
["es"]

    print(Locale.current.regionCode as Any)
Optional("US")

    print(NSLocale.preferredLanguages[0] )
es

    print(NSLocale.components(fromLocaleIdentifier:NSLocale.preferredLanguages.first!)[NSLocale.Key.languageCode.rawValue])
Optional("es")

我怎样才能得到“它”.....

谢谢

【问题讨论】:

标签: ios swift iphone


【解决方案1】:

如果在一个长调用中合并是

NSLocale.components(fromLocaleIdentifier: 
   NSLocale.preferredLanguages.first!)[NSLocale.Key.languageCode.rawValue]

初始输出:

(lldb) e NSLocale.preferredLanguages
([String]) $R10 = 2 values {
  [0] = "it-US"
  [1] = "en"
}

使用 Xcode 11.2 / iOS 13.2 测试

PS。 iPhone 设备语言必须在应用程序启动之前更改。

【讨论】:

  • 感谢您的回复。我测试了,但得到了结果 => Optional("es")
  • 如何写入控制台输出?带有“打印”它告诉我一个警告..也许是....
  • 不显示设备语言,显示应用程序语言
【解决方案2】:

您为什么不创建默认语言的副本(假设它是英语),将其标记为意大利语并添加您的短语“我们正在准备很快翻译成意大利语”?

这样,您的应用会“认为”它支持意大利语,但实际上会显示英文文本。

这个解决方案有一些限制: 如果用户在他/她的设备中配置给定的语言层次:意大利语/西班牙语/英语,他/她会期望由于缺乏对意大利语的支持,将使用西班牙语。实际上,app会使用英语(装扮成意大利语),所以可能会引起一些混乱。

【讨论】:

  • 所以答案是不可能在手机上获得真正的语言......?
  • 我不知道该怎么做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多