格式包含2个int参数

格式包含2个int参数

本文介绍了iOS.多元化. Stringdict,格式包含2个int参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于,但我试图了解是否有可能将2个int参数传递给字符串dict.

My question is similar to How to add regular string placeholders to a translated plurals .stringdict in swift ios but I am trying to understand if it is possible to pass 2 int parameters to strings dict.

如果我要翻译以下内容,请说

Say if I want to translate something like:

1 apple : 3 pears
2 apples : 1 pear

是否可以使用一种本地化格式的字符串来完成此操作,如:

Is it possible to do it in one localized format string like:

let apples = 1
let pears = 3
let applesAndPears = String.localizedStringWithFormat(<format>, apples, pears)
print(applesAndPears)

还是我必须将它们分别组合?

or do I have to combine them separately?

推荐答案

一种格式就足够了.您可以在NSStringLocalizedFormatKey条目中使用多个占位符,并为每个占位符使用带复数规则的单独词典.示例:

One format is sufficient. You can use multiple placeholders in the NSStringLocalizedFormatKey entry, and for each placeholder a separate dictionary with the plural rule. 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>apples_and_pears</key>
        <dict>
            <key>NSStringLocalizedFormatKey</key>
            <string>%#@num_apples@ : %#@num_pears@</string>
            <key>num_apples</key>
            <dict>
                <key>NSStringFormatSpecTypeKey</key>
                <string>NSStringPluralRuleType</string>
                <key>NSStringFormatValueTypeKey</key>
                <string>ld</string>
                <key>zero</key>
                <string>no apple</string>
                <key>one</key>
                <string>1 apple</string>
                <key>other</key>
                <string>%ld apples</string>
            </dict>
            <key>num_pears</key>
            <dict>
                <key>NSStringFormatSpecTypeKey</key>
                <string>NSStringPluralRuleType</string>
                <key>NSStringFormatValueTypeKey</key>
                <string>ld</string>
                <key>zero</key>
                <string>no pear</string>
                <key>one</key>
                <string>1 pear</string>
                <key>other</key>
                <string>%ld pears</string>
            </dict>
        </dict>
    </dict>
</plist>

用法:

let apples = 1
let pears = 3
let format = NSLocalizedString("apples_and_pears", comment: "")
let applesAndPears = String.localizedStringWithFormat(format, apples, pears)
print(applesAndPears) // 1 apple : 3 pears

这可以与位置参数结合使用:如果格式更改为

This can be combined with positional parameters: if the format is changed to

        <key>NSStringLocalizedFormatKey</key>
        <string>%2$#@num_pears@ : %1$#@num_apples@</string>

然后输出为"3个梨:1个苹果".

then the output becomes "3 pears : 1 apple".

这篇关于iOS.多元化. Stringdict,格式包含2个int参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:37