From 443dceaeec307070ec1fa3974b8d201abff330fd Mon Sep 17 00:00:00 2001 From: Ardani Rohman Date: Thu, 18 May 2017 00:41:54 +0700 Subject: [PATCH] improve example fallback translation and add new sample locale for lang id (#1983) --- examples/with-i18next/components/Title.js | 2 +- examples/with-i18next/pages/index.js | 7 +++++-- examples/with-i18next/static/locales/id/common.json | 3 +++ examples/with-i18next/static/locales/pt/common.json | 3 ++- examples/with-i18next/tools/startI18n.js | 3 ++- 5 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 examples/with-i18next/static/locales/id/common.json diff --git a/examples/with-i18next/components/Title.js b/examples/with-i18next/components/Title.js index ed6a37f6..3af705a6 100644 --- a/examples/with-i18next/components/Title.js +++ b/examples/with-i18next/components/Title.js @@ -1,2 +1,2 @@ import { translate } from 'react-i18next' -export default translate(['common'])((props) => (

{props.t('hello')}

)) +export default translate(['common'])((props) => (

{props.t('hello')}, {props.t('morning')}

)) diff --git a/examples/with-i18next/pages/index.js b/examples/with-i18next/pages/index.js index 2d429a86..c5eb77e5 100644 --- a/examples/with-i18next/pages/index.js +++ b/examples/with-i18next/pages/index.js @@ -4,9 +4,12 @@ import startI18n from '../tools/startI18n' import { getTranslation } from '../tools/translationHelpers' import Title from '../components/Title' +// get language from query parameter or url path +const lang = 'id' + export default class Homepage extends Component { static async getInitialProps () { - const translations = await getTranslation('pt', 'common', 'http://localhost:3000/static/locales/') + const translations = await getTranslation(lang, 'common', 'http://localhost:3000/static/locales/') return { translations } } @@ -14,7 +17,7 @@ export default class Homepage extends Component { constructor (props) { super(props) - this.i18n = startI18n(props.translations) + this.i18n = startI18n(props.translations, lang) } render (props) { diff --git a/examples/with-i18next/static/locales/id/common.json b/examples/with-i18next/static/locales/id/common.json new file mode 100644 index 00000000..d90a19f9 --- /dev/null +++ b/examples/with-i18next/static/locales/id/common.json @@ -0,0 +1,3 @@ +{ + "hello": "halo" +} \ No newline at end of file diff --git a/examples/with-i18next/static/locales/pt/common.json b/examples/with-i18next/static/locales/pt/common.json index c8f9b7c0..cbdf2773 100644 --- a/examples/with-i18next/static/locales/pt/common.json +++ b/examples/with-i18next/static/locales/pt/common.json @@ -1,3 +1,4 @@ { - "hello": "e ae tche" + "hello": "e ae tche", + "morning": "manha" } \ No newline at end of file diff --git a/examples/with-i18next/tools/startI18n.js b/examples/with-i18next/tools/startI18n.js index 791d2c93..db65b27f 100644 --- a/examples/with-i18next/tools/startI18n.js +++ b/examples/with-i18next/tools/startI18n.js @@ -1,6 +1,7 @@ import i18n from 'i18next' -const startI18n = file => i18n.init({ +const startI18n = (file, lang) => i18n.init({ + lng: lang, // active language http://i18next.com/translate/ fallbackLng: 'pt', resources: file, ns: ['common'],