Swift:使用Localizable.stringsdict

时间:2018-09-17 10:09:16

标签: ios swift xcode localization

我尝试使用Localizable.stringsdict在快速的应用程序中处理复数形式。

到目前为止,一切正常,但没有大数字的千位分隔符。有什么办法可以得到像这样的结果:

1 new Continent
1,000 new Continents

此刻我得到

1 new Continent
1000 new Continents

这是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>WNC_NewContinents</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%#@Variable@</string>
        <key>Variable</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>
            <key>zero</key>
            <string>%d new Continents</string>
            <key>one</key>
            <string>%d new Continent</string>
            <key>other</key>
            <string>%d new Continents</string>
        </dict>
    </dict>

这是我使用的Swift代码

   var bodyText: String = ""
   let item1 : Int = 1
   let item1000 : Int = 1_000

   let format = NSLocalizedString("WNC_NewContinents", comment: "")

   bodyText += String.localizedStringWithFormat(format, item1)
   bodyText += "\n"
   bodyText += String.localizedStringWithFormat(format, item1000)

1 个答案:

答案 0 :(得分:1)

如何?

let s = String(format: format, locale: Locale.current, arguments: [item1000])
print(s) //1,000 new Continents

语言环境会更改数字格式。

给出字典:

<dict>
    <key>WNC_NewContinents</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%#@variable@</string>
        <key>variable</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>
            <key>zero</key>
            <string>new Continents</string>
            <key>one</key>
            <string>%d  new Continent</string>
            <key>other</key>
            <string>%d new Continents</string>
        </dict>
</dict>

这是它在操场上提供的东西:

1000