From 723554b858aa3eefe746a8d96b63d78fd6214a8a Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Tue, 14 May 2019 11:06:08 +0200 Subject: [PATCH 1/4] test: Testing withLocales --- react/I18n/withLocales.spec.jsx | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 react/I18n/withLocales.spec.jsx diff --git a/react/I18n/withLocales.spec.jsx b/react/I18n/withLocales.spec.jsx new file mode 100644 index 0000000000..9f1e4b7528 --- /dev/null +++ b/react/I18n/withLocales.spec.jsx @@ -0,0 +1,46 @@ +import React from 'react' +import { I18n } from '.' +import withLocales from './withLocales' + +const globalLocales = { + en: { + 'hello-world': 'Hello global world !' + }, + fr: { + 'hello-world': 'Bonjour le monde global !' + } +} + +const componentLocales = { + en: { + 'hello-world': 'Hello local world !' + }, + fr: { + 'hello-world': 'Bonjour le monde local !' + } +} + +class Component extends React.Component { + render() { + const { t } = this.props + return
{t('hello-world')}
+ } +} +const TComponent = withLocales(componentLocales)(Component) + +describe('with locales', () => { + let root + const setup = ({ lang }) => { + root = mount( + globalLocales[localeCode]}> + + + ) + } + it('should provide t with correct locale strings', () => { + setup({ lang: 'en' }) + expect(root.text()).toBe('Hello local world !') + setup({ lang: 'fr' }) + expect(root.text()).toBe('Bonjour le monde local !') + }) +}) From cdda573433cfb9013728035ca4067bb70c0d54e5 Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Wed, 22 May 2019 11:55:16 +0200 Subject: [PATCH 2/4] fix: Correctly receive context and pass props --- react/I18n/withLocales.jsx | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/react/I18n/withLocales.jsx b/react/I18n/withLocales.jsx index 6ad5546989..a6227b13e1 100644 --- a/react/I18n/withLocales.jsx +++ b/react/I18n/withLocales.jsx @@ -1,13 +1,25 @@ import React from 'react' import { I18n, translate } from './' +import omit from 'lodash/omit' -const withLocales = locales => Component => - translate()(props => { - return ( - locales[localeCode]} lang={props.lang}> - - - ) - }) +const withLocales = locales => Component => { + // The inner components needs to receive t + const Translated = translate()(Component) + + class Wrapped extends React.Component { + render() { + // Do not pass t downwards + const { lang, ...rest } = omit(this.props, 't') + return ( + locales[localeCode]} lang={lang}> + + + ) + } + } + + // The outer component needs to receive lang + return translate()(Wrapped) +} export default withLocales From 3d045a31cd0fa675175eba3e9dbcc7615a81d56c Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Wed, 22 May 2019 11:55:31 +0200 Subject: [PATCH 3/4] refactor: Change variable name --- react/I18n/index.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/react/I18n/index.jsx b/react/I18n/index.jsx index aa5d6684d4..00f9f9e9f6 100644 --- a/react/I18n/index.jsx +++ b/react/I18n/index.jsx @@ -22,14 +22,14 @@ export class I18n extends Component { init(props) { const { polyglot, lang, dictRequire, context, defaultLang } = props - this.translation = + this.translator = polyglot || initTranslation(lang, dictRequire, context, defaultLang) this.format = initFormat(lang, defaultLang) } getChildContext() { return { - t: this.translation.t.bind(this.translation), + t: this.translator.t.bind(this.translator), f: this.format, lang: this.props.lang } From b4ffc954a2d39e8dbcdfceae136e93c44b67505d Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Wed, 22 May 2019 12:02:23 +0200 Subject: [PATCH 4/4] feat: Add display name --- react/I18n/withLocales.jsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/react/I18n/withLocales.jsx b/react/I18n/withLocales.jsx index a6227b13e1..6f0c30e5cb 100644 --- a/react/I18n/withLocales.jsx +++ b/react/I18n/withLocales.jsx @@ -18,6 +18,9 @@ const withLocales = locales => Component => { } } + Wrapped.displayName = `withLocales(${Component.displayName || + Component.name})` + // The outer component needs to receive lang return translate()(Wrapped) }