]> git.openstreetmap.org Git - rails.git/blob - test/lib/i18n_test.rb
Inline auth provider logos
[rails.git] / test / lib / i18n_test.rb
1 require "test_helper"
2
3 class I18nTest < ActiveSupport::TestCase
4   I18n.available_locales.each do |locale|
5     test locale.to_s do
6       without_i18n_exceptions do
7         # plural_keys = plural_keys(locale)
8
9         translation_keys.each do |key|
10           variables = []
11
12           default_value = I18n.t(key, :locale => I18n.default_locale)
13
14           if default_value.is_a?(Hash)
15             variables.push("count")
16
17             default_value.each_value do |subvalue|
18               subvalue.scan(/%\{(\w+)\}/) do
19                 variables.push(Regexp.last_match(1))
20               end
21             end
22           else
23             default_value.scan(/%\{(\w+)\}/) do
24               variables.push(Regexp.last_match(1))
25             end
26           end
27
28           variables.push("attribute") if key =~ /^(active(model|record)\.)?errors\./
29
30           value = I18n.t(key, :locale => locale, :fallback => true)
31
32           if value.is_a?(Hash)
33             value.each do |subkey, subvalue|
34               # assert plural_keys.include?(subkey), "#{key}.#{subkey} is not a valid plural key"
35
36               next if subvalue.nil?
37
38               subvalue.scan(/%\{(\w+)\}/) do
39                 assert_includes variables, Regexp.last_match(1), "#{key}.#{subkey} uses unknown interpolation variable #{Regexp.last_match(1)}"
40               end
41             end
42
43             assert_includes value, :other, "#{key}.other plural key missing"
44           else
45             assert_kind_of String, value, "#{key} is not a string"
46
47             value.scan(/%\{(\w+)\}/) do
48               assert_includes variables, Regexp.last_match(1), "#{key} uses unknown interpolation variable #{Regexp.last_match(1)}"
49             end
50           end
51         end
52
53         assert_includes %w[ltr rtl], I18n.t("html.dir", :locale => locale), "html.dir must be ltr or rtl"
54       end
55     end
56   end
57
58   Rails.root.glob("config/locales/*.yml").each do |filename|
59     code = File.basename(filename, ".yml")
60     yml = YAML.load_file(filename)
61
62     test "#{code} for raw html" do
63       assert_nothing_raised do
64         check_values_for_raw_html(yml)
65       end
66     end
67
68     test "#{code} for mediawiki magic" do
69       assert_nothing_raised do
70         check_values_for_mediawiki_magic(yml)
71       end
72     end
73
74     test "#{code} present once in ui_languages.yml" do
75       assert_equal(1, AVAILABLE_LANGUAGES.count { |language| language[:code] == code })
76     end
77   end
78
79   def test_ui_languages_have_yml_files
80     AVAILABLE_LANGUAGES.each do |language|
81       assert_path_exists Rails.root.join("config/locales/#{language[:code]}.yml")
82     end
83   end
84
85   def test_ui_languages_have_required_fields
86     AVAILABLE_LANGUAGES.each do |language|
87       assert_pattern { language => { code: String, native_name: String, english_name: String } }
88     end
89   end
90
91   def test_en_for_nil_values
92     en = YAML.load_file(Rails.root.join("config/locales/en.yml"))
93     assert_nothing_raised do
94       check_values_for_nil(en)
95     end
96   end
97
98   # We should avoid using the key `zero:` in English, since that key
99   # is used for "numbers ending in zero" in other languages.
100   def test_en_for_zero_key
101     en = YAML.load_file(Rails.root.join("config/locales/en.yml"))
102     assert_nothing_raised do
103       check_keys_for_zero(en)
104     end
105   end
106
107   private
108
109   def translation_keys(scope = nil)
110     plural_keys = plural_keys(I18n.default_locale)
111
112     I18n.t(scope || ".", :locale => I18n.default_locale).map do |key, value|
113       scoped_key = scope ? "#{scope}.#{key}" : key
114
115       case value
116       when Hash
117         if value.keys - plural_keys == []
118           scoped_key
119         else
120           translation_keys(scoped_key)
121         end
122       when String
123         scoped_key
124       end
125     end.flatten
126   end
127
128   def plural_keys(locale)
129     I18n.t("i18n.plural.keys", :locale => locale, :raise => true) + [:zero]
130   rescue I18n::MissingTranslationData
131     [:zero, :one, :other]
132   end
133
134   def check_values_for_raw_html(hash)
135     hash.each_pair do |k, v|
136       if v.is_a? Hash
137         check_values_for_raw_html(v)
138       else
139         next unless k.to_s.end_with?("_html")
140         raise "Avoid using raw html in '#{k}: #{v}'" if v.include? "<"
141       end
142     end
143   end
144
145   def check_values_for_mediawiki_magic(hash)
146     hash.each_pair do |k, v|
147       if v.is_a? Hash
148         check_values_for_mediawiki_magic(v)
149       else
150         raise "Avoid using mediawiki magic in '#{k}: #{v}'" if v.match?(/\{\{(PLURAL|GENDER|GRAMMAR)[|:]/)
151       end
152     end
153   end
154
155   def check_values_for_nil(hash)
156     hash.each_pair do |k, v|
157       if v.is_a? Hash
158         check_values_for_nil(v)
159       else
160         raise "Avoid nil values in '#{k}: nil'" if v.nil?
161       end
162     end
163   end
164
165   def check_keys_for_zero(hash)
166     hash.each_pair do |k, v|
167       if v.is_a? Hash
168         check_keys_for_zero(v)
169       else
170         raise "Avoid using 'zero' key in '#{k}: #{v}'" if k.to_s == "zero"
171       end
172     end
173   end
174 end