equivalent8
equivalent8

Reputation: 14227

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!"

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 ?

Upvotes: 9

Views: 9819

Answers (2)

Paul Fioravanti
Paul Fioravanti

Reputation: 16793

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.

Upvotes: 8

phoet
phoet

Reputation: 18835

i think that i would write an acceptance test for such a "crucial" thing.

in most cases you need the translation in some specific context, ie. displaying something in a datepicker. i would test that context using capybara or whatever works with a javascript driver.

just testing that this translation exists is useless if you don't have the context that it's used within.

Upvotes: -1

Related Questions