-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from DeKal/add-multi-language
Add localize component
- Loading branch information
Showing
16 changed files
with
1,168 additions
and
277 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { renderToStaticMarkup } from 'react-dom/server' | ||
import { withLocalize } from 'react-localize-redux' | ||
import enTrans from '~/public/translations/en.json' | ||
import vnTrans from '~/public/translations/vn.json' | ||
import { EN } from '~/consts/langs' | ||
|
||
const onMissingTranslation = ({ translationId }) => { | ||
return translationId | ||
} | ||
const defaultLanguage = EN | ||
|
||
class LocalizedComponent extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
|
||
const languages = ['en', 'vn'] | ||
|
||
this.props.initialize({ | ||
languages, | ||
options: { | ||
renderToStaticMarkup, | ||
defaultLanguage, | ||
onMissingTranslation, | ||
renderInnerHtml: true | ||
} | ||
}) | ||
|
||
this.props.addTranslationForLanguage(enTrans, 'en') | ||
this.props.addTranslationForLanguage(vnTrans, 'vn') | ||
} | ||
|
||
render() { | ||
return this.props.children | ||
} | ||
|
||
componentDidMount() { | ||
const lang = window.localStorage.getItem('lang') || defaultLanguage | ||
this.props.setActiveLanguage(lang) | ||
} | ||
} | ||
|
||
LocalizedComponent.propTypes = { | ||
initialize: PropTypes.func, | ||
addTranslationForLanguage: PropTypes.func, | ||
children: PropTypes.element, | ||
setActiveLanguage: PropTypes.func | ||
} | ||
|
||
export default withLocalize(LocalizedComponent) |
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,11 @@ | ||
{ | ||
"english": "english", | ||
"vietnamese": "vietnamese", | ||
"Menu": "Menu", | ||
"Home": "Home", | ||
"About": "About", | ||
"Experiences": "Experiences", | ||
"Certifications": "Certifications", | ||
"Projects": "Projects", | ||
"Contacts": "Contacts" | ||
} |
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,11 @@ | ||
{ | ||
"english": "Tiếng Anh", | ||
"vietnamese": "Tiếng Việt", | ||
"Menu": "Mục lục", | ||
"Home": "Trang chủ", | ||
"About": "Về tôi", | ||
"Experiences": "Kinh nghiệm", | ||
"Certifications": "Chứng chỉ", | ||
"Projects": "Dự án", | ||
"Contacts": "Liên lạc" | ||
} |
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 |
---|---|---|
@@ -1,26 +1,37 @@ | ||
import * as React from 'react' | ||
import { shallowWithTheme } from '~/utils/withThemeProviders' | ||
import { mountWithTheme } from '~/utils/withThemeProviders' | ||
import NavLink from '~/views/components/nav/NavLink' | ||
import { EN, VN } from '~/consts/langs' | ||
|
||
describe('Nav Link', () => { | ||
it('should render with English selected', function() { | ||
const wrap = shallowWithTheme(<NavLink lang={EN} selectedLang={EN} />) | ||
const wrap = mountWithTheme(<NavLink lang={EN} selectedLang={EN} />) | ||
expect(wrap).toMatchSnapshot() | ||
}) | ||
|
||
it('should render with Vietnamese selected', function() { | ||
const wrap = shallowWithTheme(<NavLink lang={VN} selectedLang={VN} />) | ||
const wrap = mountWithTheme(<NavLink lang={VN} selectedLang={VN} />) | ||
expect(wrap).toMatchSnapshot() | ||
}) | ||
|
||
it('should change Language when clicking', function() { | ||
const setSelectedLang = jest.fn() | ||
const wrap = shallowWithTheme( | ||
<NavLink lang={VN} selectedLang={EN} setSelectedLang={setSelectedLang} /> | ||
const setActiveLanguage = jest.fn() | ||
const wrap = mountWithTheme( | ||
<NavLink | ||
lang={VN} | ||
selectedLang={EN} | ||
setSelectedLang={setSelectedLang} | ||
setActiveLanguage={setActiveLanguage} | ||
/> | ||
) | ||
wrap.simulate('click') | ||
expect(wrap).toMatchSnapshot() | ||
|
||
expect(setSelectedLang.mock.calls.length).toEqual(1) | ||
expect(setSelectedLang).toHaveBeenCalledWith(VN); | ||
expect(setSelectedLang).toHaveBeenCalledWith(VN) | ||
|
||
expect(setActiveLanguage.mock.calls.length).toEqual(1) | ||
expect(setActiveLanguage).toHaveBeenCalledWith(VN) | ||
}) | ||
}) |
Oops, something went wrong.