---
title: "ruby-rails"
description: "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."
canonical_url: "https://x18n.dev/presets/ruby-rails"
---
# 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](https://guides.rubyonrails.org/i18n.html).

## Detection

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

```ts [x18n.config.ts]
import { defineConfig } from '@x18n/core'

export default defineConfig({
  presets: ['ruby-rails'],
})
```

## 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:

```yaml [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:

```yaml [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:

```erb [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`](https://x18n.dev/checks/placeholder-mismatch) check reports `hello`, because `Hallo` has no `%{name}`.

## Extraction templates

| Kinds | Template     |
| ----- | ------------ |
| any   | `t('{key}')` |


## Sitemap

See the full [sitemap](https://x18n.dev/sitemap.md) for all pages.
