gpt4 book ai didi

ios - Xcode 10 中的本地化

转载 作者:行者123 更新时间:2023-12-01 15:43:00 24 4
gpt4 key购买 nike

我有一个应用程序,其中所有面向用户的字符串都使用 NSLocalizableString()。我从未本地化过应用程序,所以我观看了 WWDC 2018 视频 New Localization Workflows in Xcode 10 .从那里我创建了一个新项目,一个单一 View 应用程序。默认情况下,基本本地化处于启用状态。在 Storyboard上,我添加了一个 UILabel 并将其限制在 View 的中心。我在 View Controller 中添加了一个导出:

@IBOutlet weak var label: UILabel!

然后我在 viewDidLoad 中设置它的文本:

label.text = NSLocalizedString("Hello World", comment: "")

构建并运行,它工作正常。

现在我添加一个本地化。我选择项目并添加西类牙语。

enter image description here

然后我在编辑器菜单上导出本地化...。我通过这个路径打开文件:

es.xcloc -> LocalizedContents -> es.xliff

在那里我找到了“Hello World”键并将值更改为“Hola Mundo”并保存文件。

...
<file original="MyProject/en.lproj/Localizable.strings" datatype="plaintext" source-language="en" target-language="es">
<header>
<tool tool-id="com.apple.dt.xcode" tool-name="Xcode" tool-version="10.1" build-num="10B61"/>
</header>
<body>
<trans-unit id="Hello World">
<source>Hola Mundo</source>
<note>No comment provided by engineer.</note>
</trans-unit>
</body>
</file>
...

然后回到 Xcode 和导入本地化。这是第一个问题:

enter image description here

为什么这是一个不匹配的翻译?

我还是继续导入。我按如下方式编辑方案:

enter image description here

当我运行时,我得到“HELLO WORLD”。它是大写的,因为这是本地化调试,表明没有翻译。

我错过了什么?

最佳答案

我们这里有一些问题。首先,当你做 NSLocalizedString("Hello World", comment: "")实际发生的是您尝试使用键 Hello World 获取本地化字符串由于 Xcode 找不到该键,它会返回您为标签设置的键本身。因此,为了获得最佳实践,请考虑使用不包含空格的 key 。也许像 hello-world-label .然后,它将是:

NSLocalizedString("hello-world-label", comment: "")

这将在您导出本地化后生成以下内容:

<trans-unit id="hello-world-label">
<source>hello-world-label</source>
<note>No comment provided by engineer.</note>
</trans-unit>

现在您必须添加 <target>标签来指定实际的翻译。因此:

<trans-unit id="hello-world-label">
<source>hello-world-label</source>
<target>Hola Mundo</target>
<note>No comment provided by engineer.</note>
</trans-unit>

然后只需将本地化文件导回即可,您应该没问题。

编辑

这样做之后,Localizable.strings文件应该已经生成。单击它并查看 File Inspector Xcode右侧Localization下部分。您会发现该文件仅针对 Spanish 进行了本地化.所以继续选择English复选框。然后,你的 Localizable.strings将针对英语和西类牙语进行编辑,如下图所示:

enter image description here

现在您编辑该文件的英文版本,以便在您的应用中同时使用这两种语言。

关于ios - Xcode 10 中的本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54544069/

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