<commit_before>// @ngInject
export default class LanguageUtilsService {
  // @ngInject
  constructor($translate, $http, ENV) {
    this.$translate = $translate;
    this.$http = $http;
    this.ENV = ENV;
  }

  getCurrentLanguage() {
    return this.current;
  }

  setCurrentLanguage(language) {
    this.current = language;
    this.$translate.use(language.code);
    moment.locale(language.code);
    this.$http.defaults.headers.common['Accept-Language'] = language.code;
  }

  checkLanguage() {
    // Check if current language is listed in choices and
    // switch to default language if current choice is invalid.
    const key = this.$translate.storageKey();
    const storage = this.$translate.storage();
    const code = storage.get(key);
    const current = this.findLanguageByCode(code) || this.findLanguageByCode(this.ENV.defaultLanguage);
    this.setCurrentLanguage(current);
  }

  findLanguageByCode(code) {
    if (!code) {
      return;
    }
    return this.getChoices().filter(language => language.code === code)[0];
  }

  getChoices() {
    return this.ENV.languageChoices;
  }
}<commit_msg>Fallback to first option in languageChoices list if defaultLanguage is invalid [WAL-1721]<commit_after>// @ngInject
export default class LanguageUtilsService {
  // @ngInject
  constructor($translate, $http, ENV) {
    this.$translate = $translate;
    this.$http = $http;
    this.ENV = ENV;
  }

  getCurrentLanguage() {
    return this.current;
  }

  setCurrentLanguage(language) {
    this.current = language;
    this.$translate.use(language.code);
    moment.locale(language.code);
    this.$http.defaults.headers.common['Accept-Language'] = language.code;
  }

  checkLanguage() {
    // Check if current language is listed in choices and
    // switch to default language if current choice is invalid.
    // Fallback to first option in languageChoices list if defaultLanguage is invalid.
    const key = this.$translate.storageKey();
    const storage = this.$translate.storage();
    const code = storage.get(key);
    const current = (
      this.findLanguageByCode(code)
        || this.findLanguageByCode(this.ENV.defaultLanguage)
        || this.getChoices()[0]
    );
    this.setCurrentLanguage(current);
  }

  findLanguageByCode(code) {
    if (!code) {
      return;
    }
    return this.getChoices().filter(language => language.code === code)[0];
  }

  getChoices() {
    return this.ENV.languageChoices;
  }
}
{'fraction_non_alphanumeric': 0.1253572886892609, 'fraction_numerical': 0.0028583095140873828, 'mean_word_length': 3.305799648506151, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7054603', 'n_tokens_mistral': 734, 'n_tokens_neox': 715, 'n_words': 163}