]> git.openstreetmap.org Git - rails.git/blob - test/lib/i18n_test.rb
Localisation updates from https://translatewiki.net.
[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     define_method("test_#{locale.to_s.underscore}".to_sym) 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 value.is_a?(String), "#{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   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)
62     end
63   end
64
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)
69     end
70   end
71
72   private
73
74   def translation_keys(scope = nil)
75     plural_keys = plural_keys(I18n.default_locale)
76
77     I18n.t(scope || ".", :locale => I18n.default_locale).map do |key, value|
78       scoped_key = scope ? "#{scope}.#{key}" : key
79
80       case value
81       when Hash
82         if value.keys - plural_keys == []
83           scoped_key
84         else
85           translation_keys(scoped_key)
86         end
87       when String
88         scoped_key
89       end
90     end.flatten
91   end
92
93   def plural_keys(locale)
94     I18n.t("i18n.plural.keys", :locale => locale, :raise => true) + [:zero]
95   rescue I18n::MissingTranslationData
96     [:zero, :one, :other]
97   end
98
99   def check_values_for_raw_html(hash)
100     hash.each_pair do |k, v|
101       if v.is_a? Hash
102         check_values_for_raw_html(v)
103       else
104         next unless k.end_with?("_html")
105         raise "Avoid using raw html in '#{k}: #{v}'" if v.include? "<"
106       end
107     end
108   end
109
110   def check_values_for_nil(hash)
111     hash.each_pair do |k, v|
112       if v.is_a? Hash
113         check_values_for_nil(v)
114       else
115         raise "Avoid nil values in '#{k}: nil'" if v.nil?
116       end
117     end
118   end
119 end