iOS Swift 和本地化StringWithFormat [英] iOS Swift and localizedStringWithFormat

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

问题描述

我在 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 版本="1.0"><字典><key>%d 记录曲子</key><字典><key>NSStringLocalizedFormatKey</key><string>%#@num_people_in_room@ 在房间里</string><key>num_people_in_room</key><字典><key>NSStringFormatSpecTypeKey</key><string>NSStringPluralRuleType</string><key>NSStringFormatValueTypeKey</key><字符串>d</字符串><key>零</key><string>无记录</string><key>一个</key><string>只有一条记录</string><key>其他</key><string>一些记录</string></dict></dict></dict></plist>

和代码:

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

知道如何在 Swift 中做到这一点吗?

解决方案

同样的方法在 Swift 中可用:

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

输出:

<上一页>房间里没有记录房间里只有一张唱片房间里的一些记录房间里的一些记录

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

Swift 3/4 更新:

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

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>

and the code:

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

Any idea on how to do this in 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)
}

Output:

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

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

Update for Swift 3/4:

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

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

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