iOS Swift和localizedStringWithFormat

栏目: Swift · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/25862960/ios-swift-and-localizedstringwithformat
我在 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中执行此操作的任何想法?

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)
}

翻译自:https://stackoverflow.com/questions/25862960/ios-swift-and-localizedstringwithformat


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Perl语言编程

Perl语言编程

[美] Larry Wall、Tom Christiansen、Jon Orwant / 何伟平 / 中国电力出版社 / 2001-12 / 129.00元

这不仅仅是一本关于Perl的书籍,更是一本独一无二的开发者自己介绍该语言及其文化的书籍。Larry Wall是Perl的开发者,他就这种语言的未来发展方向提出了自己的看法。Tom Christiansen是最早的几个拥护者之一,也是少数几个在错综复杂的中游刃有余的人之一。Jon Orwant是《Perl Journal》的主编,该杂志把Perl社区组合成了一个共同的论坛,以进行Perl新的开发。一起来看看 《Perl语言编程》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码