Translate Rails YAML locale files
Fill in config/locales/<lang>.yml for Rails and other YAML dictionaries, with %{name} interpolation kept and one/other plural maps expanded to each language’s forms.
How it works
localize-dev translate reads config/locales/en.yml, and files like devise.en.yml beside it, then writes the missing keys into each language’s file under its own locale key. A language with no file yet gets one: devise.en.yml gets devise.pl.yml.
Rails %{name} and %<name>s, i18n-js {{name}} and printf placeholders are kept. A map of plural categories (one:, other:) is treated as one message and written with the categories the target language uses.
Before and after
Real output of localize-dev translate --locales pl on the first file. The items plural map gets Polish’s few and many, and values that start with % stay quoted.
en:
orders:
title: "Your orders"
shipped: "Shipped to %{city} on %{date}"
items:
one: "%{count} item"
other: "%{count} items"pl:
orders:
title: Twoje zamówienia
shipped: Wysłano do %{city} dnia %{date}
items:
one: "%{count} przedmiot"
few: "%{count} przedmioty"
many: "%{count} przedmiotów"
other: "%{count} przedmiotów"What stays intact
%{name}interpolation, checked after translation- Plural maps, with
few:andmany:added where the language needs them - The nesting of the source file, under the target’s locale key
- Valid YAML: values are quoted where YAML needs it
Common mistakes, and how they’re avoided
Plural maps with only one and other
Rails picks the plural key with the language’s rules. If pl.yml only has one and other, counts like 3 or 25 have no form of their own. Every category the language uses is written.
Translated interpolation names
%{city} has to stay %{city}, or Rails raises a missing interpolation error at runtime. Placeholders are masked before translation and checked after it.
Broken quoting
A value that starts with % or contains : needs quotes, and a hand-edited translation easily loses them. The file is written by a YAML library, so quoting follows YAML’s rules.
Run it
In the folder that holds your locale files. Languages without a file yet get one. --dry-run shows what would change first.
npx localize-dev login
# Preview, then write the missing translations
npx localize-dev translate --dry-run -l es,fr,de
npx localize-dev translate -l es,fr,deThen keep it complete in CI. This needs no API key and fails when a language is missing translations:
npx localize-dev check --only translationsFree for 1,000 strings a month. A string counts once however many languages it goes into. Create a free account
Guides for apps that use this format
When the copy is still hardcoded in your code, localize-dev run moves it into this format first.