Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to test i18n in Rails with RSpec

Question context:

let say that there is some really important row in config/locales/en.yml that is crucial to exist.

en:
  foo:
    bar: "bubla!"
  • I don't want to test every single line but
  • I don't want the test to be too brittle (so no I18n.t('foo.bar').should =~ /bubla/)

so way I'm testing currently is like this

#spec/locals_spec.rb

require 'spec_helper'
describe I18n do
  it do
    I18n.t('date.datepicker').should be_kind_of(String)
  end
end

this way I'm just ensuring that translation exist and that it don't continues (e.g. 'foo.bar.car.lol'

but still I'm not satisfied

Question: What's the best practice to test I18n translations with RSpec and where in spec folder I should place them ?

like image 313
equivalent8 Avatar asked Jul 05 '12 15:07

equivalent8


1 Answers

Check this StackOverflow question for some ideas. My preferred way is this answer on the same question.

Update: These days I tend to use the i18n-tasks gem to handle testing related to i18n, and not what I wrote above or have answered on StackOverflow previously.

I wanted to use i18n in my RSpec tests primarily to make sure that I had translations for everything ie there were no translations missed. i18n-tasks can do that and more through static analysis of my code, so I don't need to run tests for all I18n.available_locales anymore (apart from when testing very locale-specific functionality, like, for example, switching from any locale to any other locale in the system).

Doing this has meant I can confirm that all i18n keys in the system actually have values (and that none are unused or obsolete), while keeping the number of repetitive tests, and consequently the suite running time, down.

like image 66
Paul Fioravanti Avatar answered Sep 17 '22 19:09

Paul Fioravanti