安装项目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>
,在这里您可以使用zero
, one
, two
, few
, many
,other
取决于语言和您的要求。
使用以下链接了解有关特定语言复数规则的更多信息: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 ,这篇文章也会有所帮助