Added: i18next logic
This commit is contained in:
5
public/assets/locales/en/translation.json
Normal file
5
public/assets/locales/en/translation.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"app_title": "Hey there",
|
||||
"language": "Language",
|
||||
"welcome_message": "Welcome with translations"
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
{
|
||||
"app_title": "Title",
|
||||
"language": "Language",
|
||||
"welcome_message": "Welcome"
|
||||
}
|
||||
29
src/App.js
29
src/App.js
@@ -1,9 +1,32 @@
|
||||
import i18next from 'i18next';
|
||||
import { initReactI18next } from 'react-i18next';
|
||||
import HttpApi from 'i18next-http-backend';
|
||||
import LanguageDetector from 'i18next-browser-languagedetector';
|
||||
import Test from './components/Test';
|
||||
|
||||
i18next
|
||||
.use(HttpApi)
|
||||
.use(LanguageDetector)
|
||||
.use(initReactI18next)
|
||||
.init({
|
||||
supportedLngs: ['en' /*, 'fr'*/],
|
||||
fallbackLng: 'en',
|
||||
debug: false,
|
||||
// Options for language detector
|
||||
detection: {
|
||||
order: ['localStorage', 'sessionStorage', 'cookie', 'htmlTag'],
|
||||
caches: ['localStorage'],
|
||||
},
|
||||
// react: { useSuspense: false },
|
||||
backend: {
|
||||
loadPath: '/assets/locales/{{lng}}/translation.json',
|
||||
},
|
||||
});
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<div className="App">
|
||||
<header>
|
||||
Hey there
|
||||
</header>
|
||||
<Test />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
11
src/components/Test.js
Normal file
11
src/components/Test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
export default function Test() {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<>
|
||||
<h3>{t('app_title')}</h3>
|
||||
<div>{t('welcome_message')}</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user