LoginSignup
60
63

More than 5 years have passed since last update.

i18n-tasks | Rails のロケールファイルを整理整頓する

Posted at

i18n-tasks は、Rails で利用しているロケールファイルで不足している翻訳や未使用の翻訳を検出してくれる便利な Gem です。

リソース

インストール

Gemfilei18n-tasks を追記します。

Gemfile
group :development do
  gem 'i18n-tasks'
end

インストールします。

console
$ bundle install

利用方法

ヘルプを表示する

console
$ i18n-tasks

Usage: i18n-tasks [command] [options]
    -v, --version      Print the version
    -h, --help         Display this help message.

Available commands:

  missing             show missing translations
  unused              show unused translations
  translate-missing   translate missing keys with Google Translate
  add-missing         add missing keys to the locales
  find                show where the keys are used in the code
  normalize           normalize translation data: sort and move to the right files
  remove-unused       remove unused keys
  config              display i18n-tasks configuration
  xlsx-report         save missing and unused translations to an Excel file

See `<command> --help` for more information on a specific command.

不足している翻訳を検出する: i18n-tasks missing

ビューで i18n の読み込みを定義しているにも関わらず、ロケールファイルには、まだ定義されていない翻訳を検出します。

console
$ i18n-tasks missing

Missing translations (1) | i18n-tasks v0.3.9
Types: ✗ missing from base locale, ∅ missing from locale but present in base locale, = value equals base value
+--------+------+------------------+-----------------+
| Locale | Type | i18n Key         | Base value (en) |
+--------+------+------------------+-----------------+
|   en   |  ✗   | sites.index.desc |                 |
+--------+------+------------------+-----------------+

ビューで sites.index.desc というキーで i18n の読み込みを定義しているにも関わらず、ロケールファイルには、まだ定義されていないということが分かります。

特定の翻訳が使用されているビューを検出する: i18n-tasks find --p [pattern]

特定の翻訳がどのビューのどこで使用されているか検出します。
検出したい翻訳のキーをパラメータとして渡します。

console
$ i18n-tasks find --p sites.index.desc

1 key ~ 'sites.index.desc' (1 usage) | i18n-tasks v0.3.9
sites.index.desc
  app/views/sites/index.html.erb:2 <p><%= t('.desc') %></p>

app/views/sites/index.html.erb の2行目で使用されていることが分かります。

未使用の翻訳を検出する: i18n-tasks unused

ロケールファイルに定義されているにも関わらず、ビューで使用されていない翻訳を検出します。

console
$ i18n-tasks unused

Unused keys (1) | i18n-tasks v0.3.9
+-------------------------+-------------------------------------------+
| i18n Key                | Base value (en)                           |
+-------------------------+-------------------------------------------+
| sites.index.description | Find me in app/views/sites/index.html.erb |
+-------------------------+-------------------------------------------+

sites.index.description というキーがロケールファイルに定義されているにも関わらず、使用されるはずである app/views/sites/index.html.erb で未使用となっていることが分かります。

その他

  • 不足している翻訳を自動的にロケールファイルに追加する: i18n-tasks add-missing
  • 未使用の翻訳を自動的にロケールファイルから削除する: i18n-tasks remove-unused
  • 定義されている翻訳をキーでソートしたり、適切な位置へ移動する: i18n-tasks normalize

などなど、便利そうなコマンドが用意されていますが、これらはロケールファイルの上書きを行うのと、強制的にキーのソートがされてしまうので注意が必要そうです。

60
63
4

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
60
63