-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i18n Setup : Simpler configuration + examples (#1847)
* Replace example with createMany * change comments + add an example for all databases * return to current seed.js * Rewrite a simpler configuration of i18n * Add a template for translation locale files * add sync node_module and locale files * add comment and todo * return to path.join * exemple with default generate home page * remove async for writeFile tasks * missing a "," in json files * add LanguageDetector * add error handling * remove "See this doc for info:" * Fix typo Co-authored-by: Claire Froelich <[email protected]> * Update packages/cli/src/commands/setup/i18n/i18n.js * index --> app * fix path.join error * add import './i18n' after the last import * clean addI18nImport function * fix "Module not found: Error: Can't resolve ..." * function name typo * remove Sync yarn.lock step Co-authored-by: Claire Froelich <[email protected]> Co-authored-by: David Price <[email protected]>
- Loading branch information
1 parent
4a899b5
commit 42ec5eb
Showing
4 changed files
with
189 additions
and
85 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
11 changes: 11 additions & 0 deletions
11
packages/cli/src/commands/setup/i18n/templates/en.json.template
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 @@ | ||
{ | ||
"Welcome to RedwoodJS": "Welcome to RedwoodJS", | ||
"info": "This is your English translation file", | ||
"see": "https://www.i18next.com/translation-function/essentials", | ||
"HomePage" : { | ||
"title": "Home Page", | ||
"info": "Find me in", | ||
"route": "My default route is named", | ||
"link": "link to me with" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/cli/src/commands/setup/i18n/templates/fr.json.template
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 @@ | ||
{ | ||
"Welcome to RedwoodJS": "Bienvenu sur RedwoodJS", | ||
"info": "Ceci est votre fichier de traduction", | ||
"see": "https://www.i18next.com/translation-function/essentials", | ||
"HomePage": { | ||
"title": "Page d'accueil", | ||
"info": "Trouve moi dans", | ||
"route": "Ma route par default se nomme", | ||
"link": "le lien vers moi avec" | ||
} | ||
} |
97 changes: 43 additions & 54 deletions
97
packages/cli/src/commands/setup/i18n/templates/i18n.js.template
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,64 +1,53 @@ | ||
import i18n from 'i18next' | ||
import HttpApi from 'i18next-http-backend' | ||
import LanguageDetector from 'i18next-browser-languagedetector' | ||
import { initReactI18next } from 'react-i18next' | ||
import LanguageDetector from 'i18next-browser-languagedetector' | ||
import fr from './locales/fr.json' | ||
import en from './locales/en.json' | ||
|
||
// This is a simple i18n configuration with English and French translation. | ||
// You can find the translation on web/src/locales/{language}.json | ||
// see : https://react.i18next.com | ||
// Here an example of how to use it in your components, pages or layouts : | ||
/* | ||
import { Link, routes } from '@redwoodjs/router' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
const HomePage = () => { | ||
const { t, i18n } = useTranslation() | ||
return ( | ||
<> | ||
<h1>{t('HomePage.title')}</h1> | ||
<button onClick={() => i18n.changeLanguage('fr')}>fr</button> | ||
<button onClick={() => i18n.changeLanguage('en')}>en</button> | ||
<p> | ||
{t('HomePage.info')} <code>./web/src/pages/HomePage/HomePage.js</code> | ||
</p> | ||
<p> | ||
{t('HomePage.route')} <code>home</code>, {t('HomePage.link')}` | ||
<Link to={routes.home()}>Home</Link>` | ||
</p> | ||
</> | ||
) | ||
} | ||
|
||
export default HomePage | ||
*/ | ||
i18n | ||
.use(HttpApi) | ||
.use(LanguageDetector) | ||
.use(initReactI18next) | ||
// detect user language | ||
// learn more: https://github.com/i18next/i18next-browser-languageDetector | ||
.use(LanguageDetector) | ||
.init({ | ||
backend: { | ||
loadPath: '/locales/{{lng}}/{{ns}}.json', | ||
addPath: '/locales/{{lng}}/{{ns}}.json', | ||
}, | ||
load: 'all', | ||
ns: ['site'], | ||
defaultNS: 'site', | ||
fallbackNS: 'site', | ||
fallbackLng: 'en', | ||
whitelist: ['en'], | ||
preload: ['en'], | ||
interpolation: { escapeValue: false }, // React already does escaping | ||
lng: 'en', | ||
lowerCaseLng: true, | ||
// saveMissing: true, | ||
initImmediate: true, | ||
detection: { | ||
// order and from where user language should be detected | ||
order: ['querystring', 'cookie', 'localStorage', 'sessionStorage', 'navigator', 'htmlTag', 'path', 'subdomain'], | ||
|
||
// keys or params to lookup language from | ||
lookupQuerystring: 'lng', | ||
lookupCookie: 'i18next', | ||
lookupLocalStorage: 'i18nextLng', | ||
lookupSessionStorage: 'i18nextLng', | ||
lookupFromPathIndex: 0, | ||
lookupFromSubdomainIndex: 0, | ||
|
||
// cache user language on | ||
caches: ['localStorage', 'cookie'], | ||
excludeCacheFor: ['cimode'], // languages to not persist (cookie, localStorage) | ||
|
||
// optional expire and domain for set cookie | ||
cookieMinutes: 10, | ||
cookieDomain: 'myDomain', | ||
|
||
// optional htmlTag with lang attribute, the default is: | ||
htmlTag: document.documentElement, | ||
|
||
// optional set cookie options, reference:[MDN Set-Cookie docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) | ||
cookieOptions: { path: '/', sameSite: 'strict' } | ||
}, | ||
react: { | ||
wait: true, | ||
useSuspense: false, | ||
transSupportBasicHtmlNodes: true, | ||
}, | ||
interpolation: { | ||
escapeValue: false, // react already safes from xss | ||
fallbackLng: 'en', | ||
resources: { | ||
en: { | ||
translation: en, | ||
}, | ||
fr: { | ||
translation: fr, | ||
}, | ||
}, | ||
nsSeparator: ':', | ||
keySeparator: '.', | ||
}) | ||
|
||
export default i18n |