【问题标题】:toLocaleString thousand separator doesn't appear for Ewe below 100,000 for Firefox对于 Firefox,Ewe 低于 100,000 时不会出现 toLocaleString 千位分隔符
【发布时间】:2020-09-02 03:32:35
【问题描述】:

我使用的是 Firefox,如果我将语言环境设置为“ee”到 toLocaleString。千位分隔符不会低于 100,000。

(1111).toLocaleString("ee-gh")
"1111"
(10000).toLocaleString("ee-gh")
"10000"
(123456).toLocaleString("ee-gh")
"123,456"

console.log((1111).toLocaleString("ee-gh"));
console.log((10000).toLocaleString("ee-gh"));
console.log((123456).toLocaleString("ee-gh"));

即使将 useGrouping 设置为 true

(1234).toLocaleString("ee-gh", {useGrouping: true})
"1234"

为什么母羊会出现这种情况?如果我将语言环境设置为英语,它会正常工作

(1234).toLocaleString("en-us")
"1,234"

console.log((1234).toLocaleString("ee-gh", {useGrouping: true}));

【问题讨论】:

  • 我可以看到,即使是 Chrome 在 1,111 中也省略了千位分隔符。出于好奇,Ewe 的确切规则是什么?
  • 你应该看看 firefox 是如何实现它的——我们不能在这里合理地回答它。

标签: javascript firefox locale


【解决方案1】:

区域设置 ee 的最小分组位数设置为 3,如 CLDR survey tool 所示。 如果left side of the first grouping separator. 上至少有 3 位数字,您将获得分组分隔符。这是一个罕见的事情,ee 是 CLDR 38 中唯一具有此类值的语言环境。这适用于 Chrome 和 Firefox。

我有办法解决这个问题。利用格式化到零件。 分组分隔符是通过在第一个分组分隔符的左侧按百万、4 位格式化来接收的,因为 4 是我能找到的最小分组位数的最大值,然后它使用该符号每 3 对非分组整数进行分组数字。

function format_no_minimum_grouping_digits(value_to_format, locale, options) {
    // Create a number formatter
    const formatter = new Intl.NumberFormat(locale, options);
    const formatter_options = formatter.resolvedOptions();
    // Check if grouping is disabled 
    if (!formatter_options.useGrouping
        // The POSIX locale have grouping disabled
        || formatter_options.locale === "en-US-u-va-posix"
        // Bulgarian currency have grouping disabled
        || (new Intl.Locale(formatter_options.locale).language === "bg") && formatter_options.style === "currency") {
        // If yes format as normal
        return formatter.format(value_to_format);
    };
    // Otherwise format it to parts
    const parts = formatter.formatToParts(value_to_format);
    // Check if the grouping separator isn't applied
    const groupSym = parts.find(part => part.type === "group") === undefined ? new Intl.NumberFormat(locale, options).formatToParts(10 ** 6)[1].value : undefined;
    // If the grouping separator isn't applied, group them
    return parts.map(({type, value}) => (type === "integer" && groupSym) ? value.replace(/\B(?=(\d{3})+$)/g, groupSym) : value).join('');
}

【讨论】:

  • 我认为这不需要“解决”,但可以按预期工作:-)
  • 如果 Firefox 显示正确,那么我想说我们真正需要的是对 Chrome 的修复。否则,为什么还要打扰国际化库呢?不管怎样,提供的信息非常有用(eeewe 很难用谷歌搜索)。
猜你喜欢
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
  • 2010-10-27
相关资源
最近更新 更多