i18nmatic
"The fastest way to implement internationalization"
i18nmatic is a CLI tool that automates code transformation and translation key extraction, allowing you to quickly and efficiently implement internationalization (i18n) in React and Next.js projects. Quickly apply internationalization to validate your ideas and business in the global market!
Why?
To expand your business and seize global opportunities, internationalization (i18n) is no longer optionalβit's essential.
React and Next.js projects typically use libraries such as react-i18next or next-i18next to handle multilingual content. However, wrapping every text manually in t() functions and managing extracted translation keys in JSON files is tedious, repetitive, and resource-intensive, especially in large codebases.
i18nmatic automates these repetitive tasks, enabling developers to focus on higher-level problems and empowering your business to quickly validate ideas in the global market.
πΊ Demo
See how quickly i18nmatic transforms your code and extracts translation keys.
Key Features
- Automatic code transformation: Detects all text requiring internationalization in JSX, string literals, template literals, etc. Automatically extracts translation keys based on the selected language, wraps them with
t(), and injects the necessary imports. - Translation key extraction: Extracts all text requiring translationβeven if not yet wrapped with t()βand outputs keys with source file paths into JSON, enabling efficient management and traceability.
- Multilingual support: Supports major languages including Korean, English, Japanese, and Chinese.
- React/Next.js compatibility: Fully compatible with
react-i18nextandnext-i18next.
Installation
npm install -D i18nmatic
# or
yarn add -D i18nmatic
Usage
1. Create a configuration file
Create an auto-i18n.config.json file in your project's root directory (Click here to see the default configuration options):
{
"runType": "next", // Choose between "next" or "react"
// - "next": Use for Next.js projects
// - "react": Use for React projects
"entry": "src", // Root directory of your source code
// - Example: "src" targets all files in the src directory
"locales": ["en", "ja-JP"], // Array of locale codes to support
// - Example: ["en", "ja-JP"] supports English and Japanese
// - JSON files are generated separately per language
"outputDir": "public/locales", // Directory to store generated translation JSON files
// - Example: "public/locales" is compatible with Next.js static paths
"enablePrettier": true, // Whether to format generated code and JSON files using Prettier
// - true: Use Prettier formatting
// - false: Save original formatting
"outputFileName": "common.json", // Name of the generated translation JSON file
// - Example: "common.json" is consistent across languages
"keyLanguage": "ko" // Base language to extract translation keys
// - Example: "ko" extracts Korean text as translation keys
// - Supported values: "ko", "en", "ja", "zh", etc.
}
2. Run CLI
Execute the following command to transform code and extract translation keys:
npx auto-i18n
Or add a script to package.json:
"scripts": {
"auto-i18n": "auto-i18n"
}
Then run:
npm run auto-i18n
# or
yarn auto-i18n
3. Transformation results
Before:
function Greeting() {
return <div>μλ
νμΈμ</div>;
}
After:
import { useTranslation } from "next-i18next";
function Greeting() {
const { t } = useTranslation();
return <div>{t("μλ
νμΈμ")}</div>;
}
Extracted JSON keys (public/locales/en/common.json):
{
"μλ
νμΈμ": "μλ
νμΈμ"
}
Examples
Input Code (Before Transformation)
// ν
νλ¦Ώ 리ν°λ΄
function TemplateLiteralComponent({ name }) {
return <p>{`${name}λ μλ
νμΈμ`}</p>;
}
// JSX μμ±
function JSXAttributeComponent() {
return <input type="text" placeholder="μλ
νμΈμ μ¬κΈ°μ μ
λ ₯ν΄ μ£ΌμΈμ" />;
}
Transformed Code (After Transformation)
import { useTranslation } from "next-i18next";
function TemplateLiteralComponent({ name }) {
const { t } = useTranslation();
return <p>{t("{{name}}λ μλ
νμΈμ", { name })}</p>;
}
function JSXAttributeComponent() {
const { t } = useTranslation();
return <input type="text" placeholder={t("μλ
νμΈμ μ¬κΈ°μ μ
λ ₯ν΄ μ£ΌμΈμ")} />;
}
Extracted JSON File (public/locales/{locale}/common.json)
{
"{{name}}λ μλ
νμΈμ": "{{name}}λ μλ
νμΈμ",
"μλ
νμΈμ μ¬κΈ°μ μ
λ ₯ν΄ μ£ΌμΈμ": "μλ
νμΈμ μ¬κΈ°μ μ
λ ₯ν΄ μ£ΌμΈμ"
}
When Automatic Wrapping is Difficult
In certain scenarios, as shown below, it's difficult for the tool to automatically determine whether the attributes should be wrapped with the t() function, due to the lack of explicit context within the code itself.
However, internationalization is still essential in these cases. To handle such scenarios, i18nmatic detects these texts, extracts them into JSON files, and includes a comment with the original source file path. This makes it easy for developers to manually locate and wrap the keys with t().
Example Input Code
// src/components/example.tsx
const ITEMS = [
{
id: 1,
title: 'μλ
νμΈμ',
description: 'λ°κ°μ΅λλ€.',
},
{
id: 2,
title: 'μλΆνλ립λλ€.',
description: 'κ³ λ§μ΅λλ€.',
},
{
id: 3,
title: 'λ―Έμν©λλ€.',
description: 'κ°μ¬ν©λλ€.',
},
];
function Example() {
return (
<>
{ITEMS.map((item) => (
<div key={item.title}>
<h1>{item.title}</h1>
<p>{item.description}</p>
</div>
))}
</>
);
}
Extracted JSON File Example (public/locales/{locale}/common.json)
{
...
"__comment_1": "src/components/example.tsx/ITEMS",
"λ°κ°μ΅λλ€.": "λ°κ°μ΅λλ€.",
"κ³ λ§μ΅λλ€.": "κ³ λ§μ΅λλ€.",
"μλΆνλ립λλ€.": "μλΆνλ립λλ€.",
"κ°μ¬ν©λλ€.": "κ°μ¬ν©λλ€.",
"λ―Έμν©λλ€.": "λ―Έμν©λλ€.",
...
}
Supported Patterns
- JSX text:
<div>μλ νμΈμ</div>β<div>{t("μλ νμΈμ")}</div> - String literals:
const greeting = "μλ νμΈμ";βconst greeting = t("μλ νμΈμ"); - Template literals:
const message =${name}λ μλ νμΈμ;βconst message = t("{{name}}λ μλ νμΈμ", { name }); - JSX attributes:
<input placeholder="μλ νμΈμ" />β<input placeholder={t("μλ νμΈμ")} /> - Conditional expressions:
isKorean ? "μλ νμΈμ" : "Hello"βisKorean ? t("μλ νμΈμ") : t("Hello")
π Configuration (auto-i18n.config.json)
| Option | Type | Default | Description |
|---|---|---|---|
runType |
"next" | "react" |
"next" |
Framework type used in your project. |
entry |
string |
"src" |
Root directory for your source code. |
locales |
string[] |
["ja_JP"] |
Supported locale codes (e.g., ["en", "ja-JP"]). |
outputDir |
string |
"public/locales" |
Directory for generated translation JSON files. |
enablePrettier |
boolean |
true |
Format output using Prettier. |
outputFileName |
string |
"common.json" |
Filename for generated translation files. |
keyLanguage |
"ko" | "en" | "ja" | "zh" |
"ko" |
Base language for extracting translation keys. |
Testing
This project uses Jest for testing. To run tests:
npm test
Contributing
Contributions are always welcome! Please follow these steps:
- Fork this repository.
- Create a new branch:
git checkout -b feature/my-feature - Ensure all existing tests pass, and add relevant tests for your changes.
- Commit your changes:
git commit -m "Add my feature" - Push to your branch:
git push origin feature/my-feature - Create a Pull Request.
License
This project is licensed under the MIT License.
Contact
If you have questions or issues, please open a GitHub issue.