3 class I18nTest < ActiveSupport::TestCase
4 I18n.available_locales.each do |locale|
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_kind_of String, value, "#{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 Rails.root.glob("config/locales/*.yml").each do |filename|
59 code = File.basename(filename, ".yml")
60 test "#{code} for raw html" do
61 yml = YAML.load_file(filename)
62 assert_nothing_raised do
63 check_values_for_raw_html(yml)
67 test "#{code} present once in ui_languages.yml" do
68 assert_equal(1, AVAILABLE_LANGUAGES.count { |language| language[:code] == code })
72 def test_ui_languages_have_yml_files
73 AVAILABLE_LANGUAGES.each do |language|
74 assert_path_exists Rails.root.join("config/locales/#{language[:code]}.yml")
78 def test_ui_languages_have_required_fields
79 AVAILABLE_LANGUAGES.each do |language|
80 assert_pattern { language => { code: String, native_name: String, english_name: String } }
84 def test_en_for_nil_values
85 en = YAML.load_file(Rails.root.join("config/locales/en.yml"))
86 assert_nothing_raised do
87 check_values_for_nil(en)
91 # We should avoid using the key `zero:` in English, since that key
92 # is used for "numbers ending in zero" in other languages.
93 def test_en_for_zero_key
94 en = YAML.load_file(Rails.root.join("config/locales/en.yml"))
95 assert_nothing_raised do
96 check_keys_for_zero(en)
102 def translation_keys(scope = nil)
103 plural_keys = plural_keys(I18n.default_locale)
105 I18n.t(scope || ".", :locale => I18n.default_locale).map do |key, value|
106 scoped_key = scope ? "#{scope}.#{key}" : key
110 if value.keys - plural_keys == []
113 translation_keys(scoped_key)
121 def plural_keys(locale)
122 I18n.t("i18n.plural.keys", :locale => locale, :raise => true) + [:zero]
123 rescue I18n::MissingTranslationData
124 [:zero, :one, :other]
127 def check_values_for_raw_html(hash)
128 hash.each_pair do |k, v|
130 check_values_for_raw_html(v)
132 next unless k.to_s.end_with?("_html")
133 raise "Avoid using raw html in '#{k}: #{v}'" if v.include? "<"
138 def check_values_for_nil(hash)
139 hash.each_pair do |k, v|
141 check_values_for_nil(v)
143 raise "Avoid nil values in '#{k}: nil'" if v.nil?
148 def check_keys_for_zero(hash)
149 hash.each_pair do |k, v|
151 check_keys_for_zero(v)
153 raise "Avoid using 'zero' key in '#{k}: #{v}'" if k.to_s == "zero"