# All files / src/app locale.helper.ts

46.15% Statements 24/52
15.38% Branches 2/13
83.33% Functions 5/6
45.1% Lines 23/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    9991x
    91x
    91x
    91x
    91x
    91x
    91x
    91x
    91x
    91x

    91x
    91x
    103x

    103x

    91x
    104x

    91x
    1x

    91x
    4x

    4x

    91x

    91x
     import locale_cs from '@angular/common/locales/cs';
    import locale_de from '@angular/common/locales/de';
    import locale_en from '@angular/common/locales/en';
    import locale_es from '@angular/common/locales/es';
    import locale_fr from '@angular/common/locales/fr';
    import locale_id from '@angular/common/locales/id';
    import locale_pl from '@angular/common/locales/pl';
    import locale_pt_PT from '@angular/common/locales/pt-PT';
    import locale_zh_Hans from '@angular/common/locales/zh-Hans';
    import { LOCALE_ID, TRANSLATIONS, TRANSLATIONS_FORMAT } from '@angular/core';

    declare const require;

    export class LocaleHelper {
      static getBrowserLang(): string {
        const lang = navigator.language;

        if (lang.includes('cs')) {
          return 'cs';
        } else if (lang.includes('de')) {
          return 'de-DE';
        } else if (lang.includes('en')) {
          return 'en-US';
        } else if (lang.includes('es')) {
          return 'es-ES';
        } else if (lang.includes('fr')) {
          return 'fr-FR';
        } else if (lang.includes('id')) {
          return 'id-ID';
        } else if (lang.includes('pl')) {
          return 'pl-PL';
        } else if (lang.includes('pt')) {
          return 'pt-PT';
        } else if (lang.includes('zh')) {
          return 'zh-CN';
        } else {
          return undefined;
        }
      }

      static getLocale(): string {
        return window.localStorage.getItem('lang') || this.getBrowserLang() || 'en-US';
      }

      static setLocale(lang: string) {
        window.localStorage.setItem('lang', lang);
      }

      static getLocaleData() {
        let localeData = locale_en;
        switch (this.getLocale()) {
          case 'cs':
            localeData = locale_cs;
            break;
          case 'de-DE':
            localeData = locale_de;
            break;
          case 'es-ES':
            localeData = locale_es;
            break;
          case 'fr-FR':
            localeData = locale_fr;
            break;
          case 'id-ID':
            localeData = locale_id;
            break;
          case 'pt-PT':
            localeData = locale_pt_PT;
            break;
          case 'pl-PL':
            localeData = locale_pl;
            break;
          case 'zh-CN':
            localeData = locale_zh_Hans;
            break;
        }
        return localeData;
      }
    }

    const i18nProviders = [
      { provide: LOCALE_ID, useValue: LocaleHelper.getLocale() },
      {
        provide: TRANSLATIONS,
        useFactory: (locale) => {
          locale = locale || 'en-US';
          try {
            return require(`raw-loader!locale/messages.${locale}.xlf`);
          } catch (error) {
            return [];
          }
        },
        deps: [LOCALE_ID]
      },
      { provide: TRANSLATIONS_FORMAT, useValue: 'xlf' }
    ];

    export { i18nProviders };