货币复数Swift不允许配置。

3
我希望从 NumberFormatter 中删除一个点(“.”),因为VoiceOver会将20.000(西班牙语中的点是分隔符)读成20而不是20000。
在下面的示例中,您可以测试只有currencyPlural是唯一一个完整宣布货币字符串但忽略 usesGroupingSeparator 标志的 NumberFormatter.Style
示例:
func formatTest(_ style:  NumberFormatter.Style, usesGroupingSeparator separator: Bool) -> String? {
    let formatter = NumberFormatter()
    formatter.locale = Locale(identifier: "es_CL")
    formatter.usesGroupingSeparator = separator
    formatter.numberStyle = style
    return formatter.string(from: NSNumber(value: 20000))
}


formatTest(.currency, usesGroupingSeparator: false) // "$20000"
formatTest(.currencyAccounting, usesGroupingSeparator: false) // "$20000"
formatTest(.currencyISOCode, usesGroupingSeparator: false) // "CLP 20000"
formatTest(.currencyPlural, usesGroupingSeparator: false) // "20.000 pesos chilenos"

formatTest(.currency, usesGroupingSeparator: true) // "$20.000"
formatTest(.currencyAccounting, usesGroupingSeparator: true) // "$20.000"
formatTest(.currencyISOCode, usesGroupingSeparator: true) // "CLP 20.000"
formatTest(.currencyPlural, usesGroupingSeparator: true) // "20.000 pesos chilenos"

有什么办法可以修复这个问题吗?请不要回答手动删除点,因为我想为多种语言使用格式。
附加信息:西班牙语配音总是将 $ 宣布为美元,因此我需要完整的文本。

在你要将输出用于VoiceOver的情况下,为什么不使用一个不包含.的除es_CL以外的本地语言呢? - Alexander
这是一个关于VoiceOver的老问题。苹果只有accessibilityLabel和AccessibilityHint,但它们只是为了更好地描述而放置的文本。请参阅苹果文档。因此,我想找到一些方法来像其他货币样式一样配置currencyPlural(如果不可能,我认为这应该是苹果的另一个错误)。 - Jorge Ampuero
请原谅我的无知:accessibilityLabel 不就是你要找的吗? - Alexander
我想将格式化字符串的结果放在“accessibilityLabel”上,但是如果我例如放置“20,000智利比索”,VoiceOver会将其宣布为“20智利比索”。因此,为了避免这种情况,我想在格式化时删除点。 - Jorge Ampuero
我正在处理其他任务,但 https://dev59.com/jrnoa4cB1Zd3GeqPLzKM#60914923 是我正在寻找的更接近的答案。当我回到这个任务时,我可能会使用那些信息添加更好的答案。 - Jorge Ampuero
显示剩余7条评论
2个回答

1
I have another suggestion, a more elegant way to solve the Voice Over situation, as I have been in the same place some time ago.
The problem with using explicitly the Locale.localizedString(forCurrencyCode:) method to translate the currency code in its name is that it disregards the value. That means the spelling for CLP 20.000 would be '20.000 Peso chileno', not '20.000 pesos chilenos'.
If you use the NumberFormatter.Style.currencyPlural, on the other hand, you can achieve the expected behavior. To guarantee it won't read as 'dollars', you can add a modifier to the locale to constrain the currency you want.
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currencyPlural
numberFormatter.locale = Locale(identifier: "es_CL@currency=CLP")

numberFormatter.string(from: 20000) // outputs 20.000 pesos chilenos

1
我想从NumberFormatter中删除一个点(“.”),因为VoiceOver在西班牙语中将20.000(点是分隔符)读作20而不是20000[...]。有什么解决办法吗? 显示您想要的格式化数字,并使VoiceOver读出您想要的内容 ⟹ 您不需要删除屏幕上的点。
问题在于,当没有numbers问题时,VoiceOver无法完美地读取货币。 解决您问题的方法可能是(仅针对VoiceOver)将区域货币附加到金额上,如下所示:append the locale currency to the amount
@IBOutlet weak var myLabel: UILabel!

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        // Your function is used to display the formatted text.
        myLabel.text = formatTest(.currencyPlural,
                                  usesGroupingSeparator: true)

        let locale = Locale(identifier: "es_CL")

        let formatter = NumberFormatter()
        formatter.locale = locale
        formatter.numberStyle = .spellOut

        // Define the a11y label to be specifically read out by VoiceOver.
        myLabel.accessibilityLabel = formatter.string(from: 20000)! + " " + locale.localizedString(forCurrencyCode: locale.currencyCode!)!
    }

即使我不懂一句西班牙语,我还是改变了设备配置来测试它,显然,它的效果符合预期。
将此代码片段适应您的环境,以便为多种语言保留格式,并为本地货币自动配置VoiceOver。

是的,这就是我要找的。您需要添加一个空格来连接字符串。是否存在使用本地化复数的方法? - Jorge Ampuero
@JorgeAmpuero:感谢您在代码片段中添加的空格。关于本地化中的复数形式,我没有找到其他方法,只能使用此处提供的方法 ⟹ 这是我为您的问题找到的最合适的解决方法。☀️ - XLE_22

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接