Language Codes in Translation Files — i18n File Organization
Best practices for naming and organizing translation files using language codes, covering JSON, YAML, PO, and XLIFF formats.
Detailed Explanation
Translation File Organization
How you name and organize translation files directly impacts developer experience and maintainability. Language codes serve as the primary key for file naming and directory structure.
Common File Naming Patterns
Flat Files (one file per locale)
locales/
├── en.json
├── ja.json
├── de.json
├── zh-Hans.json
└── pt-BR.json
Namespaced Files (split by feature)
locales/
├── en/
│ ├── common.json
│ ├── auth.json
│ └── dashboard.json
├── ja/
│ ├── common.json
│ ├── auth.json
│ └── dashboard.json
Translation File Formats
JSON (most popular for web)
{
"greeting": "Hello, {{name}}!",
"items_count": {
"one": "{{count}} item",
"other": "{{count}} items"
}
}
YAML (Ruby on Rails convention)
ja:
greeting: "こんにちは、{{name}}さん!"
items_count:
one: "{{count}}件のアイテム"
other: "{{count}}件のアイテム"
GNU gettext PO
# Japanese translation
msgid "Hello, %s!"
msgstr "こんにちは、%sさん!"
msgid "%d item"
msgid_plural "%d items"
msgstr[0] "%d件のアイテム"
Popular i18n Libraries and Their Code Conventions
| Library | Platform | Locale Format | File Pattern |
|---|---|---|---|
| i18next | JS/TS | BCP 47 | {lng}/{ns}.json |
| react-intl | React | BCP 47 | {locale}.json |
| vue-i18n | Vue | BCP 47 | {locale}.json |
| next-intl | Next.js | BCP 47 | messages/{locale}.json |
| Rails I18n | Ruby | ISO 639-1 | {locale}.yml |
| Django | Python | POSIX | {locale}/LC_MESSAGES/*.po |
| Flutter ARB | Dart | BCP 47 | app_{locale}.arb |
Best Practices
- Use BCP 47 tags for file/directory names (
zh-Hans.json, notzh_CN.json) - Keep a canonical source locale (usually
en.json) as the reference - Use ICU MessageFormat for plurals and interpolation
- Extract strings from code using tools like
i18next-parserorformatjs extract - Never hardcode locale lists — derive them from the file system
- Include context comments for translators (many formats support comments)
- Validate all files have the same keys on CI
Use Case
Every internationalized application needs a consistent translation file strategy. Whether you are using i18next, react-intl, Rails I18n, or Django's gettext, the choice of language code format and file organization pattern affects team productivity, translation vendor workflows, and build pipeline efficiency.
Try It — Language Code Reference
Related Topics
BCP 47 Language Tags — The Web Standard for Locale Identifiers
Standards
ISO 639-1 Overview — Two-Letter Language Codes
Standards
Locale Negotiation in Web Apps — Choosing the Right Language
Web Development
Language Codes in Content Management — CMS Localization
Internationalization
Intl API Locale Codes — JavaScript Internationalization
Web Development