lib/LanguagePicker.js
function LanguagePicker(lang = 'en', config = {}) {
  const scripts = ls.adjust({ override: dataOverrides });

  this.userLang = lang;
  this.nameTag = config.nameTag;
  // The prefix is either given or is the nameTag
  // with an underscore
  // See Babel.js#24
  this.prefix = config.multiTag || (this.nameTag && `${this.nameTag}_`) || '';
  this.forceLocal = !!config.forceLocal;

  if (this.forceLocal) {
    // If we are forcing a local language, we don't need
    // any of the fallback calculations
    return;
  }
  // Store language script
  this.langScript = scripts[lang] || 'Latn';

  // Add known fallbacks for the language
  let fallbacks;
  if (config.languageMap) {
    fallbacks = config.languageMap[lang];
    if (fallbacks && !Array.isArray(fallbacks)) {
      fallbacks = [fallbacks];
    }
  }
  if (!fallbacks) {
    fallbacks = [];
  }

  // Use the given language as first choice
  fallbacks = [lang].concat(fallbacks);

  // Remove duplicates
  fallbacks = fallbacks.filter((item, i) => fallbacks.indexOf(item) === i);

  // Add prefix to all languages if exists
  // eslint-disable-next-line arrow-body-style
  fallbacks = fallbacks.map((code) => {
    return code === this.nameTag ? code : this.prefix + code;
  });

  // Store initial fallbacks
  this.fallbacks = fallbacks;

  this.prefixedEnglish = this.prefix ? `${this.prefix}en` : 'en';
  this.prefixedLangScript = `-${this.langScript}`;
}
function LanguagePicker(lang = 'en', config = {}) {
  const scripts = ls.adjust({ override: dataOverrides });

  this.userLang = lang;
  this.nameTag = config.nameTag;
  // The prefix is either given or is the nameTag
  // with an underscore
  // See Babel.js#24
  this.prefix = config.multiTag || (this.nameTag && `${this.nameTag}_`) || '';
  this.forceLocal = !!config.forceLocal;

  if (this.forceLocal) {
    // If we are forcing a local language, we don't need
    // any of the fallback calculations
    return;
  }
  // Store language script
  this.langScript = scripts[lang] || 'Latn';

  // Add known fallbacks for the language
  let fallbacks;
  if (config.languageMap) {
    fallbacks = config.languageMap[lang];
    if (fallbacks && !Array.isArray(fallbacks)) {
      fallbacks = [fallbacks];
    }
  }
  if (!fallbacks) {
    fallbacks = [];
  }

  // Use the given language as first choice
  fallbacks = [lang].concat(fallbacks);

  // Remove duplicates
  fallbacks = fallbacks.filter((item, i) => fallbacks.indexOf(item) === i);

  // Add prefix to all languages if exists
  // eslint-disable-next-line arrow-body-style
  fallbacks = fallbacks.map((code) => {
    return code === this.nameTag ? code : this.prefix + code;
  });

  // Store initial fallbacks
  this.fallbacks = fallbacks;

  this.prefixedEnglish = this.prefix ? `${this.prefix}en` : 'en';
  this.prefixedLangScript = `-${this.langScript}`;
}
function LanguagePicker ( lang = 'en' , config = { } ) { const scripts = ls . adjust ( { override : dataOverrides } ) ; this . userLang = lang ; this . nameTag = config . nameTag ; // The prefix is either given or is the nameTag // with an underscore // See Babel.js#24 this . prefix = config . multiTag || ( this . nameTag && ` ${ this . nameTag } ` ) || '' ; this . forceLocal = ! ! config . forceLocal ; if ( this . forceLocal ) { // If we are forcing a local language, we don't need // any of the fallback calculations return ; } // Store language script this . langScript = scripts [ lang ] || 'Latn' ; // Add known fallbacks for the language let fallbacks ; if ( config . languageMap ) { fallbacks = config . languageMap [ lang ] ; if ( fallbacks && ! Array . isArray ( fallbacks ) ) { fallbacks = [ fallbacks ] ; } } if ( ! fallbacks ) { fallbacks = [ ] ; } // Use the given language as first choice fallbacks = [ lang ] . concat ( fallbacks ) ; // Remove duplicates fallbacks = fallbacks . filter ( ( item , i ) => fallbacks . indexOf ( item ) === i ) ; // Add prefix to all languages if exists // eslint-disable-next-line arrow-body-style fallbacks = fallbacks . map ( ( code ) => { return code === this . nameTag ? code : this . prefix + code ; } ) ; // Store initial fallbacks this . fallbacks = fallbacks ; this . prefixedEnglish = this . prefix ? ` ${ this . prefix } ` : 'en' ; this . prefixedLangScript = ` ${ this . langScript } ` ; }
Define a language picker with fallback rules.

@param {String} [lang='en'] Requested language.
@param {Object} [config] Optional configuration object
@cfg {string} [nameTag] A tag that defines the local value in a label.
If specified, will also be used as the prefix for other languages if
a prefix is not already specified with `multiTag`.
@cfg {string} [multiTag] A specified prefix for the language keys
@cfg {Object} [languageMap] An object representing language fallbacks; languages
may have more than one fallback, represented in a string or an array of strings.
Example:
{
'langA': [ 'lang1', 'lang2' ]
'langB': 'lang3'
}
@cfg {boolean} [forceLocal] Force the system to fetch a local representation
of the labels, if it exists. This will only work if there is a nameTag specified,
since that tag dictates the key of the local value. If the value doesn't exist
or if the nameTag is not specified, the system will return the first value given.
Note: All fallbacks are skipped if this parameter is truthy!
@constructor
Define a language picker with fallback rules .