i18n Locale Extractor
Prerequisites & Dependencies
- Node.js 18+ with npm or pnpm
- Mandatory packages:
npm i -g i18next-extract or npm i react-i18next i18next
- A codebase with hardcoded strings (JSX, HTML, or plain text)
- Optional:
npm i -D json5 for flexible JSON handling during extraction
Execution Steps
- Scan the codebase for hardcoded string literals: search for
\", '\, or template literals used in innerText, placeholder, alt, or render() calls
- Extract each unique string and assign a message ID (camelCase or UUID) to avoid collisions
- Group strings by namespace or component (e.g.,
login, header, footer) to produce a structured JSON tree
- Generate the initial translation file in the format expected by your i18n library:
- i18next:
{ "login": { "username": "Username", "password": "Password" } }
- react-i18next: same JSON structure, consumed via
<Translation>
- Replace the original hardcoded strings in the source code with
i18next.t('message.id') calls (or t('login.username') for namespaced access)
- Validate the extraction by building the app and checking that no missing keys appear in the console warnings
- Commit the translation JSON files (
en.json, es.json, etc.) and continue translating with your team
// Example: extracted en.json for a login form
{
"login": {
"title": "Sign in to your account",
"username": "Username",
"password": "Password",
"submit": "Log in",
"forgot_password": "Forgot password?"
},
"header": {
"logo": "Acme Corp",
"nav_home": "Home",
"nav_profile": "Profile"
}
}
// Example: replacing hardcoded strings in a React component
import React from 'react';
import i18next from 'i18next';
import { useTranslation } from 'react-i18next';
const LoginForm = () => {
const { t } = useTranslation();
return (
<form>
<h2>{t('login.title')}</h2>
<div>
<label>{t('login.username')}</label>
<input type="text" placeholder={t('login.username')} />
</div>
<div>
<label>{t('login.password')}</label>
<input type="password" placeholder={t('login.password')} />
</div>
<button type="submit">{t('login.submit')}</button>
<a href="/forgot">{t('login.forgot_password')}</a>
</form>
);
};
export default LoginForm;
// Initialization (usually in entry point)
i18next.init({
lng: 'en',
resources: {
en: { translation: require('./locales/en.json') },
// ... other locales
},
fallbackLng: 'en',
});
# Extract strings using i18next-extract CLI
i18next-extract -p i18next-extract-defaults.js
# Generate missing locale files
cpLocales/en.json locales/es.json
1---2name: i18n-locale-extractor3description: Extract hardcoded UI text strings into structured internationalization JSON translation files.4---56# i18n Locale Extractor78## Prerequisites & Dependencies9- Node.js 18+ with npm or pnpm10- Mandatory packages: `npm i -g i18next-extract` or `npm i react-i18next i18next`11- A codebase with hardcoded strings (JSX, HTML, or plain text)12- Optional: `npm i -D json5` for flexible JSON handling during extraction1314## Execution Steps151. Scan the codebase for hardcoded string literals: search for `\"`, `'\`, or template literals used in `innerText`, `placeholder`, `alt`, or `render()` calls162. Extract each unique string and assign a message ID (camelCase or UUID) to avoid collisions173. Group strings by namespace or component (e.g., `login`, `header`, `footer`) to produce a structured JSON tree184. Generate the initial translation file in the format expected by your i18n library:19 - **i18next**: `{ "login": { "username": "Username", "password": "Password" } }`20 - **react-i18next**: same JSON structure, consumed via `<Translation>`215. Replace the original hardcoded strings in the source code with `i18next.t('message.id')` calls (or `t('login.username')` for namespaced access)226. Validate the extraction by building the app and checking that no missing keys appear in the console warnings237. Commit the translation JSON files (`en.json`, `es.json`, etc.) and continue translating with your team2425```json26// Example: extracted en.json for a login form27{28 "login": {29 "title": "Sign in to your account",30 "username": "Username",31 "password": "Password",32 "submit": "Log in",33 "forgot_password": "Forgot password?"34 },35 "header": {36 "logo": "Acme Corp",37 "nav_home": "Home",38 "nav_profile": "Profile"39 }40}41```4243```javascript44// Example: replacing hardcoded strings in a React component45import React from 'react';46import i18next from 'i18next';47import { useTranslation } from 'react-i18next';4849const LoginForm = () => {50 const { t } = useTranslation();5152 return (53 <form>54 <h2>{t('login.title')}</h2>55 <div>56 <label>{t('login.username')}</label>57 <input type="text" placeholder={t('login.username')} />58 </div>59 <div>60 <label>{t('login.password')}</label>61 <input type="password" placeholder={t('login.password')} />62 </div>63 <button type="submit">{t('login.submit')}</button>64 <a href="/forgot">{t('login.forgot_password')}</a>65 </form>66 );67};6869export default LoginForm;7071// Initialization (usually in entry point)72i18next.init({73 lng: 'en',74 resources: {75 en: { translation: require('./locales/en.json') },76 // ... other locales77 },78 fallbackLng: 'en',79});80```8182```bash83# Extract strings using i18next-extract CLI84i18next-extract -p i18next-extract-defaults.js8586# Generate missing locale files87cpLocales/en.json locales/es.json88```