| Total Complexity | 2 |
| Complexity/F | 1 |
| Lines of Code | 48 |
| Function Count | 2 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | import React from 'react' |
||
| 2 | import PropTypes from 'prop-types' |
||
| 3 | import { renderToStaticMarkup } from 'react-dom/server' |
||
| 4 | import { withLocalize } from 'react-localize-redux' |
||
| 5 | import enTrans from '~/public/translations/en.json' |
||
| 6 | import vnTrans from '~/public/translations/vn.json' |
||
| 7 | import { EN } from '~/modules/home/consts/langs' |
||
| 8 | |||
| 9 | 1 | const defaultLanguage = EN |
|
| 10 | |||
| 11 | class LocalizedComponent extends React.Component { |
||
| 12 | constructor(props) { |
||
| 13 | 1 | super(props) |
|
| 14 | |||
| 15 | 1 | const languages = ['en', 'vn'] |
|
| 16 | |||
| 17 | 1 | this.props.initialize({ |
|
| 18 | languages, |
||
| 19 | options: { |
||
| 20 | renderToStaticMarkup, |
||
| 21 | defaultLanguage, |
||
| 22 | renderInnerHtml: true |
||
| 23 | } |
||
| 24 | }) |
||
| 25 | |||
| 26 | 1 | this.props.addTranslationForLanguage(enTrans, 'en') |
|
| 27 | 1 | this.props.addTranslationForLanguage(vnTrans, 'vn') |
|
| 28 | } |
||
| 29 | |||
| 30 | render() { |
||
| 31 | 1 | return this.props.children |
|
| 32 | } |
||
| 33 | |||
| 34 | componentDidMount() { |
||
| 35 | 2 | const lang = window.localStorage.getItem('lang') || defaultLanguage |
|
| 36 | 1 | this.props.setActiveLanguage(lang) |
|
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | 1 | LocalizedComponent.propTypes = { |
|
| 41 | initialize: PropTypes.func, |
||
| 42 | addTranslationForLanguage: PropTypes.func, |
||
| 43 | children: PropTypes.element, |
||
| 44 | setActiveLanguage: PropTypes.func |
||
| 45 | } |
||
| 46 | |||
| 47 | export default withLocalize(LocalizedComponent) |
||
| 48 |