<commit_before>'use strict';

var Context = require('./context').Context;

var DEBUG = false;
var isPretranslated = false;
var rtlList = ['ar', 'he', 'fa', 'ps', 'ur'];

var ctx = new Context();
ctx.ready(onReady);

if (DEBUG) {
  ctx.addEventListener('error', logMessage.bind(null, 'error'));
  ctx.addEventListener('warning', logMessage.bind(null, 'warn'));
}


// Public API

navigator.mozL10n = {
  translate: translateFragment,
  localize: localizeElement,
  get: ctx.get.bind(ctx),
  ready: ctx.ready.bind(ctx),
  get readyState() {
    return ctx.isReady ? 'complete' : 'loading';
  },
  language: {
    set code(lang) {
      initLocale(lang, true);
    },
    get code() {
      return ctx.supportedLocales[0];
    },
    get direction() {
      return getDirection(ctx.supportedLocales[0]);
    }
  }
};

function getDirection(lang) {
  return (rtlList.indexOf(lang) >= 0) ? 'rtl' : 'ltr';
}

var readyStates = {
  'loading': 0,
  'interactive': 1,
  'complete': 2
};

function waitFor(state, callback) {
  state = readyStates[state];
  if (readyStates[document.readyState] >= state) {
    callback();
    return;
  }
  document.addEventListener('readystatechange', function l10n_onrsc() {
    if (readyStates[document.readyState] >= state) {
      document.removeEventListener('readystatechange', l10n_onrsc);
      callback();
    }
  });
}

if (window.document) {
  isPretranslated = (document.documentElement.lang === navigator.language);

  if (isPretranslated) {
    waitFor('complete', function() {
      window.setTimeout(initDocumentLocalization.bind(null, initLocale));
    });
  } else {
    if (document.readyState === 'complete') {
      window.setTimeout(initDocumentLocalization.bind(null, initLocale));
    } else {
      waitFor('interactive', pretranslate);
    }
  }


}

function pretranslate() {
  if (inlineLocalization()) {
    waitFor('interactive', function() {
      window.setTimeout(initDocumentLocalization.bind(null, initLocale));
    });
  } else {
    initDocumentLocalization(initLocale);
  }
}

function inlineLocalization() {
  var lang = navigator.language;
  var script = document.documentElement.querySelector('script[type="application/l10n"][lang="' +
      lang + '"]');
  if (!script) {
    return false;
  }
  var locale = ctx.getLocale(lang);
  // the inline localization is happenning very early, when the ctx is not
  // yet ready and when the resources haven't been downloaded yet;  add the
  // inlined JSON directly to the current locale
  locale.addAST(JSON.parse(script.innerHTML));
  // record the fact that the locale already has the inlined strings
  locale.isPartial = true;
  // localize the visible DOM
  translateFragment(null, locale);
  // the visible DOM is now pretranslated
  isPretranslated = true;
  return true;
}

function initDocumentLocalization(cb) {
  var resLinks = document.head.querySelectorAll('link[type="application/l10n"]');
  var iniLinks = [];
  var link;

  for (link of resLinks) {
    var url = link.getAttribute('href');
    var type = url.substr(url.lastIndexOf('.') + 1);
    if (type === 'ini') {
      iniLinks.push(url);
    }
    ctx.resLinks.push(url);
  }

  var iniLoads = iniLinks.length;
  if (iniLoads === 0) {
    onIniLoaded();
  }

  function onIniLoaded() {
    iniLoads--;
    if (iniLoads <= 0) {
      cb();
    }
  }

  for (link of iniLinks) {<commit_msg>remove cb from inlineLocalization<commit_after>'use strict';

var Context = require('./context').Context;

var DEBUG = false;
var isPretranslated = false;
var rtlList = ['ar', 'he', 'fa', 'ps', 'ur'];

var ctx = new Context();
ctx.ready(onReady);

if (DEBUG) {
  ctx.addEventListener('error', logMessage.bind(null, 'error'));
  ctx.addEventListener('warning', logMessage.bind(null, 'warn'));
}


// Public API

navigator.mozL10n = {
  translate: translateFragment,
  localize: localizeElement,
  get: ctx.get.bind(ctx),
  ready: ctx.ready.bind(ctx),
  get readyState() {
    return ctx.isReady ? 'complete' : 'loading';
  },
  language: {
    set code(lang) {
      initLocale(lang, true);
    },
    get code() {
      return ctx.supportedLocales[0];
    },
    get direction() {
      return getDirection(ctx.supportedLocales[0]);
    }
  }
};

function getDirection(lang) {
  return (rtlList.indexOf(lang) >= 0) ? 'rtl' : 'ltr';
}

var readyStates = {
  'loading': 0,
  'interactive': 1,
  'complete': 2
};

function waitFor(state, callback) {
  state = readyStates[state];
  if (readyStates[document.readyState] >= state) {
    callback();
    return;
  }
  document.addEventListener('readystatechange', function l10n_onrsc() {
    if (readyStates[document.readyState] >= state) {
      document.removeEventListener('readystatechange', l10n_onrsc);
      callback();
    }
  });
}

if (window.document) {
  isPretranslated = (document.documentElement.lang === navigator.language);

  if (isPretranslated) {
    waitFor('complete', function() {
      window.setTimeout(initDocumentLocalization);
    });
  } else {
    if (document.readyState === 'complete') {
      window.setTimeout(initDocumentLocalization);
    } else {
      waitFor('interactive', pretranslate);
    }
  }


}

function pretranslate() {
  if (inlineLocalization()) {
    waitFor('interactive', function() {
      window.setTimeout(initDocumentLocalization);
    });
  } else {
    initDocumentLocalization();
  }
}

function inlineLocalization() {
  var lang = navigator.language;
  var script = document.documentElement.querySelector('script[type="application/l10n"][lang="' +
      lang + '"]');
  if (!script) {
    return false;
  }
  var locale = ctx.getLocale(lang);
  // the inline localization is happenning very early, when the ctx is not
  // yet ready and when the resources haven't been downloaded yet;  add the
  // inlined JSON directly to the current locale
  locale.addAST(JSON.parse(script.innerHTML));
  // record the fact that the locale already has the inlined strings
  locale.isPartial = true;
  // localize the visible DOM
  translateFragment(null, locale);
  // the visible DOM is now pretranslated
  isPretranslated = true;
  return true;
}

function initDocumentLocalization() {
  var resLinks = document.head.querySelectorAll('link[type="application/l10n"]');
  var iniLinks = [];
  var link;

  for (link of resLinks) {
    var url = link.getAttribute('href');
    var type = url.substr(url.lastIndexOf('.') + 1);
    if (type === 'ini') {
      iniLinks.push(url);
    }
    ctx.resLinks.push(url);
  }

  var iniLoads = iniLinks.length;
  if (iniLoads === 0) {
    initLocale();
    return;
  }

  function onIniLoaded() {
    iniLoads--;
    if (iniLoads <= 0) {
      initLocale();
    }
  }

  for (link of iniLinks) {
{'fraction_non_alphanumeric': 0.14590800477897253, 'fraction_numerical': 0.005675029868578256, 'mean_word_length': 3.5588835942818244, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3480749', 'n_tokens_mistral': 2175, 'n_tokens_neox': 2106, 'n_words': 491}