This project was bootstrapped with Create React App and is fully configured with i18n (internationalization) support using react-i18next
.
Ensure you have Node.js (LTS version) installed. You can check your version with:
node -v
npm -v
If Node.js is not installed, download it from nodejs.org.
Run the following command to create a new project:
npx create-react-app my-i18n-app --template typescript
Once created, navigate into the project directory:
cd my-i18n-app
Run the following command to install the necessary dependencies:
npm install
npm install i18next react-i18next i18next-browser-languagedetector
npm install typescript@latest --save-dev
If error still persists then clean your cache
npm cache clean --force
npm install
Verify that your typescript version is greater then 5
npx tsc --version
npm start
After installing the dependencies, the project will have the following structure:
my-i18n-app/
βββ node_modules/
βββ public/
β βββ index.html
β βββ favicon.ico
βββ src/
β βββ i18n/
β β βββ en/translation.json
β β βββ fr/translation.json
β β βββ i18n.ts
β βββ App.tsx
β βββ index.tsx
β βββ react-i18next.d.ts
β βββ tsconfig.json
β βββ package.json
β βββ README.md
βββ package-lock.json
βββ tsconfig.json
βββ .gitignore
Inside src/
, create an i18n/
folder with language files.
{
"welcome": "Welcome to my app!",
"description": "This is an English description."
}
{
"welcome": "Bienvenue sur mon application!",
"description": "Ceci est une description en franΓ§ais."
}
Create the file src/i18n/i18n.ts
and add the following code:
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import translationEN from './en/translation.json';
import translationFR from './fr/translation.json';
const resources = {
en: { translation: translationEN },
fr: { translation: translationFR },
};
i18n
.use(LanguageDetector)
.use(initReactI18next)
.init({
resources,
lng: 'en',
fallbackLng: 'en',
interpolation: { escapeValue: false },
});
export default i18n;
Create the file src/react-i18next.d.ts
and add:
import "i18next";
import translation from "./i18n/en/translation.json";
declare module "i18next" {
interface CustomTypeOptions {
defaultNS: "translation";
resources: {
translation: typeof translation;
};
}
}
Modify src/App.tsx
to use the translation function:
import React from "react";
import { useTranslation } from "react-i18next";
import "./i18n/i18n"; // Ensure i18n is initialized
const App: React.FC = () => {
const { t, i18n } = useTranslation();
const changeLanguage = (lng: string) => {
i18n.changeLanguage(lng);
};
return (
<div>
<h1>{t("welcome")}</h1>
<p>{t("description")}</p>
<button onClick={() => changeLanguage("en")}>English</button>
<button onClick={() => changeLanguage("fr")}>FranΓ§ais</button>
</div>
);
};
export default App;
Ensure tsconfig.json
includes the following settings:
{
"compilerOptions": {
"target": "ESNext",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "ESNext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"]
}
npm install
npm start
Runs the app in development mode at http://localhost:3000.
npx tsc --noEmit
Ensures there are no TypeScript errors.
npm run build
-
Initially, the page should display:
Welcome to my app! This is an English description. [English] [FranΓ§ais]
-
Clicking FranΓ§ais should update the text to:
Bienvenue sur mon application! Ceci est une description en franΓ§ais.
Here is a preview of the application:
π Now your project is fully configured with TypeScript and i18n! Happy coding! π