Written by Martin Chrástek.
In Spanish, "post" the noun is publicación and "post" the verb is publicar. Name your key common.post, use it for both a blog post title and a "Post" button, and one of them ships with the wrong word.
That's the thing about translation keys. They look like a naming detail, but for a translator they're often the only context there is. A good key says where the text lives and what it means. A bad one makes them guess.
Here are the six rules we recommend for naming translation keys.
TL;DR
-
Be descriptive.
common.modal.save_changes.header, notkey1. -
Be unambiguous. One meaning per key:
form.name_personandform.name_company, notform.name1andform.name2. -
Don't abbreviate.
registration_form.label.first_name, notrg_frm_fnam_lb. -
Pick one convention and stick to it. In these examples, dot mark hierarchy and
snake_caseis used inside each segment. - Keep common only for repeated strings with the same meaning. Avoid one-off strings and ambiguous words like common.post.
- Nest by the most stable useful location, such as a page, flow, or shared component, and keep it shallow.
What a translation key is
A translation key is a unique ID that points to a piece of text your app shows. Instead of hardcoding the text in a component, you reference a key, and your i18n library swaps in the right language at runtime.
// Instead of this
<h1>Hello world!</h1>
// you write this
<h1>{t('homepage.welcome_message.title')}</h1>
The text lives in your translation files. The key is the only thing connecting the two, and for the person translating it, it's often the only hint about where the text appears.
Rule 1: Use descriptive names
A key should tell you what the text is and where it lives.
| Text | Bad | Good |
|---|---|---|
| Save Changes | key1 |
common.modal.save_changes.header |
| Learn More | link-text |
blog.cta.learn_more |
Rule 2: Use unambiguous names
The keys form.name1 and form.name2 do not show whether “name” means a person’s name or a company’s name. The more descriptive keys form.name_person and form.name_company make the distinction clear.
| Text | Bad | Good |
|---|---|---|
| Name | form.name1 |
form.name_person |
| Name | form.name2 |
form.name_company |
If one English word can mean two things, give it two keys.
Rule 3: Don't abbreviate
| Text | Bad | Good |
|---|---|---|
| First Name | rg_frm_fnam_lb |
registration_form.label.first_name |
| Last Name | rg_frm_lnam_lb |
registration_form.label.last_name |
Abbreviations and poorly named keys can make it harder to understand what a key represents, leading to confusion and errors.
Rule 4: Pick one convention and stick to it
Use a consistent naming convention that is easy to understand and follow. Casing options include camelCase, snake_case, and PascalCase.
| Text | Inconsistent | Consistent |
|---|---|---|
| Log in | login_text |
homepage.header.button.log_in |
| Sign up | sign_up |
homepage.header.button.sign_up |
| Blog | header_button_blog |
homepage.header.link_label.blog |
Rule 5: Keep common for strings that are really shared
common is for text that appears in many places and means the same thing everywhere. It goes wrong in two directions.
| Problem | Key | Why |
|---|---|---|
| Too specific | common.account.mfa_status_disabled |
Used in one place, so it isn't common |
| Too vague | common.post |
Noun or verb? In Spanish, that's publicación _or _publicar |
Good candidates for common:
common.form.button.cancelcommon.label.creditscommon.pricing_plan.standard
Rule 6: Nest by where the text lives
Nesting groups related strings, so you and your translators can find them. As a project grows, namespaces and tags help too (we wrote a separate guide to organizing translation keys).
Before, a flat file where every key is a guess:
{
"title": "Welcome!",
"title2": "Something went wrong",
"btn": "Contact us",
"footer_txt": "Email us at hello@example.com"
}
After, nested by where each string lives:
{
"header": {
"title": "Welcome!"
},
"error": {
"generic": "Something went wrong"
},
"footer": {
"contact_info": {
"text": "Email us at hello@example.com",
"button": "Contact us"
}
}
}
Tolgee tip: if you use . as the separator in your key names, Tolgee can export them as nested JSON like the example above. See the export docs.
Your turn
Do you use structured keys like homepage.header.button.log_in, or the English text itself as the key? And what's the worst key name you've found in a codebase?
If you're working on localization, Tolgee is open source, and a star on GitHub helps us a lot. You can also come talk i18n with us on Slack.
Top comments (0)