-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i18n-calypso: add useTranslate hook (#31043)
* i18n-calypso: add useTranslate React hook * post-share/no-connection-notice: use the useTranslate hook * Optimize the useTranslate hook a bit Use lazy creation of the initial state, create the change handler inside the `useEffect` callback. Both changes prevent creation of unneeded function objects on every hook call. * Document the useTranslate hook * Add unit test for the useTranslate hook * Returns just the translate function from the hook (not array) and expose translate.localeSlug property * Use the ReactDOM rendered and JSDOM in tests * Add unit test for useTranslate reactive rerender * Add test for reactivity when locale does not change, fix implementation
- Loading branch information
Showing
6 changed files
with
178 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import React from 'react'; | ||
|
||
export default function( i18n ) { | ||
function bindTranslate() { | ||
const translate = i18n.translate.bind( i18n ); | ||
Object.defineProperty( translate, 'localeSlug', { get: i18n.getLocaleSlug.bind( i18n ) } ); | ||
return translate; | ||
} | ||
|
||
return function useTranslate() { | ||
const [ translate, setTranslate ] = React.useState( bindTranslate ); | ||
|
||
React.useEffect(() => { | ||
const onChange = () => setTranslate( bindTranslate ); | ||
i18n.on( 'change', onChange ); | ||
return () => i18n.off( 'change', onChange ); | ||
}, []); | ||
|
||
return translate; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
/** | ||
* External dependencies | ||
*/ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import i18n, { useTranslate } from '../src'; | ||
|
||
function Label() { | ||
const translate = useTranslate(); | ||
return translate( 'hook (%(lang)s)', { args: { lang: translate.localeSlug } } ); | ||
} | ||
|
||
describe( 'useTranslate()', () => { | ||
let container; | ||
|
||
beforeEach( () => { | ||
// reset to default locale | ||
i18n.setLocale(); | ||
|
||
// create container | ||
container = document.createElement( 'div' ); | ||
document.body.appendChild( container ); | ||
} ); | ||
|
||
afterEach( () => { | ||
// tear down the container | ||
ReactDOM.unmountComponentAtNode( container ); | ||
document.body.removeChild( container ); | ||
container = null; | ||
} ); | ||
|
||
test( 'renders a translated string', () => { | ||
// set some locale data | ||
i18n.setLocale( { | ||
'': { localeSlug: 'cs' }, | ||
'hook (%(lang)s)': [ 'háček (%(lang)s)' ], | ||
} ); | ||
|
||
// render the Label component | ||
act( () => { | ||
ReactDOM.render( <Label />, container ); | ||
} ); | ||
|
||
// check that it's translated | ||
expect( container.textContent ).toBe( 'háček (cs)' ); | ||
} ); | ||
|
||
test( 'rerenders after locale change', () => { | ||
// render with the default locale | ||
act( () => { | ||
ReactDOM.render( <Label />, container ); | ||
} ); | ||
|
||
expect( container.textContent ).toBe( 'hook (en)' ); | ||
|
||
// change locale and ensure that React UI is rerendered | ||
act( () => { | ||
i18n.setLocale( { | ||
'': { localeSlug: 'cs' }, | ||
'hook (%(lang)s)': [ 'háček (%(lang)s)' ], | ||
} ); | ||
} ); | ||
|
||
expect( container.textContent ).toBe( 'háček (cs)' ); | ||
} ); | ||
|
||
test( 'rerenders after update of current locale translations', () => { | ||
// set some locale data | ||
i18n.setLocale( { | ||
'': { localeSlug: 'cs' }, | ||
'hook (%(lang)s)': [ 'háček (%(lang)s)' ], | ||
} ); | ||
|
||
// render the Label component | ||
act( () => { | ||
ReactDOM.render( <Label />, container ); | ||
} ); | ||
|
||
// check that it's translated | ||
expect( container.textContent ).toBe( 'háček (cs)' ); | ||
|
||
// update the translations for the current locale | ||
act( () => { | ||
i18n.setLocale( { | ||
'': { localeSlug: 'cs' }, | ||
'hook (%(lang)s)': [ 'hák (%(lang)s)' ], | ||
} ); | ||
} ); | ||
|
||
// check that the rendered translation is updated | ||
expect( container.textContent ).toBe( 'hák (cs)' ); | ||
} ); | ||
} ); |