本文介绍了如何处理本地化swift中的特殊情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Localizable.strings文件,翻译工作正常。但是有一个特殊情况,英语中有一个单词和复数,如'系列'。但在德语中有两个不同的词:一个系列是'Serie',两个系列是'Serien'。我该如何处理这些例外?

I created a Localizable.strings file and the translation works fine. But there is a special case where in english there is one word for singular and plural, like 'series'. But in german there are two different words: one series is 'Serie', two and more series is 'Serien'. How can I handle such exceptions?

感谢马丁

推荐答案

这正是这个

in
适用于。

因此,除了Localizable.strings文件外,您还必须提供
a Localizable.stringsdict属性列表文件,其中
复数规则的语言。在您的情况下:

So in addition to the "Localizable.strings" file you have to providea "Localizable.stringsdict" property list file withplural rules for the language. In your case:

Localizable.strings:

"%ld series" = "%ld Serien";

Localizable.stringsdict:

<plist version="1.0">
<dict>
    <key>%ld series</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%#@series@</string>
        <key>series</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>ld</string>
            <key>one</key>
            <string>%ld Serie</string>
            <key>other</key>
            <string>%ld Serien</string>
        </dict>
    </dict>
</dict>
</plist>

请注意 Int 的正确格式说明符(
可以是32位或64位整数)是%ld

Note that the proper format specifier for Int (which canbe a 32-bit or 64-bit integer) is %ld.

现在一切都自动起作用,Swift代码没有变化:

Now everything works "automatically", with no changes in the Swift code:

for n in 1...3 {
    let str = String(format: NSLocalizedString("%ld series", comment: ""), n)
    print(str)
}

输出:


1 Serie
2 Serien
3 Serien

即使添加了其他复数规则的更多语言,也不需要在Swift代码中更改

Even if more languages with other plural rules are added, no changesin the Swift code are necessary.

这篇关于如何处理本地化swift中的特殊情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 03:55