ios本地化
If you are already familiar with localizing plurals in iOS and you are just looking to remember the specifics, the cheat sheet below will help you out.
如果您已经熟悉在iOS中对复数进行本地化并且只是想记住具体细节,则下面的备忘单将为您提供帮助。
If you are new to this, skip the cheat sheet for now and read the basics below.
如果您是新手,请暂时略过备忘单,并阅读以下基础知识。
备忘单 (Cheat sheet)
基本 (Basics)
Every time you want to localize texts like My dog ate 2 carrots
where the carrot count is dynamic, one localization string will not be enough. For example, localizing with My dog ate %i carrots
would produce My dog ate 1 carrots
when passing in 1
.
每次您要对文本进行本地化时,例如“ My dog ate 2 carrots
,而这些胡萝卜的数量是动态的,一个本地化字符串是不够的。 例如,用My dog ate %i carrots
本地化将在传递1
时产生My dog ate 1 carrots
。
The first solution that may come to your mind could be to create another localization string for one carrot and adding some logic to your code like:
您可能想到的第一个解决方案可能是为一个胡萝卜创建另一个本地化字符串,并向代码中添加一些逻辑,例如:
if carrotCount == 1 {
label.text = NSLocalizedString("dog_eating_carrots_one")
} else {
label.text = String(format: NSLocalizedString("dog_eating_carrots_multiple"), carrotCount)
}
This solution may work for some languages, but different languages vary in how they handle plurals. For example, in Russian you would have to handle more cases. Plural rules for the word dog look like this:
此解决方案可能对某些语言有效,但是不同的语言在处理复数形式方面会有所不同。 例如,用俄语,您将不得不处理更多案件。 狗一词的复数规则如下:
собака
for1, 21, 31, 41, 51, 61
etc. Examples:1 собака, 21 собака
собака
为1, 21, 31, 41, 51, 61
个。实施例:1 собака, 21 собака
собаки
for2-4, 22-24, 32-34
etc. Examples:2 собаки, 22 собаки
собаки
为2-4, 22-24, 32-34
等。实施例:2 собаки, 22 собаки
собак
for5-20, 25-30, 35-40
etc. Examples:5 собак, 20 собак
собак
为5-20, 25-30, 35-40
等。实施例:5 собак, 20 собак
It would be complicated to handle this logic in code. So here comes Localizable.strigsdict
to the rescue.
在代码中处理此逻辑将很复杂。 因此,出现了Localizable.strigsdict
进行了救援。
Unlike a Localizable.strings
file, a Localizable.strigsdict
file provides additional features to work with different languages, for example to define plural rules. The Localizable.strigsdict
is able to interpret the arguments you pass in and select the right localized string based on it.
与Localizable.strings
文件不同, Localizable.strigsdict
文件提供了其他功能以使用不同的语言,例如定义复数规则。 Localizable.strigsdict
能够解释您传入的参数,并根据其选择正确的本地化字符串。
创建和本地化Localizable.stringdict
文件 (Creating and localizing a Localizable.stringdict
file)
To create a Localizable.stringdict
file select a .stringdict
template from Xcode's dialog when creating a new file.
要创建Localizable.stringdict
文件,请在创建新文件时从Xcode的对话框中选择一个.stringdict
模板。
![Image for post](https://miro.medium.com/max/9999/0*unGZqUWTBhaNNXlB.png)
The localization process is exactly the same as for Localizable.strings
files. If you are new to localization in iOS in general, you can catch up by reading Paul Hudson's article on this topic.
本地化过程与Localizable.strings
文件完全相同。 如果您通常不熟悉iOS的本地化功能,则可以阅读Paul Hudson关于该主题的文章,以了解最新信息。
添加复数配置 (Adding plural configuration)
Now, you can add the plural configuration for the specific language. The template gives you a good starting point.
现在,您可以为特定语言添加复数配置。 该模板为您提供了一个很好的起点。
![Image for post](https://miro.medium.com/max/9999/0*0tGR4H6Oa_Qtu7rO.png)
The configuration for our example could look like this:
本示例的配置如下所示:
![Image for post](https://miro.medium.com/max/9999/0*yA0f7L7uTfKtn8S2.png)
Let’s go through it step by step.
让我们逐步进行。
The value is the localized text with parameters. In our case it’s
My dog ate %#@carrotsCount@.
该值是带有参数的本地化文本。 在我们的情况下,这是
My dog ate %#@carrotsCount@.
Every variable you define should be preceded by
%#@
and followed by@
您定义的每个变量都应在
%#@
之后,并在@
- You only need to define variables for parameters where plural rules should be applied 您只需要为应应用复数规则的参数定义变量
If you just want to pass something like the dog’s name, you can simply use the string format specifiers you already know like
%@
,%d
etc. To pass the dog's name, our format string would look like this:My dog %@ ate %#@carrotsCount@.
如果您只想传递狗的名字,则可以简单地使用
%@
,%d
等已知的字符串格式说明符。要传递狗的名字,我们的格式字符串应如下所示:My dog %@ ate %#@carrotsCount@.
在属性列表和源代码之间切换 (Switching between property list and source code)
Xcode provides a user interface to work with property list files. Depending on your preferences, you can also directly work with the source code. To switch, right-click on the Localizable.stringdict
file and choose Open as -> Source Code
. Switching back is the same with Open as -> Property List
.
Xcode提供了一个使用属性列表文件的用户界面。 根据您的首选项,您也可以直接使用源代码。 要进行切换,请右键单击Localizable.stringdict
文件,然后选择Open as -> Source Code
Localizable.stringdict
Open as -> Source Code
。 回切与“ Open as -> Property List
。
Here is the code for our example from above:
这是上面示例的代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple <plist version="1.0">
<dict>
<key>dog_eating_carrots</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>My dog ate %#@carrotsCount@.</string>
<key>carrotsCount</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>zero</key>
<string>no carrots</string>
<key>one</key>
<string>1 carrot</string>
<key>other</key>
<string>%d carrots</string>
</dict>
</dict>
</dict>
</plist>
结论 (Conclusion)
Now that you are familiar with plurals localization, you can jump back to the cheat sheet on the top of the article and look at another example with 2 variables. To get fast results, you can just copy the souce code und paste it into your .stringsdict file as a starting point.
既然您已经熟悉了复数形式的本地化,那么您可以跳回到文章顶部的备忘单,然后看看另一个带有2个变量的示例。 为了获得快速结果,您可以复制源代码并将其粘贴到.stringsdict文件中作为起点。
Originally published at https://www.tanaschita.com.
最初在 https://www.tanaschita.com上 发布 。
翻译自: https://levelup.gitconnected.com/step-by-step-guide-for-localizing-plurals-in-ios-57f9deaade3e
ios本地化