jjzjj

iOS Swift 和 localizedStringWithFormat

coder 2023-07-16 原文

我无法在 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

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 和 localizedStringWithFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25862960/

有关iOS Swift 和 localizedStringWithFormat的更多相关文章

  1. ios - Swift 3 (Xcode 8 beta 6) localizedStringWithFormat - 2

    有没有更好的方法来获取本地化格式并用Xcode8Swift3替换字符串?我是否需要使用NSString,并在NSString和String之间来回转换?letlocalizedDue=NSString.localizedStringWithFormat(NSLocalizedString("Due:%@",comment:"duedatelabelwithdate")asNSString,formattedDate)asString 最佳答案 我可能遗漏了一些东西,但在Swift3中,String有一个类型方法localizedSt

  2. ios - 使用正确的 NumberFormatter 和 localizedStringWithFormat 的手动语言 - 2

    我在我的iOS应用程序中添加了一个手动语言部分,您可以在其中更改应用程序的显示语言。如果有人选择选择手动语言,我会像这样覆盖“AppleLanguages”standardUserDefaultsNSString*language=[[[NSUserDefaultsalloc]initWithSuiteName:kAppGroup]objectForKey:kManualLanguageKey];[[NSUserDefaultsstandardUserDefaults]setObject:[NSArrayarrayWithObjects:language,nil]forKey:@"Ap

  3. iOS Swift 和 localizedStringWithFormat - 2

    我无法在swift中找到localizedStringWithFormat的替代品。我想做的是使用Localizable.stringsdict来使用单数/复数本地化,英语示例:%drecordtrovatiNSStringLocalizedFormatKey%#@num_people_in_room@intheroomnum_people_in_roomNSStringFormatSpecTypeKeyNSStringPluralRuleTypeNSStringFormatValueTypeKeydzeroNorecordoneOnlyonerecordotherSomerecord

  4. iOS Swift 和 localizedStringWithFormat - 2

    我无法在swift中找到localizedStringWithFormat的替代品。我想做的是使用Localizable.stringsdict来使用单数/复数本地化,英语示例:%drecordtrovatiNSStringLocalizedFormatKey%#@num_people_in_room@intheroomnum_people_in_roomNSStringFormatSpecTypeKeyNSStringPluralRuleTypeNSStringFormatValueTypeKeydzeroNorecordoneOnlyonerecordotherSomerecord

随机推荐