我想使用类似aurelia.io documentation中的CurrencyValueConverter,但将结果本地化为荷兰语或德语,但我不知道如何使所有语言都可用于数字
我能够导入Ducth语言环境并明确加载它,如下所示:
import numeral from 'numeral';
import nl from "numeral/languages/nl-nl";
export class CurrencyValueConverter {
toView(value, language = "nl-nl") {
numeral.language(language, nl); // this line loads the nl language definition
numeral.language(language);
return numeral(value).format("$0,0.00");
}
}
但当然,这只适用于一种语言。 如何在避免类似
之类的情况下加载多种语言if(language === "nl-nl")
numeral.language(language, nl);
else if(language === "de-de")
// etc
答案 0 :(得分:4)
Here's an example: https://gist.run?id=6af6cf41d4f8dc206aaa
<强> app.html 强>
<template>
<require from="./currency-value-converter"></require>
<label>
Language:
<select value.bind="selectedLanguage">
<option repeat.for="language of languages" value.bind="language">${language}</option>
</select>
</label>
<h1>${value | currency:selectedLanguage}</h1>
</template>
<强> app.js 强>
export class App {
value = 1234567.890123;
selectedLanguage = 'nl-nl';
languages = [
'en-gb',
'es',
'et',
'fi',
'fr',
'fr-CA',
'fr-ch',
'hu',
'it',
'ja',
'nl-nl',
'pl',
'pt-br',
'pt-pt',
'ru',
'ru-UA',
'sk',
'th',
'tr',
'uk-UA',
'be-nl',
'chs',
'cs',
'da-dk',
'de',
'de-ch'];
}
<强>货币值-converter.js 强>
import numeral from 'numeral';
import 'numeral/min/languages.min';
export class CurrencyValueConverter {
toView(value, language = 'nl-nl') {
numeral.language(language);
return numeral(value).format('$0,0.00');
}
}