货币复数 swift 不允许配置

Currency Plural swift doesn't allow configuration

我想从 NumberFormatter 中删除一个点 ("."),因为 VoiceOver 的一个问题是将 20.000(点在西班牙语中是分隔符)读作 20 而不是 20000。

在下面的示例中,您可以测试只有 currencyPlural 是唯一的 NumberFormatter.Style 声明货币的完整字符串但忽略 usesGroupingSeparator 标志。

示例:

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) // "000"
formatTest(.currencyAccounting, usesGroupingSeparator: false) // "000"
formatTest(.currencyISOCode, usesGroupingSeparator: false) // "CLP 20000"
formatTest(.currencyPlural, usesGroupingSeparator: false) // "20.000 pesos chilenos"

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

知道如何解决这个问题吗?请不要 post 手动删除点之类的回复,因为我想使用多种语言的格式。

额外:西班牙语画外音总是宣布 $ 为美元,所以我需要完整的文本。

I want to remove a dot (".") from a NumberFormatter, because an issue with VoiceOver that read 20.000 (dot is separator in spanish) as 20 instead of 20000 [...] Any idea how to fix this?

显示你想要的格式化数字,让 VoiceOver 读出你想要的任何内容 ⟹ 你不需要删除屏幕上的点。
问题是 VoiceOver 无法完美读取货币,而 numbers.

没有问题

您的问题的解决方案可能是 将区域设置货币附加到金额(仅适用于 VoiceOver),如下所示:

@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。

我有另一个建议,一个更优雅的解决画外音问题的方法,因为我前段时间在同一个地方。

明确使用 Locale.localizedString(forCurrencyCode:) 方法翻译其名称中的货币代码的问题在于它忽略了该值,这意味着 CLP 20.000 的拼写将是“20.000 Peso chileno”,而不是“20.000”智利比索'.

如果您使用 NumberFormatter.Style.currencyPlural,另一方面,您可以实现预期的行为。为了保证它不会读作 'dollars',您可以向语言环境添加修饰符以限制您想要的货币:

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currencyPlural
numberFormatter.locale = Locale(identifier: "es_CL@currency=CLP")

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