🏁Quick start

This project is designed to be used in SPAs (Single page applications) with no server side rendering.

If you're not using either Vite or Create-React-App, i18nifty is probably not the best choice for you.

yarn add --dev i18nifty

Before 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";

tsconfig.json
 {
     "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-paths
vite.config.ts
 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:

Asynchronous locale resources download

Last updated

Was this helpful?