]> git.openstreetmap.org Git - rails.git/blob - test/lib/i18n_test.rb
Merge remote-tracking branch 'upstream/pull/2381'
[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 variables.include?(Regexp.last_match(1)), "#{key}.#{subkey} uses unknown interpolation variable #{Regexp.last_match(1)}"
40               end
41             end
42           else
43             assert value.is_a?(String), "#{key} is not a string"
44
45             value.scan(/%\{(\w+)\}/) do
46               assert variables.include?(Regexp.last_match(1)), "#{key} uses unknown interpolation variable #{Regexp.last_match(1)}"
47             end
48           end
49         end
50
51         assert %w[ltr rtl].include?(I18n.t("html.dir", :locale => locale)), "html.dir must be ltr or rtl"
52       end
53     end
54   end
55
56   private
57
58   def translation_keys(scope = nil)
59     plural_keys = plural_keys(I18n.default_locale)
60
61     I18n.t(scope || ".", :locale => I18n.default_locale).map do |key, value|
62       scoped_key = scope ? "#{scope}.#{key}" : key
63
64       if value.is_a?(Hash)
65         if value.keys - plural_keys == []
66           scoped_key
67         else
68           translation_keys(scoped_key)
69         end
70       elsif value.is_a?(String)
71         scoped_key
72       end
73     end.flatten
74   end
75
76   def plural_keys(locale)
77     I18n.t("i18n.plural.keys", :locale => locale, :raise => true) + [:zero]
78   rescue I18n::MissingTranslationData
79     [:zero, :one, :other]
80   end
81 end