6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

i18nのキーを調べる方法(nestされているパターン等)

Posted at

nestした場合のキーってどうなるのだろうか?ということで色々ググったりしますが、そんなことよりメソッドから探したほうが早かったです。

class WorkForm::CastForm
  include ActiveModel::Model

  attr_accessor :name
end

このパターンです。

i18n_scope

WorkForm::CastForm.i18n_scope
=> :activemodel

これでactivemodelに書けばいいことがわかる。

model_name

WorkForm::CastForm.model_name
=> #<ActiveModel::Name:0x007f86a2093c30
 @collection="work_form/cast_forms",
 @element="cast_form",
 @human="Cast form",
 @i18n_key=:"work_form/cast_form",
 @klass=WorkForm::CastForm,
 @name="WorkForm::CastForm",
 @param_key="work_form_cast_form",
 @plural="work_form_cast_forms",
 @route_key="work_form_cast_forms",
 @singular="work_form_cast_form",
 @singular_route_key="work_form_cast_form">

情報が列挙される。

ここで必要なのはi18n_keyなので、こうする。

WorkForm::CastForm.model_name.i18n_key
=> :"work_form/cast_form"

こう書けばいいことがわかる

i18nのファイルの中

ja:
  activemodel:
    attributes:
      work_form/cast_form:
        name: "qiita"


WorkForm::CastForm.human_attribute_name("name")
=> "qiita"

以上です。

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?