All files translate.ts

98.21% Statements 55/56
93.75% Branches 15/16
100% Functions 8/8
98.04% Lines 50/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 99 100 101 102 103 104                        1x     1x 1x 1x   1x 8x 8x 5x 5x   8x   8x     8x 8x 8x   8x 8x     1x       9x 9x 9x 6x 6x   9x     1x 8x   1x 2x     1x     9x 9x 3x 3x 3x   6x 6x   9x     3x 1x 3x   1x 11x 11x 7x                           1x 1x 1x 1x 1x 1x 1x      
/*!
 * Mady
 *
 * Easy-to-use tool to manage and translate ICU MessageFormat messages.
 *
 * @copyright Guillermo Grau Panea 2016-present
 * @license MIT
 */
 
import { decode } from 'js-base64';
 
// Current locales
let _locales: Record<string, Function> = {};
 
// Caches
const allLocales: Record<string, Record<string, Function>> = {};
const allLocaleCode: Record<string, string> = {};
const keyCache: Record<string, string> = {};
 
const translate = (utf8: string, data?: any): string => {
  let base64 = keyCache[utf8];
  if (base64 == null) {
    base64 = decode(utf8);
    keyCache[utf8] = base64;
  }
  const fnTranslate = _locales[base64];
  let out;
  Iif (fnTranslate) {
    out = fnTranslate(data);
  } else {
    out = utf8;
    const idxContextSep = out.indexOf('_');
    if (idxContextSep >= 0) out = out.slice(idxContextSep + 1);
  }
  out = out.trim();
  return out;
};
 
const getLocaleOrLocaleCode = <T>(
  cache: Record<string, T>,
  initialLang: string
): { lang: string | null; result: T | null } => {
  let result = null;
  let lang: string | null = initialLang;
  while (!(result = cache[lang])) {
    lang = getParentBcp47(lang);
    if (!lang) break;
  }
  return { lang, result };
};
 
const addLocales = (lang: string, locales: Record<string, Function>) => {
  allLocales[lang] = locales;
};
const addLocaleCode = (lang: string, localeCode: string) => {
  allLocaleCode[lang] = localeCode;
};
 
const setLocales = (
  langOrLocales: string | Record<string, Function>
): string | null => {
  let out = null;
  if (typeof langOrLocales === 'string') {
    const { lang, result } = getLocaleOrLocaleCode(allLocales, langOrLocales);
    if (lang && result) _locales = result;
    out = lang;
  } else {
    console.log('here i am');
    _locales = langOrLocales;
  }
  return out;
};
 
const getLocales = (lang: string) => getLocaleOrLocaleCode(allLocales, lang);
const getLocaleCode = (lang: string) =>
  getLocaleOrLocaleCode(allLocaleCode, lang);
 
const getParentBcp47 = (bcp47: string) => {
  const tokens = bcp47.replace(/_/g, '-').split('-');
  if (tokens.length <= 1) return null;
  return tokens.slice(0, tokens.length - 1).join('-');
};
 
// =======================================
// Public API
// =======================================
const _t: {
  (utf8: string, data?: any): string;
  addLocales: typeof addLocales;
  addLocaleCode: typeof addLocaleCode;
  setLocales: typeof setLocales;
  getLocales: typeof getLocales;
  getLocaleCode: typeof getLocaleCode;
  getParentBcp47: typeof getParentBcp47;
} = translate as any;
_t.addLocales = addLocales;
_t.addLocaleCode = addLocaleCode;
_t.setLocales = setLocales;
_t.getLocales = getLocales;
_t.getLocaleCode = getLocaleCode;
_t.getParentBcp47 = getParentBcp47;
 
export default _t;