Home
Softono
i18nmatic

i18nmatic

Open source TypeScript
110
Stars
1
Forks
0
Issues
1
Watchers
1 year
Last Commit

About i18nmatic

CLI tool to automate internationalization for React & Next.js projects

Platforms

Web Self-hosted

Languages

TypeScript

Links

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

Watch the 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-i18next and next-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:

  1. Fork this repository.
  2. Create a new branch: git checkout -b feature/my-feature
  3. Ensure all existing tests pass, and add relevant tests for your changes.
  4. Commit your changes: git commit -m "Add my feature"
  5. Push to your branch: git push origin feature/my-feature
  6. 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.