Skip to content

Commit

Permalink
Merge 4d5d34f into 27b72e4
Browse files Browse the repository at this point in the history
  • Loading branch information
flepretre committed Feb 18, 2019
2 parents 27b72e4 + 4d5d34f commit 35231f1
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 29 deletions.
39 changes: 33 additions & 6 deletions packages/react-i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Add the provider above a component to configure and provide translation function

```jsx harmony
// Import the provider
import { I18nProvider } from '@m6we/react-i18n';
import { I18nProvider } from '@m6web/react-i18n';

// Dictionnary for your app, you should have a different dictionary for each language
const translations = {
Expand Down Expand Up @@ -51,7 +51,7 @@ This component needs React 16 at least because its render returns a string value

```jsx harmony
import React from 'react';
import { Trans } from '@m6we/react-i18n';
import { Trans } from '@m6web/react-i18n';

// Interpolation values
const data = { element: 'foo' };
Expand Down Expand Up @@ -79,7 +79,7 @@ export default const MyComponent = ({ nbExample, t }) => {

```jsx harmony
import React from 'react';
import { HtmlTrans } from '@m6we/react-i18n';
import { HtmlTrans } from '@m6web/react-i18n';

// Interpolation values
const data = { element: 'foo' };
Expand All @@ -98,7 +98,7 @@ export default const MyComponent = ({ nbExample, t }) => {
* **number**: amount used for plural forms
* **data**: object containing key/values used for interpolation in the translation
* **general**: use general plural form if truthy
* **element**: HTML element, or React element used for rendering. (default value: `span`)
* **element**: HTML element, or React element used for rendering. (default value: `span`)

Note that **number** and **data** can be used together.

Expand All @@ -108,7 +108,7 @@ This HOC provides the translate function to the component as prop.

```jsx harmony
import React from 'react';
import { translate } from '@m6we/react-i18n';
import { translate } from '@m6web/react-i18n';

// Interpolation values
const data = { element: 'foo' };
Expand Down Expand Up @@ -137,12 +137,39 @@ export default translate(MyComponent);

Note that **number** and **data** can be used together.

### useTranslate hook

This hook provides the `t` function in a functional component.

```jsx harmony
import React from 'react';
import { useTranslate } from '@m6web/react-i18n';

// Interpolation values
const data = { element: 'foo' };

export const MyComponent = ({ nbExample }) => {
const t = useTranslate();

return (
<div class="foo">
<h1>
{t('foo.bar')}
</h1>
<p>
{t('foo.exemple', data, nbExample, true)}
</p>
</div>
);
};
```

### BuildList

Build list function allows you to build a list in specific language.

```jsx harmony
import { buildListFunction } from '@m6we/react-i18n';
import { buildListFunction } from '@m6web/react-i18n';

// Define separators with translations
const lang = {
Expand Down
5 changes: 0 additions & 5 deletions packages/react-i18n/src/components/__mocks__/i18n.context.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { shallow } from 'enzyme';
import { translate } from '../i18n.container';

jest.mock('../i18n.context');
jest.mock('../../context/i18n.context');

describe('i18n container', () => {
const DummyComponent = props => <div {...props} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import React from 'react';
import { shallow } from 'enzyme';
import { HtmlTrans } from '../i18nElement.component';

jest.mock('../i18n.context', () => ({
Consumer: jest.fn(({ children}) => children(x => `<div>${x}</div>`))
jest.mock('../../context/i18n.context', () => ({
Context: {
Consumer: jest.fn(({ children}) => children(x => `<div>${x}</div>`))
}
}));

describe('i18n.renderProps', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { shallow } from 'enzyme';
import { Trans } from '../i18nString.component';

jest.mock('../i18n.context');
jest.mock('../../context/i18n.context');

describe('i18n.renderProps', () => {

Expand Down
6 changes: 3 additions & 3 deletions packages/react-i18n/src/components/i18n.container.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Consumer } from './i18n.context';
import { Context } from '../context/i18n.context';

export const translate = RenderComponent => {
const TranslatedComponent = props => (
<Consumer>
<Context.Consumer>
{t => {
if (props.componentRef) {
return <RenderComponent t={t} {...props} ref={element => props.componentRef(element)} />;
}

return <RenderComponent t={t} {...props} />;
}}
</Consumer>
</Context.Consumer>
);

TranslatedComponent.propTypes = {
Expand Down
3 changes: 0 additions & 3 deletions packages/react-i18n/src/components/i18n.context.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/react-i18n/src/components/i18n.provider.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import PropTypes from 'prop-types';
import { translate } from '../utils/i18n.utils';
import { Provider } from './i18n.context';
import { Context } from '../context/i18n.context';

export const I18nProvider = ({ lang, i18nNames, children }) => (
<Provider value={translate(lang, i18nNames)}>{children}</Provider>
<Context.Provider value={translate(lang, i18nNames)}>{children}</Context.Provider>
);

I18nProvider.propTypes = {
Expand Down
8 changes: 4 additions & 4 deletions packages/react-i18n/src/components/i18nElement.component.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Consumer } from './i18n.context';
import { Context } from '../context/i18n.context';

export const HtmlTrans = ({ i18nKey, data, number, general, element: Element, ...props }) => (
<Consumer>
<Context.Consumer>
{t => <Element {...props} dangerouslySetInnerHTML={{ __html: t(i18nKey, data, number, general) }} />}
</Consumer>
</Context.Consumer>
);

HtmlTrans.defaultProps = {
Expand All @@ -15,7 +15,7 @@ HtmlTrans.defaultProps = {
};

HtmlTrans.propTypes = {
element: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
element: PropTypes.oneOfType([PropTypes.string, PropTypes.element, PropTypes.func]),
i18nKey: PropTypes.string.isRequired,
data: PropTypes.object,
number: PropTypes.number,
Expand Down
4 changes: 2 additions & 2 deletions packages/react-i18n/src/components/i18nString.component.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Consumer } from './i18n.context';
import { Context } from './../context/i18n.context';

export const Trans = ({ i18nKey, data, number, general }) => (
<Consumer>{t => t(i18nKey, data, number, general)}</Consumer>
<Context.Consumer>{t => t(i18nKey, data, number, general)}</Context.Consumer>
);

Trans.defaultProps = {
Expand Down
7 changes: 7 additions & 0 deletions packages/react-i18n/src/context/__mocks__/i18n.context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Provider = jest.fn(x => x);
Provider.displayName = 'Provider';

const Consumer = jest.fn(({ children}) => children(jest.fn(x => x)));
Consumer.displayName = 'Consumer';

export const Context = { Provider, Consumer };
3 changes: 3 additions & 0 deletions packages/react-i18n/src/context/i18n.context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from 'react';

export const Context = React.createContext();
16 changes: 16 additions & 0 deletions packages/react-i18n/src/hooks/__tests__/useTranslate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useContext } from 'react';
import { Context } from '../../context/i18n.context';
import { useTranslate } from '../useTranslate';

jest.mock('react');

useContext.mockReturnValue('t function');

describe('useTranslate', () => {
it('should return the translation function', () => {
const t = useTranslate();

expect(useContext).toHaveBeenCalledWith(Context);
expect(t).toBe('t function');
});
});
4 changes: 4 additions & 0 deletions packages/react-i18n/src/hooks/useTranslate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { useContext } from 'react';
import { Context } from '../context/i18n.context';

export const useTranslate = () => useContext(Context);

0 comments on commit 35231f1

Please sign in to comment.