Rails の view では、相対パス(キー)のロケールが使える

Published: 2023/9/7


https://api.rubyonrails.org/classes/ActionView/Helpers/TranslationHelper.html#method-i-translate

上記 Rails API の資料に記載があるが、 view の中では実は t('.title') みたいな形で、相対パス形式でロケールのキーを指定できる。 これは、例えば render しているファイルが users/index.html である場合、 t('users.index.title') したのとまったく等価な記述となる。

具体的には上記の資料で解説される、 ActionView::Helpers::TranslationHelper において、そのような処理がはさみこまれている。

https://github.com/rails/rails/blob/3668b4b5978822f1e6311c7fd7a32f58daee136a/actionview/lib/action_view/helpers/translation_helper.rb#L89


Tags: rails

関連記事