<commit_before>import { Inject } from '@angular/core';
import { Client } from './api';
import { Storage } from './storage';

export class TranslationService {
  private defaultLanguage: string;

  constructor(
    @Inject(Client) private clientService: Client,
    @Inject(Storage) private storage: Storage
  ) {
    this.defaultLanguage = 'en'; // TODO: Set to get translated names (when i18n is in place)
    this.load();
  }

  private languagesReady: Promise<any>;

  private load() {
    this.getLanguages(); // Initial caching
  }

  getLanguages(): Promise<any> {
    if (!this.languagesReady) {
      let cached = this.storage.get(`translation:languages:${this.defaultLanguage}`);

      if (cached) {
        this.languagesReady = Promise.resolve(JSON.parse(cached));
      } else {
        this.languagesReady = this.clientService.get(`api/v1/translation/languages`, { target: this.defaultLanguage })
          .then((response: any) => {
            if (!response.languages) {
              throw new Error('No languages array');
            }

            this.storage.set(`translation:languages:${this.defaultLanguage}`, JSON.stringify(response.languages));

            return response.languages;
          })
          .catch(e => [ ]);
      }
    }

    return this.languagesReady;
  }

  purgeLanguagesCache() {
    this.languagesReady = void 0;
    this.storage.set(`translation:languages:${this.defaultLanguage}`, '');
  }

  getLanguageName(query: string): Promise<string> {
    if (!query) {
      return Promise.resolve('None');
    }

    return this.getLanguages()
      .then((languages: any[]) => {<commit_msg>(fix): ignore empty languages cache<commit_after>import { Inject } from '@angular/core';
import { Client } from './api';
import { Storage } from './storage';

export class TranslationService {
  private defaultLanguage: string;

  constructor(
    @Inject(Client) private clientService: Client,
    @Inject(Storage) private storage: Storage
  ) {
    this.defaultLanguage = 'en'; // TODO: Set to get translated names (when i18n is in place)
    this.load();
  }

  private languagesReady: Promise<any>;

  private load() {
    this.getLanguages(); // Initial caching
  }

  getLanguages(): Promise<any> {
    if (!this.languagesReady) {
      let cached = this.storage.get(`translation:languages:${this.defaultLanguage}`);

      if (cached) {
        cached = JSON.parse(cached);
      }

      if (cached && cached.length > 0) {
        this.languagesReady = Promise.resolve(cached);
      } else {
        this.languagesReady = this.clientService.get(`api/v1/translation/languages`, { target: this.defaultLanguage })
          .then((response: any) => {
            if (!response.languages) {
              throw new Error('No languages array');
            }

            this.storage.set(`translation:languages:${this.defaultLanguage}`, JSON.stringify(response.languages));

            return response.languages;
          })
          .catch(e => [ ]);
      }
    }

    return this.languagesReady;
  }

  purgeLanguagesCache() {
    this.languagesReady = void 0;
    this.storage.set(`translation:languages:${this.defaultLanguage}`, '');
  }

  getLanguageName(query: string): Promise<string> {
    if (!query) {
      return Promise.resolve('None');
    }

    return this.getLanguages()
      .then((languages: any[]) => {
{'fraction_non_alphanumeric': 0.14099139210448206, 'fraction_numerical': 0.0026714158504007124, 'mean_word_length': 2.8735632183908044, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 16, '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': '4308895', 'n_tokens_mistral': 980, 'n_tokens_neox': 953, 'n_words': 204}