【问题标题】:Is it possible to list all known localized currencies?是否可以列出所有已知的本地化货币?
【发布时间】:2020-03-29 17:25:16
【问题描述】:

从 MDN 文档中,我可以使用

格式化货币值
var locale = 'fr-CA';
var number = 123456.789;
var currency = 'EUR';

console.log(new Intl.NumberFormat(locale, { style: 'currency', currency: currency }).format(number));
// expected output: "123.456,79 €"

但是是否可以列出所有可用的本地货币?这个问题的重点是构建一个本地化的货币选择器,而不需要从第三方 URL 或模块中获取数据。

例如:

const localeEl = document.getElementById('locales');
const selectEl = document.getElementById('currencies');
const inputEl = document.getElementById('number');
const outputEl = document.getElementById('output');

// locales already provided from app
const locales = ['fr-CA', 'en-US', 'de-DE'];

// REQUIREMENT: retrieve all known currencies here
const currencies = ['CAD', 'USD', 'EUR'];

locales.forEach(locale => {
   const optionEl = document.createElement('option');
   optionEl.value = locale;
   
   // OPTIONAL retrieve locale name (localized)
   optionEl.text = locale;
   
   localeEl.appendChild(optionEl);
});

currencies.forEach(currency => {
   const optionEl = document.createElement('option');
   optionEl.value = currency;
   
   // OPTIONAL retrieve localized currency name here
   optionEl.text = currency;
   
   selectEl.appendChild(optionEl);
});

const updateOutput = () => {
  const locale = localeEl.children[localeEl.selectedIndex].value;
  const currency = selectEl.children[selectEl.selectedIndex].value;
  const number = inputEl.value;

  outputEl.innerHTML = new Intl.NumberFormat(locale, { style: 'currency', currency: currency }).format(number);
}

localeEl.addEventListener('change', updateOutput);
selectEl.addEventListener('change', updateOutput);
inputEl.addEventListener('change', updateOutput);
<select id="locales"></select>
<select id="currencies"></select>
<input type="number" id="number" />
<div id="output"></div>

【问题讨论】:

  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 不知道为什么你不能把它们都包括在内,并根据你上面的做法进行参考。当您说本地化时,我想我不明白您的意思。您是指当前地理位置的本地,还是将它们全部嵌入到 javascript 代码中的本地?
  • 本地化为法语,美元货币为$US,而在英语中则为$。同样,法语中的CAD$CA,英语中的CA$

标签: javascript browser intl


【解决方案1】:

确实可以列出用户浏览器支持的所有货币,而无需从第三方资源获取数据。

但是,目前,该解决方案不太优雅或高效:

function getSupportedCurrencies() {
  function $(amount, currency) {
    let locale = 'en-US';
    let options = {
      style: 'currency',
      currency: currency,
      currencyDisplay: "name"
    };
    return Intl.NumberFormat(locale, options).format(amount);
  }
  const getAllPossibleThreeLetterWords = () => {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    const arr = [];
    let text = '';
    for (let i = 0; i < chars.length; i++) {
      for (let x = 0; x < chars.length; x++) {
        for (let j = 0; j < chars.length; j++) {
          text += chars[i];
          text += chars[x];
          text += chars[j];
          arr.push(text);
          text = '';
        }
      }
    }
    return arr;
  };
  let ary = getAllPossibleThreeLetterWords();
  let currencies = [];
  const rx = /(?<= ).+/; // This line doesn't work in Firefox versions older than version 78 due to bug 1225665: https://bugzilla.mozilla.org/show_bug.cgi?id=1225665
  ary.forEach((cur) => {
    let output = $(0, cur).trim();
    if (output.replace(/^[^ ]+ /, '') !== cur) {
      let obj = {};
      obj.code = cur;
      obj.name = output.match(rx)[0];
      currencies.push(obj);
    }
  });
  return currencies;
}
console.log(getSupportedCurrencies());

根据我上面的概念证明,Polywhirl 先生创建了一个 memory optimized version,它使用 product iterable,而不是将所有这 3 个字母猜测预先加载到一个数组中。

非常令人失望的是,规范没有提供一种以更直接(非暴力猜测)的方式完成此任务的方法。

理想情况下,规范应该演变为标准化这种类型的环境反射。规范应该提供一个getSupportedCurrencies 函数作为标准 API 的一部分,这样程序员就不必求助于低效的基于猜测的解决方案。

【讨论】:

  • 这个问题是用户必须安装语言环境才能查看货币,所以使用该应用程序的用户会看到日元货币(因为它有语言环境),但另一个用户不会(因为他们没有语言环境)。这不是一个可取的解决方案。 ://
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多