Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Init i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
JayJay1024 committed Mar 9, 2021
1 parent 6b5ffdb commit b70a4ee
Show file tree
Hide file tree
Showing 6 changed files with 15,581 additions and 0 deletions.
15,505 changes: 15,505 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"i18next": "^19.9.1",
"i18next-browser-languagedetector": "^6.0.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-i18next": "^11.8.9",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
Expand Down
16 changes: 16 additions & 0 deletions src/locales/browserDetector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default {
name: 'browser',

lookup(options) {
// options -> are passed in options
var userLang = navigator.language || navigator.userLanguage;
return userLang;
},

cacheUserLanguage(lng, options) {
// options -> are passed in options
// lng -> current language, will be called after init and on changeLanguage

// store it
}
};
1 change: 1 addition & 0 deletions src/locales/en-us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
55 changes: 55 additions & 0 deletions src/locales/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import LanguageDetector from 'i18next-browser-languagedetector';
import browserDetector from './browserDetector'

import en_us from './en-us'
import zh_cn from './zh-cn'

const languageDetector = new LanguageDetector();
languageDetector.addDetector(browserDetector);



// the translations
// (tip move them in a JSON file and import them)
const resources = {
en: {
...en_us
},
zh: {
...zh_cn
}
};

i18n
.use(initReactI18next) // passes i18n down to react-i18next
.use(languageDetector)
.init({
resources,
// lng: localStorage.getItem("lng") || 'en-us',
detection:{
// order and from where user language should be detected
order: ['querystring', 'cookie', 'localStorage','browser', 'navigator', 'path', 'subdomain'],

// keys or params to lookup language from
lookupQuerystring: 'lng',
lookupCookie: 'i18next',
lookupLocalStorage: 'i18nextLng',
lookupFromPathIndex: 0,
lookupFromSubdomainIndex: 0,

// cache user language on
caches: ['localStorage', 'cookie'],

// optional htmlTag with lang attribute, the default is:
},
fallbackLng: "en",
keySeparator: false, // we do not use keys in form messages.welcome

interpolation: {
escapeValue: false // react already safes from xss
}
});

export default i18n;
1 change: 1 addition & 0 deletions src/locales/zh-cn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit b70a4ee

Please sign in to comment.