From 248776b220e4c824031ae2f024884e213d29dc77 Mon Sep 17 00:00:00 2001 From: Gina Contrino Date: Fri, 3 Nov 2017 14:07:43 +0100 Subject: [PATCH] Refactor i18n --- app/src/i18n.js | 23 ++----------- app/src/languages.js | 12 ------- {src/assets/images => i18n}/flags/de.png | Bin {src/assets/images => i18n}/flags/en.png | Bin i18n/i18n-setup.js | 27 +++++++++++++++ i18n/languages.js | 13 +++++++ {src => i18n}/locales/de/common.json | 0 {src => i18n}/locales/en/common.json | 0 package.json | 2 +- src/components/languageDropdown/index.js | 2 +- src/constants/languages.js | 14 -------- src/i18n.js | 42 ++--------------------- 12 files changed, 46 insertions(+), 89 deletions(-) delete mode 100644 app/src/languages.js rename {src/assets/images => i18n}/flags/de.png (100%) rename {src/assets/images => i18n}/flags/en.png (100%) create mode 100644 i18n/i18n-setup.js create mode 100644 i18n/languages.js rename {src => i18n}/locales/de/common.json (100%) rename {src => i18n}/locales/en/common.json (100%) delete mode 100644 src/constants/languages.js diff --git a/app/src/i18n.js b/app/src/i18n.js index 7e0274b49..e9a19e42e 100755 --- a/app/src/i18n.js +++ b/app/src/i18n.js @@ -1,24 +1,5 @@ -import i18n from 'i18next'; // eslint-disable-line import/no-extraneous-dependencies -import languages from './languages'; +import setup from '../../i18n/i18n-setup'; -const resources = Object.keys(languages).reduce((accumulator, key) => { - accumulator[key] = { - common: languages[key].common, - }; - return accumulator; -}, {}); - -i18n - .init({ - fallbackLng: 'en', - resources, - lang: 'en', - ns: ['common'], - defaultNS: 'common', - saveMissing: true, - debug: false, - keySeparator: '>', - nsSeparator: '|', - }); +const i18n = setup(); export default i18n; diff --git a/app/src/languages.js b/app/src/languages.js deleted file mode 100644 index 1d052a3fd..000000000 --- a/app/src/languages.js +++ /dev/null @@ -1,12 +0,0 @@ -const languages = { - en: { - name: 'English', - common: require('../build/locales/en/common.json'), - }, - de: { - name: 'Deutsch', - common: require('../build/locales/de/common.json'), - }, -}; - -export default languages; diff --git a/src/assets/images/flags/de.png b/i18n/flags/de.png similarity index 100% rename from src/assets/images/flags/de.png rename to i18n/flags/de.png diff --git a/src/assets/images/flags/en.png b/i18n/flags/en.png similarity index 100% rename from src/assets/images/flags/en.png rename to i18n/flags/en.png diff --git a/i18n/i18n-setup.js b/i18n/i18n-setup.js new file mode 100644 index 000000000..8e08b07a5 --- /dev/null +++ b/i18n/i18n-setup.js @@ -0,0 +1,27 @@ +import i18n from 'i18next'; +import languages from './languages'; + +const setup = () => { + const resources = Object.keys(languages).reduce((accumulator, key) => { + accumulator[key] = { + common: languages[key].common, + }; + return accumulator; + }, {}); + + + return i18n + .init({ + fallbackLng: 'en', + resources, + lang: 'en', + ns: ['common'], + defaultNS: 'common', + saveMissing: true, + debug: false, + keySeparator: '>', + nsSeparator: '|', + }); +}; + +export default setup; diff --git a/i18n/languages.js b/i18n/languages.js new file mode 100644 index 000000000..4915c6598 --- /dev/null +++ b/i18n/languages.js @@ -0,0 +1,13 @@ +const languages = { + en: { + name: 'English', + common: require('./locales/en/common.json'), + flag: require('./flags/en.png'), + }, + de: { + name: 'Deutsch', + common: require('./locales/de/common.json'), + flag: require('./flags/de.png'), + }, +}; +export default languages; diff --git a/src/locales/de/common.json b/i18n/locales/de/common.json similarity index 100% rename from src/locales/de/common.json rename to i18n/locales/de/common.json diff --git a/src/locales/en/common.json b/i18n/locales/en/common.json similarity index 100% rename from src/locales/en/common.json rename to i18n/locales/en/common.json diff --git a/package.json b/package.json index 6e16a9dca..231cc7b8b 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "dist:win": "build --win --ia32 --x64", "dist:mac": "build --mac", "dist:linux": "build --linux --ia32 --x64 --armv7l", - "copy-files": "cpx \"./src/{index.html,assets/**/*,locales/**/*}\" ./app/build/", + "copy-files": "cpx \"./src/{index.html,assets/**/*}\" ./app/build/", "clean-build": "rm -rf app/build", "clean-dist": "rm -rf dist", "eslint": "eslint ./src/ ./app/src/ ./app/config/ ./features/", diff --git a/src/components/languageDropdown/index.js b/src/components/languageDropdown/index.js index dbbd2350f..fb9f50ed4 100644 --- a/src/components/languageDropdown/index.js +++ b/src/components/languageDropdown/index.js @@ -3,7 +3,7 @@ import { translate } from 'react-i18next'; import React from 'react'; import i18n from '../../i18n'; -import languages from '../../constants/languages'; +import languages from '../../../i18n/languages'; const languagesSource = Object.keys(languages).map(key => ({ value: key, diff --git a/src/constants/languages.js b/src/constants/languages.js deleted file mode 100644 index cc885990c..000000000 --- a/src/constants/languages.js +++ /dev/null @@ -1,14 +0,0 @@ -const languages = { - en: { - name: 'English', - common: require('../locales/en/common.json'), - flag: require('../assets/images/flags/en.png'), - }, - de: { - name: 'Deutsch', - common: require('../locales/de/common.json'), - flag: require('../assets/images/flags/de.png'), - }, -}; - -export default languages; diff --git a/src/i18n.js b/src/i18n.js index 4e130c7c8..5c96c5ee2 100755 --- a/src/i18n.js +++ b/src/i18n.js @@ -1,43 +1,5 @@ -import i18n from 'i18next'; -import languages from './constants/languages'; +import setup from '../i18n/i18n-setup'; -const resources = Object.keys(languages).reduce((accumulator, key) => { - accumulator[key] = { - common: languages[key].common, - }; - return accumulator; -}, {}); - -i18n - .init({ - fallbackLng: 'en', - resources, - react: { - // wait: true, // globally set to wait for loaded translations in translate hoc - // exposeNamespace: true // exposes namespace on data-i18next-options to be used in eg. - }, - - // have a common namespace used around the full app - ns: ['common'], - defaultNS: 'common', - - keySeparator: '>', - nsSeparator: '|', - - debug: false, - - // cache: { - // enabled: true, - // }, - - interpolation: { - escapeValue: false, // not needed for react!! - formatSeparator: ',', - format: (value, format) => { - if (format === 'uppercase') return value.toUpperCase(); - return value; - }, - }, - }); +const i18n = setup(); export default i18n;