miyohideの日記

技術的なメモなどを記しています

2023年4月16日 Railsのi18n

Railsi18n

Railsアプリに対してRubocopをかけたところ、以下の警告が出た。

app/controllers/todos_controller.rb:45:60: C: Rails/I18nLocaleTexts: Move locale texts to the locale files in the config/locales directory.
        format.html { redirect_to todo_url(@todo), notice: 'Todo was successfully updated.' }

これの対処はRailsi18n化。

railsguides.jp

以下のようなYAMLファイルをconfig/locales/en.ymlとして作って...

en:
  todos:
    create:
      success: 'Todo was successfully created.'
    update:
      success: 'Todo was successfully updated.'
    destroy:
      success: 'Todo was successfully destroy.'

controllerにて、以下のように記述する。

format.html { redirect_to todo_url(@todo), notice: t('.success') }