# How to use i18n in a page
Materialize admin provides i18n for navigation only. There might be a time when you might want to translate any text in a page. Here's how you can achieve that:
- Make sure your have
i18next
,react-i18next
,i18next-http-backend
andi18next-browser-languagedetector
packages installed - Copy the
src/configs/i18n.ts
file from the full version and paste it under the same directory in your project - Add
import 'src/configs/i18n'
import statement in thesrc/pages/_app.tsx
file - Create a
locales
folder in thepublic
folder - Create JSON files with language as their file names in
locales
depending on the languages you need. For example, we're using three languages English, French & Arabic
// en.json
{
"Hello World": "Hello World"
}
// fr.json
{
"Hello World": "Bonjour le monde"
}
// ar.json
{
"Hello World": "مرحبا بالعالم"
}
- Import
useTranslation
in the page where you want to translate the content
import { useTranslation } from 'react-i18next'
- You can now initialize the
useTranslation
and use thechangeLanguage
function to change the language
import { useTranslation } from 'react-i18next'
const Component = () => {
const { t, i18n } = useTranslation()
return (
<div>
<h1>{t('Hello World')}</h1>
<button onClick={() => i18n.changeLanguage('fr')}>Translate</button>
</div>
)
}
export default Component