3 class I18nTest < ActiveSupport::TestCase
4 I18n.available_locales.each do |locale|
5 define_method("test_#{locale.to_s.underscore}".to_sym) do
6 without_i18n_exceptions do
7 # plural_keys = plural_keys(locale)
9 translation_keys.each do |key|
12 default_value = I18n.t(key, :locale => I18n.default_locale)
14 if default_value.is_a?(Hash)
15 variables.push("count")
17 default_value.each_value do |subvalue|
18 subvalue.scan(/%\{(\w+)\}/) do
19 variables.push(Regexp.last_match(1))
23 default_value.scan(/%\{(\w+)\}/) do
24 variables.push(Regexp.last_match(1))
28 variables.push("attribute") if key =~ /^(active(model|record)\.)?errors\./
30 value = I18n.t(key, :locale => locale, :fallback => true)
33 value.each do |subkey, subvalue|
34 # assert plural_keys.include?(subkey), "#{key}.#{subkey} is not a valid plural key"
38 subvalue.scan(/%\{(\w+)\}/) do
39 assert_includes variables, Regexp.last_match(1), "#{key}.#{subkey} uses unknown interpolation variable #{Regexp.last_match(1)}"
43 assert_includes value, :other, "#{key}.other plural key missing"
45 assert value.is_a?(String), "#{key} is not a string"
47 value.scan(/%\{(\w+)\}/) do
48 assert_includes variables, Regexp.last_match(1), "#{key} uses unknown interpolation variable #{Regexp.last_match(1)}"
53 assert_includes %w[ltr rtl], I18n.t("html.dir", :locale => locale), "html.dir must be ltr or rtl"
58 def test_en_for_raw_html
59 en = YAML.load_file(Rails.root.join("config/locales/en.yml"))
60 assert_nothing_raised do
61 check_values_for_raw_html(en)
65 def test_en_for_nil_values
66 en = YAML.load_file(Rails.root.join("config/locales/en.yml"))
67 assert_nothing_raised do
68 check_values_for_nil(en)
72 # We should avoid using the key `zero:` in English, since that key
73 # is used for "numbers ending in zero" in other languages.
74 def test_en_for_zero_key
75 en = YAML.load_file(Rails.root.join("config/locales/en.yml"))
76 assert_nothing_raised do
77 check_keys_for_zero(en)
83 def translation_keys(scope = nil)
84 plural_keys = plural_keys(I18n.default_locale)
86 I18n.t(scope || ".", :locale => I18n.default_locale).map do |key, value|
87 scoped_key = scope ? "#{scope}.#{key}" : key
91 if value.keys - plural_keys == []
94 translation_keys(scoped_key)
102 def plural_keys(locale)
103 I18n.t("i18n.plural.keys", :locale => locale, :raise => true) + [:zero]
104 rescue I18n::MissingTranslationData
105 [:zero, :one, :other]
108 def check_values_for_raw_html(hash)
109 hash.each_pair do |k, v|
111 check_values_for_raw_html(v)
113 next unless k.end_with?("_html")
114 raise "Avoid using raw html in '#{k}: #{v}'" if v.include? "<"
119 def check_values_for_nil(hash)
120 hash.each_pair do |k, v|
122 check_values_for_nil(v)
124 raise "Avoid nil values in '#{k}: nil'" if v.nil?
129 def check_keys_for_zero(hash)
130 hash.each_pair do |k, v|
132 check_keys_for_zero(v)
134 raise "Avoid using 'zero' key in '#{k}: #{v}'" if k.to_s == "zero"