Code coverage report for js/i18n.tsx

# All files / js i18n.tsx

65.38% Statements 17/26
33.33% Branches 6/18
16.67% Functions 1/6
65.38% Lines 17/26

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
    911x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x
    1x

    1x

     import ca from "../../../translations/ca.json";
    import de from "../../../translations/de.json";
    import en from "../../../translations/en.json";
    import es from "../../../translations/es.json";
    import fr from "../../../translations/fr.json";
    import it from "../../../translations/it.json";
    import ja from "../../../translations/ja.json";
    import ko from "../../../translations/ko.json";
    import nl from "../../../translations/nl.json";
    import pl from "../../../translations/pl.json";
    import pt from "../../../translations/pt.json";
    import ru from "../../../translations/ru.json";
    import zh from "../../../translations/zh.json";

    type LektorTranslations = typeof en;
    export type TranslationEntry = keyof LektorTranslations;

    export const translations: Record<string, Partial<LektorTranslations>> = {
      ca,
      de,
      en,
      es,
      fr,
      it,
      ja,
      ko,
      nl,
      pl,
      pt,
      ru,
      zh,
    };

    let currentLanguage = "en";
    let currentTranslations = translations[currentLanguage] ?? {};

    export function setCurrentLanguage(lang: string) {
      currentLanguage = lang;
      currentTranslations = translations[currentLanguage];
    }

    export function getCurrentLanguge() {
      return currentLanguage;
    }

    export type Translatable = Partial<Record<string, string>>;

    /**
     * Get translation for a key.
     * @param key - The translation key.
     */
    export function trans(key: TranslationEntry): string {
      return currentTranslations[key] ?? key;
    }

    /**
     * Get translation from an object of translations
     * @param translation_object - The object containing translations.
     */
    export function trans_obj(translation_object: Translatable): string {
      return translation_object[currentLanguage] ?? translation_object.en ?? "";
    }

    /**
     * Get translation for a key with a fallback.
     * @param translation_object - The translation key
     * @param fallback - A fallback to use if the translation is missing.
     */
    export function trans_fallback(
      translation_object: Translatable | undefined,
      fallback: string
    ): string {
      if (!translation_object) {
        return fallback;
      }
      return trans_obj(translation_object) || fallback;
    }

    /**
     * Get translation for a key with a `%s` replacement.
     * @param key - The translation key
     * @param replacement - replacement for `%s`.
     */
    export function trans_format(
      key: TranslationEntry,
      replacement: string
    ): string {
      const translation = trans(key);
      return translation.replace("%s", replacement);
    }