Docs / Ruby on Rails
Rails i18n for ERB views, automated
Move hardcoded copy in Rails ERB views into t() calls with config/locales YAML, plurals with count:, available locales and a locale switch, in one command or pull request.
How it works
Text in ERB views becomes <%= t('key', name: @user.name) %>, with %{name} placeholders in config/locales/<locale>.yml, under the language key. Logic tags split sentences; <script> and <code> are left alone.
A ternary on a count becomes a plural with one: and other: forms (and whatever else each language needs), called with count:.
Before and after
The engine’s real output for this file. In a real run, the hosted model names the keys from context.
<h1>Welcome back, <%= @user.name %>!</h1>
<% if @items.any? %>
<p><%= @items.count %> <%= @items.count == 1 ? 'item' : 'items' %> in your cart</p>
<% end %>
<input placeholder="Search products" type="text"><h1><%= t('show.welcome_back', name: @user.name) %></h1>
<% if @items.any? %>
<p><%= t('show.items_in_your_cart', count: @items.count) %></p>
<% end %>
<input placeholder="<%= t('show.search_products') %>" type="text">en:
show:
welcome_back: Welcome back, %{name}!
items_in_your_cart:
one: "%{count} item in your cart"
other: "%{count} items in your cart"
search_products: Search productsWhat the setup adds
The first run sets up Rails I18n if the project doesn’t have it yet:
config.i18n.available_localesinconfig/application.rb- A
switch_localearound-action inApplicationController:?locale=, then alangcookie, then Accept-Language
Run it
The CLI needs Node.js 22 or later, nothing else: npx runs it without adding a package.json to the project. No Node on your machine? The GitHub Action below runs it for you.
npx localize-dev login
# Preview, then write the changes
npx localize-dev run --dry-run -l es,fr,de
npx localize-dev run -l es,fr,de
# Or open a pull request instead
npx localize-dev pr -l es,fr,deOr add the GitHub Action and get a pull request on every push. Try it on your own code first in the playground.