导入所有语言的数字.js在es2016中为Aurelia pp

时间:2016-02-22 14:28:28

标签: javascript ecmascript-6 aurelia es6-module-loader numeral.js

我想使用类似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

1 个答案:

答案 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');
  }
}