How to use cldr - 10 common examples

To help you get started, we’ve selected a few cldr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github commercetools / merchant-center-application-kit / packages / l10n / scripts / generate-l10n-data.js View on Github external
const extractCountryDataForLocale = locale => {
  const countryNames = cldr.extractTerritoryDisplayNames(locale);
  // We only support ISO 3166-1 country code (https://en.wikipedia.org/wiki/ISO_3166-1)
  // there is alpha-2 (two-letter) , alpha-3 (three-letter) , and numeric (three-digit) representation
  // that's why we escape the non-supported representations
  // That's the lib used by the backend https://github.com/TakahikoKawasaki/nv-i18n/blob/master/src/main/java/com/neovisionaries/i18n/CountryCode.java#L2507-L2536
  const numberRegex = /^\d+$/;

  // filter out continents and invalid countries
  Object.keys(countryNames).forEach(key => {
    if (
      numberRegex.test(key) ||
      key.length > 3 ||
      excludedCountries.includes(key)
    )
      delete countryNames[key];
  });
github angular / angular / tools / gulp-tasks / cldr / extract.js View on Github external
function getPluralFunction(locale, withTypes = true) {
  let fn = cldr.extractPluralRuleFunction(locale).toString();

  if (fn === EMPTY_RULE) {
    fn = DEFAULT_RULE;
  }

  const numberType = withTypes ? ': number' : '';
  fn = fn.replace(/function anonymous\(n[^}]+{/g, `function plural(n${numberType})${numberType} {`)
           .replace(toRegExp('var'), 'let')
           .replace(toRegExp('if(typeof n==="string")n=parseInt(n,10);'), '')
           .replace(toRegExp('\n}'), ';\n}');

  // The replacement values must match the `Plural` enum from common.
  // We do not use the enum directly to avoid depending on that package.
  return fn.replace(toRegExp('"zero"'), ' 0')
      .replace(toRegExp('"one"'), ' 1')
      .replace(toRegExp('"two"'), ' 2')
github ember-intl / ember-intl / lib / extract.js View on Github external
function extractPluralRuleFunction(locale) {
    assert(isValidLocale(locale), 'Invalid locale');

    var fn = cldr.extractPluralRuleFunction(normalize(locale)).toString();

    // Remove unnecessary function name.
    fn = fn.replace('function anonymous(', 'function (');

    // ParseInt() is expensive given that we already know the input is a number.
    fn = fn.replace('if(typeof n==="string")n=parseInt(n,10);', 'n=Math.floor(n);');

    // js-hint asi.
    fn = fn.replace('"\n}', '";\n}');
    // js-hint W018 "Confusing use of '!'" caused by stuff like "!(n===11)".
    fn = fn.replace(/!\((\w+)===(\d+)\)/g, '($1!==$2)');
    // js-hint W018 "Confusing use of '!'" caused by stuff like "!(n%100===11)".
    fn = fn.replace(/!\((\w+)%(\d+)===(\d+)\)/g, '($1%$2!==$3)');

    // Keep it neat.
    fn = fn.replace(/\n/g, '');
github bobisjan / ember-format / blueprints / locale / index.js View on Github external
extractPluralRuleFunction: function(locale) {
    var fn = cldr.extractPluralRuleFunction(locale).toString();

    // Remove unnecessary function name.
    fn = fn.replace('function anonymous(', 'function (');

    // ParseInt() is expensive given that we already know the input is a number.
    fn = fn.replace('if(typeof n==="string")n=parseInt(n,10);', 'n=Math.floor(n);');

    // js-hint asi.
    fn = fn.replace('"\n}', '";\n}');
    // js-hint W018 "Confusing use of '!'" caused by stuff like "!(n===11)".
    fn = fn.replace(/!\((\w+)===(\d+)\)/g, '($1!==$2)');
    // js-hint W018 "Confusing use of '!'" caused by stuff like "!(n%100===11)".
    fn = fn.replace(/!\((\w+)%(\d+)===(\d+)\)/g, '($1%$2!==$3)');

    // Keep it neat.
    fn = fn.replace(/\n/g, '');
github bobisjan / ember-format / lib / tasks / format-extract.js View on Github external
extractPluralRuleFunction: function(locale) {
    var fn = cldr.extractPluralRuleFunction(locale).toString();

    // Remove unnecessary function name.
    fn = fn.replace('function anonymous(', 'function (');

    // ParseInt() is expensive given that we already know the input is a number.
    fn = fn.replace('if(typeof n==="string")n=parseInt(n,10);', 'n=Math.floor(n);');

    // js-hint asi.
    fn = fn.replace('"\n}', '";\n}');
    // js-hint W018 "Confusing use of '!'" caused by stuff like "!(n===11)".
    fn = fn.replace(/!\((\w+)===(\d+)\)/g, '($1!==$2)');
    // js-hint W018 "Confusing use of '!'" caused by stuff like "!(n%100===11)".
    fn = fn.replace(/!\((\w+)%(\d+)===(\d+)\)/g, '($1%$2!==$3)');

    // Keep it neat.
    fn = fn.replace(/\n/g, '');
github angular / angular / scripts / cldr / gen_plural_rules.js View on Github external
/**
 * @license
 * Copyright Google Inc. All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */

const cldr = require('cldr');
// locale list
const locales = cldr.localeIds;
const langToRule = {};
const ruleToLang = {};
const variants = [];
const localeToVariant = {};
const DEFAULT_RULE = `function anonymous(n\n/**/) {\nreturn"other"\n}`;
const EMPTY_RULE = `function anonymous(n\n/**/) {\n\n}`;

locales.forEach(locale => {
  const rule = normalizeRule(cldr.extractPluralRuleFunction(locale).toString());
  const lang = getVariantLang(locale, rule);

  if (!lang || !rule) {
    return;
  }

  if (!ruleToLang[rule]) {
github commercetools / merchant-center-application-kit / packages / l10n / scripts / generate-l10n-data.js View on Github external
const extractLanguageDataForLocale = locale => {
  // Get the list of all languages.
  const languages = cldr.extractLanguageSupplementalData(locale);
  // Get the list of all remaining languages.
  const oldLanguages = cldr.extractLanguageSupplementalMetadata(locale);
  // Get the list of all language names
  const languageNames = cldr.extractLanguageDisplayNames(locale);

  /* Here we are filtering the oldLanguages for having only the ones that
   * are not deprecated and has a key or its replacement key with length of 2
   * as stipulated by the ISO 639 (1, 2)
   *
   * Let's say that we got this coming from cldr
   *  {
   *    bh: { replacement: 'bhr', reason: 'legacy'},
   *    tr: { replacement: 'thr', reason: 'deprecated'},
   *    mar: { replacement: 'ma', reason: 'legacy'},
   *    adr: { replacement: 'adr-a', reason: 'legacy'},
   *  }
   *
   * The output with the following function would be:
   *  {
   *    bh: { replacement: 'bhr', reason: 'legacy'}, --> key of length 2
github papandreou / inter / lib / worldInfo.js View on Github external
function extractWorldInfo () {
    // Extract data
    var territoryInfoByTerritoryId = cldr.extractTerritories();
    var territoryContainmentGroups = cldr.extractTerritoryContainmentGroups();

    // Build convenience arrays
    var numericTerritoryIdByAlpha2Code = {};
    Object.keys(territoryInfoByTerritoryId).forEach(function (type) {
        var numericCode = territoryInfoByTerritoryId[type].numericCode;

        if (numericCode) {
            numericTerritoryIdByAlpha2Code[type] = numericCode;
        }
    });

    var containedTerritoriesByAlpha2TerritoryId = {};

    var parentRegionIdByTerritoryId = {};
    var numericRegionIdByTerritoryId = {};
github papandreou / inter / lib / worldInfo.js View on Github external
function extractWorldInfo () {
    // Extract data
    var territoryInfoByTerritoryId = cldr.extractTerritories();
    var territoryContainmentGroups = cldr.extractTerritoryContainmentGroups();

    // Build convenience arrays
    var numericTerritoryIdByAlpha2Code = {};
    Object.keys(territoryInfoByTerritoryId).forEach(function (type) {
        var numericCode = territoryInfoByTerritoryId[type].numericCode;

        if (numericCode) {
            numericTerritoryIdByAlpha2Code[type] = numericCode;
        }
    });

    var containedTerritoriesByAlpha2TerritoryId = {};

    var parentRegionIdByTerritoryId = {};
    var numericRegionIdByTerritoryId = {};
    var alpha2CodeByNumericTerritoryId = {};
github commercetools / merchant-center-application-kit / packages / l10n / scripts / generate-l10n-data.js View on Github external
const extractCurrencyDataForLocale = async locale => {
  // Get the list of all currencies.
  // NOTE: this list contains "old" currencies that are not in used anymore.
  const currencyInfo = cldr.extractCurrencyInfoById(locale);
  // Fetch list of currencies that are still in use, then use this list
  // to "remove" the outdated currencies from the previous list.
  const activeCurrencies = await fetch(
    'http://www.localeplanet.com/api/auto/currencymap.json'
  ).then(response => response.json());

  return Promise.resolve(
    Object.keys(activeCurrencies).reduce(
      (acc, key) =>
        // `currencyInfo` given by `cldr` may not contain any information based on the
        // currencyCode that we fetched from `currencymap.json`, so we have this definition
        // check in place.
        currencyInfo[key]
          ? Object.assign({}, acc, {
              [key]: {
                label: currencyInfo[key].displayName,