🏁Quick start
yarn add --dev i18niftynpm install --save-dev i18niftybun add --dev i18niftypnpm add --save-dev i18niftyBefore diving into the thick of things let's make sure you can do local imports relative to your src directory. It will prevent you from having to write imports like:
import { useTranslations } from "../../../../i18n";
{
"compilerOptions": {
"target": "es5",
+ "baseUrl": "src"
// ...
}
}If you are using Vite (If you're using CRA you don't need the vite-tsconfig-paths plugin):
yarn add --dev vite-tsconfig-pathsnpm install --save-dev vite-tsconfig-pathsbun add --dev vite-tsconfig-pathspnpm add --save-dev vite-tsconfig-paths import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
import react from "@vitejs/plugin-react";
// https://vitejs.dev/config/
export default defineConfig({
"plugins": [
react(),
+ tsconfigPaths()
]
});
Start by declaring the text keys you'll need in each component.
src/components/MyComponent.tsx
src/components/MyOtherComponent.tsx
then create your src/i18n.tsx file:
Now go back to your component and use the translation function:
And so forth for your other components.
Now this setup is great if you're supporting only a few languages and you're app does not contain a lot of text. As you app grow however, you probably want to enable only only the resources for a specific language to be dowloaded.
Eslint
You should add this rule to your eslint config:
Last updated
Was this helpful?