Xcode、Swift:如何在 iOS 应用程序中添加多语言支持并使字符串具有占位符和复数?

Xcode, Swift: how to add multi-language support in an iOS app and have strings with placeholders and plurals?

我需要在使用 Swift 以 Xcode 编写的 iOS 应用程序中添加多语言支持。我需要本地化

  1. 静态字符串
  2. 带占位符的字符串
  3. 复数(数量字符串)

例如下面Android我们在XML文件中添加命名字符串和复数:

<string name="static_string">Hello world!</string>
<string name="placeholder_string">You have %2$d new messages.</string>
<plurals name="plural_string">
    <item quantity="one">You have a new message.</item>
    <item quantity="other">You have %2$d new messages.</item>
</plurals>

并遵循 Java 以编程方式获取字符串:

res.getString(R.string.placeholder_string, mailCount)
res.getQuantityString(R.plurals.plural_string, mailCount, mailCount)

我在找Swift(iOS)

对应的解

This blog explains 详细介绍了复数的含义。 IOS.

的代码示例

in some languages, plurals don't work the same way as they do in English. Some languages don't indicate plurals (such as Japanese), while for others the word will change depending on the quantity (such as Russian).

To make things a bit easier, iOS has categorised the different plural types as follows:

  • Zero. Used to indicate a 0 quantity.
  • One. Used to indicate a quantity of exactly 1.
  • Two. Used to indicate a quantity of exactly 2.
  • Few. Used to indicate a small quantity greater than 2, but this depends on the language.
  • Many. Used to indicate a large number, but this also depends on the language.

Other. Used to indicate every number that isn't covered by the above categories.

Not all languages need all categories specified, since all work differently. At first glance, it would appear that English would require rules for zero, one and many. However, the plural rules for English would be specified with one and other, since all numbers apart from 1 are treated equally.

To specify the plural rules, you need to use a strings dictionary (Localizable.stringsdict) instead of a regular Localizable.strings file. In actual fact, you'll need the .strings file to still be present for the .stringsdict to work, even if it's empty.

In Xcode, create a new Plist file, and name it Localizable.stringsdict. Once populated, the raw data in the Plist will look as follows:

<?xml version="1.0" encoding="UTF-8"?>  
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  
    <plist version="1.0">  
    <dict>  
        <key>number_of_days</key>
        <dict>
            <key>NSStringLocalizedFormatKey</key>
            <string>%#@value@</string>
            <key>value</key>
            <dict>
                <key>NSStringFormatSpecTypeKey</key>
                <string>NSStringPluralRuleType</string>
                <key>NSStringFormatValueTypeKey</key>
                <string>d</string>
                <key>one</key>
                <string>%d day remaining</string>
                <key>other</key>
                <string>%d days remaining</string>
            </dict>
        </dict>
    </dict>  
    </plist> 

这是在代码中引用复数的方法:

let format = NSLocalizedString("number_of_days", comment: "")
let message = String.localizedStringWithFormat(format, numDays)

再次感谢Quentin

设置项目Localizations,如截图所示项目有英语和俄语。

1: 创建一个名为 Localizable.strings 的文件并添加以下行

static_string="Hello world!";
placeholder_string="You have %d new messages.";

2:我们在单独的文件中定义复数,让我们创建一个名为 Localizable.stringsdict 的文件并粘贴以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

<!-- FIRST PLURAL STRING -->
<dict>
    <key>plural_string</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%#@value@</string>
        <key>value</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>
            <key>one</key>
            <string>You have a new message.</string>
            <key>other</key>
            <string>You have %d new messages.</string>
        </dict>
    </dict>
</dict>

<!-- NEXT PLURAL STRING -->
</plist>

接下来,本地化 Localizable.stringsdict 文件,如屏幕截图所示,文件已本地化为英语和俄语:

用法:

NSLocalizedString("static_string", comment: "") // Hello world!

String(format: NSLocalizedString("placeholder_string", comment: ""), 5) // You have 5 new messages.

let plural = NSLocalizedString("plural_string", comment: "")
String(format: plural, 1) // You have a new message.
String(format: plural, 2) // You have 2 new messages.

复数规则:在XML代码中,注意<key>one</key><key>other</key>,这里可以使用zeroonetwofewmanyother,具体取决于语言和您的要求。

使用以下 link 获取有关语言特定复数规则的更多信息:http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html

如果您不熟悉字符串格式设置,本文也会有所帮助 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html