本地化在Xcode 10

问题描述 投票:0回答:1

我有所有用户面临的字符串使用NSLocalizableString()的应用程序。所以我看了WWDC 2018的视频New Localization Workflows in Xcode 10我从来没有本地化的应用程序。从那里,我创建了一个新的项目,单一视图应用。基本定位是在默认情况下。在故事板我添加一个的UILabel并在视图的中心约束它。我在视图控制器加入一个出口到它:

@IBOutlet weak var label: UILabel!

我设置其文本中viewDidLoad中:

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

构建并运行和正常工作。

现在,我想补充一个定位。我选择项目并添加西班牙语。

enter image description here

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

es.xcloc -> LocalizedContents -> es.xliff

在那里我找到了“Hello World”的键,我将值改为“世界报HOLA”并保存文件。

...
<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”。它是大写的,因为这说明有没有翻译的本土化调试。

我在想什么?

ios xcode localization nslocalizedstring
1个回答
1
投票

在这里,我们有几个问题。首先,当你做什么NSLocalizedString("Hello World", comment: "")实际情况是,你尝试获取与关键Hello World本地化的字符串,因为Xcode中找不到键,返回键本身,你的标签设置。因此,最佳做法,考虑使用不包含上有一个空白的关键。也许像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文件。点击它,看看插入的Xcode下File Inspector部分右侧的Localization。您将在该文件只本地化Spanish。所以,尽管选择English复选框。然后,你Localizable.strings将可编辑的英语和西班牙语,你可以在下面的图中看到:

enter image description here

现在,编辑该文件的英文版为您的应用程序有两种语言。

© www.soinside.com 2019 - 2024. All rights reserved.