Javascript 是否可以列出所有已知的本地化货币?

Javascript 是否可以列出所有已知的本地化货币?,javascript,browser,intl,Javascript,Browser,Intl,从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 €" 但是有可能列出所有可用的货币吗?这个问题的重点是构建一个

从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(“货币”);
const inputEl=document.getElementById('number');
const outputEl=document.getElementById('output');
//已从应用程序提供区域设置
const locales=['fr-CA','en-US','de-de'];
//要求:在此处检索所有已知货币
常量货币=[‘加元’、‘美元’、‘欧元’];
locales.forEach(locale=>{
const optionEl=document.createElement('option');
optionEl.value=语言环境;
//可选检索区域设置名称(本地化)
optionEl.text=语言环境;
localeEl.appendChild(optionEl);
});
货币。forEach(货币=>{
const optionEl=document.createElement('option');
optionEl.value=货币;
//可选:在此处检索本地化货币名称
optionEl.text=货币;
选择附加子项(可选项);
});
常量updateOutput=()=>{
const locale=localeEl.children[localeEl.selectedIndex].value;
const currency=selectEl.children[selectEl.selectedIndex].value;
常量编号=输入值;
outputEl.innerHTML=新的Intl.NumberFormat(区域设置,{style:'currency',currency:currency});
}
localeEl.addEventListener('change',updateOutput);
选择el.addEventListener('change',updateOutput);
inputEl.addEventListener('change',updateOutput)

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

然而,目前的解决方案并不十分优雅或高效:

函数getSupportedCurrences(){ 函数$(金额、货币){ 让locale='en-US'; 让选项={ 风格:“货币”, 货币:货币,, 当前显示:“名称” }; 返回Intl.NumberFormat(区域设置、选项).format(金额); } 常量getAllPossibleThreeLetterWords=()=>{ 常量字符='ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 常数arr=[]; 让text=''; for(设i=0;i常数rx=/(?不知道为什么你不能把它们全部包括进来,并根据你的上述做法进行引用。我想我不明白你说的本地化是什么意思。你是指当前地理位置的本地,还是像将它们全部嵌入javascript代码一样的本地?本地化为法语,美元货币是
$US
,而e在英语中只是
$
。同样地,法语中的
CAD
$CA
,英语中的
CA$
。问题是,用户必须安装区域设置才能查看货币,因此使用该应用的用户将看到日元货币(因为它有区域设置),而其他用户则不会(因为他们没有区域设置)。这不是更好的解决方案:/