function loadLocale(lang, translationRequired) {
    clear();
    gReadyState = 'loading';
    gLanguage = lang;

    var untranslatedElements = [];

    // if there is an inline / pre-compiled dictionary,
    // the current HTML document can be translated right now
    var inlineDict = getL10nDictionary(lang);
    if (inlineDict) {
      gL10nData = inlineDict;
      if (translationRequired) {
        untranslatedElements = translateFragment();
      }
    }

    // translate the document if required and fire a `localized' event
    function finish() {
      if (translationRequired) {
        if (!inlineDict) {
          // no inline dictionary has been used: translate the whole document
          untranslatedElements = translateFragment();
        } else if (untranslatedElements.length) {
          // the document should have been already translated but the inline
          // dictionary didn't include all necessary l10n keys:
          // try to translate all remaining elements now
          untranslatedElements = translateElements(untranslatedElements);
        }
      }
      // tell the rest of the world we're done
      // -- note that `gReadyState' must be set before the `localized' event is
      //    fired for `localizeElement()' to work as expected
      gReadyState = 'complete';
      fireL10nReadyEvent(lang);
      consoleWarn_missingKeys(untranslatedElements, lang);
    }

    // l10n resource loader
    function l10nResourceLink(link) {
      /**
       * l10n resource links can use the following syntax for href:
       * <link type="application/l10n" href="resources/{{locale}}.json" />
       * -- in which case, {{locale}} will be replaced by `navigator.language'.
       */
      var re = /\{\{\s*locale\s*\}\}/;

      var parse = function(locale, onload, onerror) {
        var href = unescape(link.href).replace(re, locale);
        parseResource(href, locale, onload, function notFound() {
          consoleWarn(href + ' not found.');
          onerror();
        });
      };

      this.load = function(locale, onload, onerror) {
        onerror = onerror || function() {};
        parse(locale, onload, function parseFallbackLocale() {
          /**
           * For links like <link href="resources/{{locale}}.json" />,
           * there's no way to know if the resource file matching the current
           * language has been found... before trying to fetch it with XHR
           * => if something went wrong, try the default locale as fallback.
           */
          if (re.test(unescape(link.href)) && gDefaultLocale != locale) {
            consoleLog('Trying the fallback locale: ' + gDefaultLocale);
            parse(gDefaultLocale, onload, onerror);
          } else {
            onerror();
          }
        });
      };
    }

    // check all <link type="application/l10n" href="..." /> nodes
    // and load the resource files
    var resourceLinks = getL10nResourceLinks();
    var resourceCount = resourceLinks.length;
    if (!resourceCount) {
      consoleLog('no resource to load, early way out');
      translationRequired = false;
      finish();
    } else {
      var onResourceCallback = function() {
        if (--resourceCount <= 0) { // <=> all resources have been XHR'ed
          finish();
        }
      };
      for (var i = 0, l = resourceCount; i < l; i++) {
        var resource = new l10nResourceLink(resourceLinks[i]);
        resource.load(lang, onResourceCallback, onResourceCallback);
      }
    }
  }
// load and parse all resources for the specified locale
load and parse all resources for the specified locale
load and parse all resources for the specified locale
function loadLocale(lang, translationRequired) {
    clear();
    gReadyState = 'loading';
    gLanguage = lang;
    var untranslatedElements = [];
    var inlineDict = getL10nDictionary(lang);
    if (inlineDict) {
      gL10nData = inlineDict;
      if (translationRequired) {
        untranslatedElements = translateFragment();
      }
    }
    function finish() {
      if (translationRequired) {
        if (!inlineDict) {
          untranslatedElements = translateFragment();
        } else if (untranslatedElements.length) {
          untranslatedElements = translateElements(untranslatedElements);
        }
      }
      gReadyState = 'complete';
      fireL10nReadyEvent(lang);
      consoleWarn_missingKeys(untranslatedElements, lang);
    }
    function l10nResourceLink(link) {
      var re = /\{\{\s*locale\s*\}\}/;
      var parse = function(locale, onload, onerror) {
        var href = unescape(link.href).replace(re, locale);
        parseResource(href, locale, onload, function notFound() {
          consoleWarn(href + ' not found.');
          onerror();
        });
      };
      this.load = function(locale, onload, onerror) {
        onerror = onerror || function() {};
        parse(locale, onload, function parseFallbackLocale() {
          if (re.test(unescape(link.href)) && gDefaultLocale != locale) {
            consoleLog('Trying the fallback locale: ' + gDefaultLocale);
            parse(gDefaultLocale, onload, onerror);
          } else {
            onerror();
          }
        });
      };
    }
    var resourceLinks = getL10nResourceLinks();
    var resourceCount = resourceLinks.length;
    if (!resourceCount) {
      consoleLog('no resource to load, early way out');
      translationRequired = false;
      finish();
    } else {
      var onResourceCallback = function() {
        if (--resourceCount <= 0) { 
          finish();
        }
      };
      for (var i = 0, l = resourceCount; i < l; i++) {
        var resource = new l10nResourceLink(resourceLinks[i]);
        resource.load(lang, onResourceCallback, onResourceCallback);
      }
    }
  }
function loadLocale ( lang , translationRequired ) { clear ( ) ; gReadyState = 'loading' ; gLanguage = lang ; var untranslatedElements = [ ] ; var inlineDict = getL10nDictionary ( lang ) ; if ( inlineDict ) { gL10nData = inlineDict ; if ( translationRequired ) { untranslatedElements = translateFragment ( ) ; } } function finish ( ) { if ( translationRequired ) { if ( ! inlineDict ) { untranslatedElements = translateFragment ( ) ; } else if ( untranslatedElements . length ) { untranslatedElements = translateElements ( untranslatedElements ) ; } } gReadyState = 'complete' ; fireL10nReadyEvent ( lang ) ; consoleWarn_missingKeys ( untranslatedElements , lang ) ; } function l10nResourceLink ( link ) { var re = / \{\{\s*locale\s*\}\} / ; var parse = function ( locale , onload , onerror ) { var href = unescape ( link . href ) . replace ( re , locale ) ; parseResource ( href , locale , onload , function notFound ( ) { consoleWarn ( href + ' not found.' ) ; onerror ( ) ; } ) ; } ; this . load = function ( locale , onload , onerror ) { onerror = onerror || function ( ) { } ; parse ( locale , onload , function parseFallbackLocale ( ) { if ( re . test ( unescape ( link . href ) ) && gDefaultLocale != locale ) { consoleLog ( 'Trying the fallback locale: ' + gDefaultLocale ) ; parse ( gDefaultLocale , onload , onerror ) ; } else { onerror ( ) ; } } ) ; } ; } var resourceLinks = getL10nResourceLinks ( ) ; var resourceCount = resourceLinks . length ; if ( ! resourceCount ) { consoleLog ( 'no resource to load, early way out' ) ; translationRequired = false ; finish ( ) ; } else { var onResourceCallback = function ( ) { if ( -- resourceCount <= 0 ) { finish ( ) ; } } ; for ( var i = 0 , l = resourceCount ; i < l ; i ++ ) { var resource = new l10nResourceLink ( resourceLinks [ i ] ) ; resource . load ( lang , onResourceCallback , onResourceCallback ) ; } } }
load and parse all resources for the specified locale
load and parse all resources for the specified locale
// if there is an inline / pre-compiled dictionary, // the current HTML document can be translated right now // translate the document if required and fire a `localized' event // no inline dictionary has been used: translate the whole document // the document should have been already translated but the inline // dictionary didn't include all necessary l10n keys: // try to translate all remaining elements now // tell the rest of the world we're done // -- note that `gReadyState' must be set before the `localized' event is //    fired for `localizeElement()' to work as expected // l10n resource loader /**
       * l10n resource links can use the following syntax for href:
       * <link type="application/l10n" href="resources/{{locale}}.json" />
       * -- in which case, {{locale}} will be replaced by `navigator.language'.
       */ /**
           * For links like <link href="resources/{{locale}}.json" />,
           * there's no way to know if the resource file matching the current
           * language has been found... before trying to fetch it with XHR
           * => if something went wrong, try the default locale as fallback.
           */ // check all <link type="application/l10n" href="..." /> nodes // and load the resource files // <=> all resources have been XHR'ed