# All files / react/I18n index.jsx

55% Statements 11/20
0% Branches 0/4
37.5% Functions 3/8
52.63% Lines 10/19

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

    2x

    2x

    2x

    2x

    2x

    3x
    3x
    14x

    3x
    3x

     /**
     * preact plugin that provides an I18n helper using a Higher Order Component.
     */

    'use strict'

    import React, { Component } from 'react'
    import PropTypes from 'prop-types'

    import { initTranslation } from './translation.jsx'
    import { initFormat } from './format.jsx'

    export const DEFAULT_LANG = 'en'

    // Provider root component
    export class I18n extends Component {
      constructor(props) {
        super(props)
        this.init(this.props)
      }

      init(props) {
        const { polyglot, lang, dictRequire, context, defaultLang } = props

        this.translation =
          polyglot || initTranslation(lang, dictRequire, context, defaultLang)
        this.format = initFormat(lang, defaultLang)
      }

      getChildContext() {
        return {
          t: this.translation.t.bind(this.translation),
          f: this.format,
          lang: this.props.lang
        }
      }

      componentWillReceiveProps(newProps) {
        if (newProps.lang !== this.props.lang) {
          this.init(newProps)
        }
      }

      render() {
        return React.Children.only(this.props.children)
      }
    }

    I18n.propTypes = {
      lang: PropTypes.string.isRequired, // current language.
      polyglot: PropTypes.object, // A polyglot instance.
      dictRequire: PropTypes.func, // A callback to load locales.
      context: PropTypes.string, // current context.
      defaultLang: PropTypes.string // default language. By default is 'en'
    }

    I18n.defaultProps = {
      defaultLang: DEFAULT_LANG
    }

    const i18nContextTypes = {
      t: PropTypes.func,
      f: PropTypes.func,
      lang: PropTypes.string
    }

    I18n.childContextTypes = i18nContextTypes

    // higher order decorator for components that need `t` and/or `f`
    export const translate = () => WrappedComponent => {
      const Wrapper = (props, context) => (
        <WrappedComponent
          {...props}
          t={context.t}
          f={context.f}
          lang={context.lang}
        />
      )
      Wrapper.contextTypes = i18nContextTypes
      return Wrapper
    }

    export { initTranslation, extend } from './translation'

    export default I18n