# All files / src/constants subtitleLanguages.ts

0% Statements 0/30
0% Branches 0/22
0% Functions 0/3
0% Lines 0/27

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
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181

     /**
     * Subtitle language label utilities.
     *
     * The UI must stay in English, so labels are generated with English display names.
     */
    const ISO3_TO_ISO1: Record<string, string> = {
      afr: 'af',
      alb: 'sq',
      amh: 'am',
      ara: 'ar',
      arm: 'hy',
      aze: 'az',
      baq: 'eu',
      bel: 'be',
      ben: 'bn',
      bos: 'bs',
      bul: 'bg',
      bur: 'my',
      cat: 'ca',
      ces: 'cs',
      chi: 'zh',
      cze: 'cs',
      dan: 'da',
      deu: 'de',
      dut: 'nl',
      ell: 'el',
      eng: 'en',
      est: 'et',
      eus: 'eu',
      fas: 'fa',
      fin: 'fi',
      fra: 'fr',
      fre: 'fr',
      geo: 'ka',
      ger: 'de',
      glg: 'gl',
      gre: 'el',
      guj: 'gu',
      heb: 'he',
      hin: 'hi',
      hrv: 'hr',
      hun: 'hu',
      hye: 'hy',
      ice: 'is',
      ind: 'id',
      isl: 'is',
      ita: 'it',
      jpn: 'ja',
      kan: 'kn',
      kat: 'ka',
      kaz: 'kk',
      khm: 'km',
      kor: 'ko',
      lao: 'lo',
      lav: 'lv',
      lit: 'lt',
      mac: 'mk',
      mal: 'ml',
      mar: 'mr',
      may: 'ms',
      mkd: 'mk',
      mon: 'mn',
      msa: 'ms',
      mya: 'my',
      nep: 'ne',
      nld: 'nl',
      nor: 'no',
      pan: 'pa',
      per: 'fa',
      pol: 'pl',
      por: 'pt',
      ron: 'ro',
      rum: 'ro',
      rus: 'ru',
      sin: 'si',
      slk: 'sk',
      slo: 'sk',
      slv: 'sl',
      som: 'so',
      spa: 'es',
      sqi: 'sq',
      srp: 'sr',
      swe: 'sv',
      tam: 'ta',
      tel: 'te',
      tha: 'th',
      tur: 'tr',
      ukr: 'uk',
      urd: 'ur',
      uzb: 'uz',
      vie: 'vi',
      yid: 'yi',
      zho: 'zh'
    };

    const SPECIAL_LANGUAGE_NAMES: Record<string, string> = {
      und: 'Unknown',
      unknown: 'Unknown',
      zxx: 'No linguistic content'
    };

    const LANGUAGE_NAME_ALIASES: Record<string, string> = {
      espanol: 'Spanish',
      spanish: 'Spanish',
      portugues: 'Portuguese',
      portuguese: 'Portuguese',
      francais: 'French',
      french: 'French',
      deutsch: 'German',
      german: 'German',
      italiano: 'Italian',
      italian: 'Italian',
      english: 'English'
    };

    const hasIntlDisplayNames = typeof Intl !== 'undefined' && typeof Intl.DisplayNames === 'function';
    const languageDisplayNames = hasIntlDisplayNames ? new Intl.DisplayNames(['en'], { type: 'language' }) : null;

    function normalizeAscii(value: string): string {
      return value
        .normalize('NFD')
        .replace(/[\u0300-\u036f]/g, '')
        .toLowerCase()
        .trim();
    }

    function normalizeLanguageCode(code: string): string {
      const normalized = code.toLowerCase().trim().replace(/_/g, '-');
      if (!normalized) return normalized;

      const [languagePart, ...rest] = normalized.split('-');
      const iso1Language = ISO3_TO_ISO1[languagePart] ?? languagePart;

      return [iso1Language, ...rest].join('-');
    }

    /**
     * Backward-compatible map for explicit overrides.
     */
    export const SUBTITLE_LANGUAGE_NAMES: Record<string, string> = {
      ...SPECIAL_LANGUAGE_NAMES,
      'en-us': 'English (US)',
      'en-gb': 'English (UK)',
      'en-ca': 'English (Canada)',
      'en-au': 'English (Australia)',
      'es-419': 'Spanish (Latin America)',
      'pt-br': 'Portuguese (Brazil)',
      'pt-pt': 'Portuguese (Portugal)'
    };

    /**
     * Get the subtitle language name in English from a language code or label.
     */
    export function getSubtitleLanguageName(code: string): string {
      if (!code) return 'Unknown';

      const normalizedCode = normalizeLanguageCode(code);

      if (SUBTITLE_LANGUAGE_NAMES[normalizedCode]) {
        return SUBTITLE_LANGUAGE_NAMES[normalizedCode];
      }

      if (languageDisplayNames) {
        const displayName = languageDisplayNames.of(normalizedCode);
        if (displayName && displayName.toLowerCase() !== normalizedCode) {
          return displayName;
        }
      }

      const alias = LANGUAGE_NAME_ALIASES[normalizeAscii(code)];
      if (alias) {
        return alias;
      }

      if (normalizedCode.length > 3) {
        return code;
      }

      return normalizedCode.toUpperCase();
    }