iOS Swift和localizedStringWithFormat [英] iOS Swift and localizedStringWithFormat

查看:298
本文介绍了iOS Swift和localizedStringWithFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在swift中找不到 localizedStringWithFormat 的替代品。我想要做的是使用 Localizable.stringsdict 使用单数/复数本地化,英语示例:

I could not find a substitute for localizedStringWithFormat in swift. What I am trying to do is to use the singular/plural localization using the Localizable.stringsdict, english example:

<?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中执行此操作的想法?

Any idea on how to do this in Swift?

推荐答案

Swift中提供了相同的方法:

The same methods are available in 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文件(可能为空)。

Note that in addition to the "Localizable.stringsdict" file there needs to be a "Localizable.strings" file (which may be empty).

更新Swift 3/4:

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

这篇关于iOS Swift和localizedStringWithFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆