# All files / react/I18n translation.jsx

14.29% Statements 2/14
0% Branches 0/9
0% Functions 0/2
15.38% Lines 2/13

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

    2x

    2x
     import Polyglot from 'node-polyglot'
    import { DEFAULT_LANG } from '.'

    export let _polyglot

    export const initTranslation = (
      lang,
      dictRequire,
      context,
      defaultLang = DEFAULT_LANG
    ) => {
      _polyglot = new Polyglot({
        phrases: dictRequire(defaultLang),
        locale: defaultLang
      })

      // Load global locales
      if (lang && lang !== defaultLang) {
        try {
          const dict = dictRequire(lang)
          _polyglot.extend(dict)
          _polyglot.locale(lang)
        } catch (e) {
          console.warn(`The dict phrases for "${lang}" can't be loaded`)
        }
      }

      // Load context locales
      if (context) {
        const dict = dictRequire(lang, context)
        _polyglot.extend(dict)
      }

      return _polyglot
    }

    export const extend = dict => _polyglot && _polyglot.extend(dict)