iOS Swift和localizedStringWithFormat

时间:2014-09-16 07:28:23

标签: ios swift localization

我无法在swift中找到localizedStringWithFormat的替代品。我想要做的是使用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">
    <dict>
        <key>%d record trovati</key>
        <dict>
            <key>NSStringLocalizedFormatKey</key>
            <string>%#@num_people_in_room@ in the room</string>
            <key>num_people_in_room</key>
            <dict>
                <key>NSStringFormatSpecTypeKey</key>
                <string>NSStringPluralRuleType</string>
                <key>NSStringFormatValueTypeKey</key>
                <string>d</string>
                <key>zero</key>
                <string>No record</string>
                <key>one</key>
                <string>Only one record</string>
                <key>other</key>
                <string>Some records</string>
            </dict>
        </dict>
    </dict>
</plist>

和代码:

[NSString localizedStringWithFormat:NSLocalizedString(@"%d record trovati", nil), totRecsFound];

有关如何在Swift中执行此操作的想法吗?

2 个答案:

答案 0 :(得分:26)

Swift中提供了相同的方法:

for totRecsFound in 0 ... 3 {
    let str = NSString.localizedStringWithFormat(NSLocalizedString("%d record trovati", comment: ""), totRecsFound)
    println(str)
}

输出:

No record in the room
Only one record in the room
Some records in the room
Some records in the room

请注意,除了“Localizable.stringsdict”文件外,还需要有一个 “Localizable.strings”文件(可能为空)。

Swift 3/4的更新:

for totRecsFound in 0 ... 3 {
    let str = String.localizedStringWithFormat(NSLocalizedString("%d record trovati", comment: ""), totRecsFound)
    print(str)
}

答案 1 :(得分:2)

我写了一个易于使用的扩展程序:

extension NSString {
  static func localizedStringForPlurals(formatKey key: String, comment: String = "", args: CVarArg... ) -> String
{
    let format = NSLocalizedString(key, comment: comment)
    let result = withVaList(args){
        (NSString(format: format, locale: NSLocale.current, arguments: $0) as String)
    }

    return result
}

以下是一些用法:

for totRecsFound in 0 ... 3 {
  let str = String. localizedStringForPlurals(formatKey: "%d record trovati", comment: "my comments", args: totRecsFound)
  println(str)
}
// Or
for totRecsFound in 0 ... 3 {
  let str = String. localizedStringForPlurals(formatKey: "%d record trovati", args: totRecsFound)
  println(str)
}

如果一个键中有多个数值(您必须在资源文件中指定每个变量,如下例所示),您可以使用此方法传递多个数字参数以获取本地化字符串。 / p>

这是有趣的部分:

<?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>record in room</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%#@num_people_in_room@ in %#@room@ (%#@comment@)</string>
        <key>num_people_in_room</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>
            <key>zero</key>
            <string>No record</string>
            <key>one</key>
            <string>Only one record</string>
            <key>other</key>
            <string>Some records</string>
        </dict>
        <key>room</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>
            <key>zero</key>
            <string>none room</string>
            <key>one</key>
            <string>%d room</string>
            <key>other</key>
            <string>%d rooms</string>
        </dict>
        <key>comment</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>
            <key>zero</key>
            <string>Weird case: how 0 record in > 0 rooms or 0 room with > 0 records?</string>
            <key>one</key>
            <string>Weird case: how 1 record in > 2 rooms?</string>
            <key>other</key>
            <string>%dth or normal case: OK</string>
        </dict>
    </dict>
</dict>
</plist>

示例代码:

var c = 2
for r in 0 ... 2 {
  for m in 0 ... 2 {
    switch (true) {
      case (r == 0 && m > 0) || (r > 0 && m == 0):
        c = 0
      case r == 1 && m > 1:
        c = 1
      default:
        c = 2
    }
    let str = String. localizedStringForPlurals(formatKey: "record in room", args: r, m, c)
    println(str)
  }
}

打印结果:

No record in none room (Normal case: OK)
No record in 1 room (Weird case: how 0 record in > 0 rooms or 0 room with > 0 records?)
No record in 2 rooms (Weird case: how 0 record in > 0 rooms or 0 room with > 0 records?)
Only one record in none room (Weird case: how 0 record in > 0 rooms or 0 room with > 0 records?)
Only one record in 1 room (Normal case: OK)
Only one record in 2 rooms (Weird case: how 1 record in > 2 rooms?)
Some records in none room (Weird case: how 0 record in > 0 rooms or 0 room with > 0 records?)
Some records in 1 room (Normal case: OK)
Some records in 2 rooms (Normal case: OK)

请参阅CLDR:Language Plural Rules了解英语。