Added: i18next logic

This commit is contained in:
Llewellyn Dsouza
2022-01-05 17:42:28 +05:30
parent 65478a1622
commit 3af4fae1c5
4 changed files with 42 additions and 8 deletions

View File

@@ -0,0 +1,5 @@
{
"app_title": "Hey there",
"language": "Language",
"welcome_message": "Welcome with translations"
}

View File

@@ -1,5 +0,0 @@
{
"app_title": "Title",
"language": "Language",
"welcome_message": "Welcome"
}

View File

@@ -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
View 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>
</>
);
}