All files / js i18n.tsx

38.1% Statements 8/21
37.5% Branches 9/24
25% Functions 2/8
38.1% Lines 8/21

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  1x 1x 1x                   1x     1x   1x 1x                                   1x                                                                      
function loadTranslations() {
  const rv: Record<string, Record<string, string>> = {};
  try {
    const ctx = require.context("../../../translations", true, /\.json$/);
    ctx.keys().forEach((key) => {
      const langIdMatch = key.match(/([a-z]+)/);
      if (langIdMatch) {
        rv[langIdMatch[1]] = ctx(key);
      }
    });
  } catch (err) {
    // require.context is not available when running tests.
  }
  return rv;
}
 
const translations: Record<string, Record<string, string>> = loadTranslations();
 
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: string): 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: string, replacement: string): string {
  const translation = trans(key);
  return translation.replace("%s", replacement);
}