LoginSignup
44
43

More than 5 years have passed since last update.

i18nのtranslation missingをテストする

Last updated at Posted at 2014-06-22

i18n対応したサービスを提供していると、i18nのdictが一部抜けていたり、デザイン崩れているじゃんって事が多々有りますよね?

やりたい事

で、そういうのをデプロイ前に未然に防ぎたいと思ってcontroller specにて、translation_missingが含まれているかとかチェックしていたけど、複数人で書いていると中々網羅性に掛けたりして困ってました。

そういうときに便利なのがi18n-task gem

インストール

gemいれて

$ vim Gemfile
gem 'i18n-tasks', '~> 0.4.5' 

$ bundle

設定

デフォルトの言語はja、テスト対象はja, enにしたいと思います。

$ i18n-tasks config > config/i18n-tasks.yml
$ vim config/i18n-tasks.yml
base_locale: ja
locales: [ja, en]

# dictを分割している場合は、それぞれ読み込んじゃいます。
data:
  read: 
    - 'config/locales/**/*%{locale}.yml'

実行

# translation missing
$ i18n-tasks missing

# unused
$ i18n-tasks unused

さらに使っていない物をremoveしたり、検索対象やremoveに際しignoreする事もconfigurationできます。

rspecにインテグレーション

spec/view/translation/i18n_spec.rbを追加することで、CIでちゃんと試験してくれるようになります。

require 'spec_helper'
require 'i18n/tasks'

describe 'I18n' do
  let(:i18n) { I18n::Tasks::BaseTask.new }

  it 'does not have missing keys' do
    count = i18n.missing_keys.count
    fail "There are #{count} missing i18n keys! Run 'i18n-tasks missing' for more details." unless count.zero?
  end

  it 'does not have unused keys' do
    count = i18n.unused_keys.count
    fail "There are #{count} unused i18n keys! Run 'i18n-tasks unused' for more details." unless count.zero?
  end
end

遣り過ぎるとメンテナンス性が落ちたり、設定が複雑化することもあるので、現行プロジェクトではtranslation missingだけの抽出を対象にしています。

やっと心が安らぐ。。。

44
43
2

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
44
43