1 # frozen_string_literal: true
5 class I18nTest < ActiveSupport::TestCase
6 I18n.available_locales.each do |locale|
8 without_i18n_exceptions do
9 # plural_keys = plural_keys(locale)
11 translation_keys.each do |key|
14 default_value = I18n.t(key, :locale => I18n.default_locale)
16 if default_value.is_a?(Hash)
17 variables.push("count")
19 default_value.each_value do |subvalue|
20 subvalue.scan(/%\{(\w+)\}/) do
21 variables.push(Regexp.last_match(1))
25 default_value.scan(/%\{(\w+)\}/) do
26 variables.push(Regexp.last_match(1))
30 variables.push("attribute") if key =~ /^(active(model|record)\.)?errors\./
32 value = I18n.t(key, :locale => locale, :fallback => true)
35 value.each do |subkey, subvalue|
36 # assert plural_keys.include?(subkey), "#{key}.#{subkey} is not a valid plural key"
40 subvalue.scan(/%\{(\w+)\}/) do
41 assert_includes variables, Regexp.last_match(1), "#{key}.#{subkey} uses unknown interpolation variable #{Regexp.last_match(1)}"
45 assert_includes value, :other, "#{key}.other plural key missing"
47 assert_kind_of String, value, "#{key} is not a string"
49 value.scan(/%\{(\w+)\}/) do
50 assert_includes variables, Regexp.last_match(1), "#{key} uses unknown interpolation variable #{Regexp.last_match(1)}"
55 assert_includes %w[ltr rtl], I18n.t("html.dir", :locale => locale), "html.dir must be ltr or rtl"
60 Rails.root.glob("config/locales/*.yml").each do |filename|
61 code = File.basename(filename, ".yml")
62 yml = YAML.load_file(filename)
64 test "#{code} for raw html" do
65 assert_nothing_raised do
66 check_values_for_raw_html(yml)
70 test "#{code} for mediawiki magic" do
71 assert_nothing_raised do
72 check_values_for_mediawiki_magic(yml)
76 test "#{code} present once in ui_languages.yml" do
77 assert_equal(1, AVAILABLE_LANGUAGES.count { |language| language[:code] == code })
81 def test_ui_languages_have_yml_files
82 AVAILABLE_LANGUAGES.each do |language|
83 assert_path_exists Rails.root.join("config/locales/#{language[:code]}.yml")
87 def test_ui_languages_have_required_fields
88 AVAILABLE_LANGUAGES.each do |language|
89 assert_pattern { language => { code: String, native_name: String } }
93 def test_en_for_nil_values
94 en = YAML.load_file(Rails.root.join("config/locales/en.yml"))
95 assert_nothing_raised do
96 check_values_for_nil(en)
100 # We should avoid using the key `zero:` in English, since that key
101 # is used for "numbers ending in zero" in other languages.
102 def test_en_for_zero_key
103 en = YAML.load_file(Rails.root.join("config/locales/en.yml"))
104 assert_nothing_raised do
105 check_keys_for_zero(en)
111 def translation_keys(scope = nil)
112 plural_keys = plural_keys(I18n.default_locale)
114 I18n.t(scope || ".", :locale => I18n.default_locale).map do |key, value|
115 scoped_key = scope ? "#{scope}.#{key}" : key
119 if value.keys - plural_keys == []
122 translation_keys(scoped_key)
130 def plural_keys(locale)
131 I18n.t("i18n.plural.keys", :locale => locale, :raise => true) + [:zero]
132 rescue I18n::MissingTranslationData
133 [:zero, :one, :other]
136 def check_values_for_raw_html(hash)
137 hash.each_pair do |k, v|
139 check_values_for_raw_html(v)
141 next unless k.to_s.end_with?("_html")
142 raise "Avoid using raw html in '#{k}: #{v}'" if v.include? "<"
147 def check_values_for_mediawiki_magic(hash)
148 hash.each_pair do |k, v|
150 check_values_for_mediawiki_magic(v)
152 raise "Avoid using mediawiki magic in '#{k}: #{v}'" if v.match?(/\{\{(PLURAL|GENDER|GRAMMAR)[|:]/)
157 def check_values_for_nil(hash)
158 hash.each_pair do |k, v|
160 check_values_for_nil(v)
162 raise "Avoid nil values in '#{k}: nil'" if v.nil?
167 def check_keys_for_zero(hash)
168 hash.each_pair do |k, v|
170 check_keys_for_zero(v)
172 raise "Avoid using 'zero' key in '#{k}: #{v}'" if k.to_s == "zero"