ruby-rails

The preset for Ruby on Rails. It reads config/locales YAML files with their top-level locale key, t and I18n.t calls, and lazy lookups.

The ruby-rails preset supports Rails Internationalization.

Detection

The preset applies when the Gemfile has rails, rails-i18n or railties.

x18n.config.ts
import { rubyRails } from '@x18n/cli/presets'
import { defineConfig } from '@x18n/core'

export default defineConfig({
  presets: [rubyRails()],
})

Locale files

If your config has no resources, the preset uses these patterns:

  • config/locales/{locale}.yml
  • config/locales/**/*.{locale}.yml
  • config/locales/{locale}/**/*.yml

Resource transform

A Rails locale file starts with the locale code as its top-level key. The preset reads the messages under that key, so the keys do not start with the locale:

config/locales/en.yml
en:
  users:
    index:
      title: Users
  hello: Hello %{name}

This file has the keys hello and users.index.title. When x18n writes a key, it writes it under the locale key of the file. For example, setting users.index.title in de changes config/locales/de.yml to:

config/locales/de.yml
de:
  hello: Hallo
  users:
    index:
      title: Benutzer

The transform applies to every YAML resource of the project. A YAML file without the locale key of its locale has no keys.

Supported syntax

In .rb, .erb, .haml and .slim files, the preset reads t('key'), t 'key', t(:key) and I18n.t('key').

Lazy lookups

In a view under app/views/, a key that starts with . is a lazy lookup. x18n adds the path of the view to the key, as Rails does:

app/views/users/index.html.erb
<h1><%= t(".title") %></h1>
<p><%= t(:hello) %></p>

t(".title") is a usage of users.index.title, and t(:hello) is a usage of hello. x18n removes the extension (.html.erb, .haml, .slim) and the _ of a partial name, so t('.label') in app/views/users/_form.html.erb is a usage of users.form.label.

Placeholders

Percent braces: %{name}. For the files above, the placeholder-mismatch check reports hello, because Hallo has no %{name}.

Extraction templates

KindsTemplate
anyt('{key}')