Adding i18n to the Starter-Kit
Our template supports Right-to-Left (RTL) languages like Arabic and Left-to-Right (LTR) languages like English. By adding i18n, you enable the template to automatically switch between RTL and LTR layouts based on the selected language.
-
Add the following dependency to your project:
Begin by installing packages related to i18n, which handle locale matching and negotiation.
@formatjs/intl-localematcher@types/negotiatornegotiator
Use the following command to add these dependencies:
- pnpm
- yarn
- npm
pnpm install @formatjs/intl-localematcher @types/negotiator negotiatoryarn add @formatjs/intl-localematcher @types/negotiator negotiatornpm install @formatjs/intl-localematcher @types/negotiator negotiator -
In
src/app, create a folder named[lang]. Move all content fromsrc/apptosrc/app/[lang], excludingsrc/app/api,favicon, andglobal.css. Update path references in the moved files. -
Add the following files from the full-version to your project:
src/configs/i18n.tssrc/utils/i18n.tssrc/hocs/TranslationWrapper.tsxsrc/components/LangRedirect.tsx
-
In
src/data, create adictionariesfolder and create a json file for each language in thedictionariesfolder. For example,en.json,fr.json, andar.json. -
For example in
en.jsonfile add the following code:{
"navigation": {
"home": "Home",
"about": "About"
}
}Similarly, you can add the dictionary for other languages as well.
-
Add the
src/utils/getDictionary.tsfile from the full-version to your project. Make sure to update the languages as per your requirement. -
Go to the
src/app/[lang]/layout.tsxfile and make the following changes.-
Update the following code
+ import { i18n } from '@configs/i18n'
+ import { headers } from 'next/headers'
+ import type { Locale } from '@configs/i18n'
- { children }: ChildrenType
+ props: ChildrenType & { params: Promise<{ lang: string }> }
+ const params = await props.params
+ const { children } = props
+ const headersList = await headers()
+const lang: Locale = i18n.locales.includes(params.lang as Locale) ? (params.lang as Locale) : i18n.defaultLocale
- const direction = 'ltr'
+ const direction = i18n.langDirection[lang]
- lang='en'
+ lang={lang}Make the above changes in the files where you want to use
directionWrap the
htmltag with the<TranslationWrapper headersList={headersList} lang={lang}>component.
-
-
Go to the
src/app/[lang]/(dashboard)/layout.tsxfile and make the following changes.-
Add the only direction changes as we did in the
src/app/[lang]/layout.tsxfile. -
Additionally, add the following statement:
+ import { getDictionary } from '@/utils/getDictionary'
+ const dictionary = await getDictionary(lang) -
Add the
dictionary={dictionary}prop to the<Navigation />and<Header />components.
-
-
Go to the
src/components/layout/vertical/Navigation.tsxfile and make the following changes.-
Add the following in props and get the dictionary from props.
// If you have used props
+ import type { getDictionary } from '@/utils/getDictionary'
type Props = {
...
+ dictionary: Awaited<ReturnType<typeof getDictionary>>
}
// get the dictionary from props
+ const { dictionary } = props // get the dictionary from destructuring propsor
+ import type { getDictionary } from '@/utils/get-dictionary'
// Direct passing the parameter
+ { dictionary }: { dictionary: Awaited<ReturnType<typeof getDictionary>> }You have to make the changes mentioned above in the following files as well:
-
src/components/layout/vertical/VerticalMenu.tsx -
src/components/layout/horizontal/Header.tsx -
src/components/layout/horizontal/Navigation.tsx -
src/components/layout/horizontal/HorizontalMenu.tsx
-
-
-
pass the
dictionary={dictionary}prop to the in the following files:-
src/components/layout/vertical/Navigation.tsxfile in the<VerticalMenu>component. -
src/components/layout/horizontal/Navigation.tsxfile in the<HorizontalMenu>component. -
src/components/layout/horizontal/Header.tsxfile in the<Navigation>component.
-
-
Make the following changes in
src/components/layout/vertical/VerticalMenu.tsxandsrc/components/layout/horizontal/HorizontalMenu.tsxfiles.-
Add the following code:
// Next Imports
+ import { useParams } from 'next/navigation'
// Hooks
+ const params = useParams()
+ const { lang: locale } = params -
Replace the following statement:
- <MenuItem href='/home' icon={<i className='ri-home-smile-line' />}>
- Home
- </MenuItem>
- <MenuItem href='/about' icon={<i className='ri-information-line' />}>
- About
- </MenuItem>
+ <MenuItem href={`/${locale}/home`} icon={<i className='ri-home-smile-line' />}>
+ {dictionary['navigation'].home}
+ </MenuItem>
+ <MenuItem href={`/${locale}/about`} icon={<i className='ri-information-line' />}>
+ {dictionary['navigation'].about}
+ </MenuItem>
-
-
If you want to add a language dropdown in your project, then you may copy the
LanguageDropdown.tsxfile from the full-version of the template and modify as per your requirement. The file is located atsrc/components/layout/shared/LanguageDropdown.tsx. -
You need to import this component in the
src/components/layout/vertical/NavbarContent.tsxandsrc/components/layout/horizontal/NavbarContent.tsxfiles and add above theUserDropdowncomponent. -
Copy the
redirectsfrom thenext.config.mjsfile from the full-version of the template to your project'snext.config.mjsfile and remove thefront-pagesfrom thesource. Also update the destination path as per your requirement.
After completing these steps, if you encounter any type errors, try refreshing/reopening your editor or deleting the .next folder before running your project again.
Congratulations! You've successfully added i18n to your Next.js project. 🥳🎉