]> git.openstreetmap.org Git - rails.git/blob - test/lib/i18n_test.rb
Migrate UserBlocksController to use CanCanCan
[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       # plural_keys = plural_keys(locale)
7
8       translation_keys.each do |key|
9         variables = []
10
11         default_value = I18n.t(key, :locale => I18n.default_locale)
12
13         if default_value.is_a?(Hash)
14           variables.push("count")
15
16           default_value.each_value do |subvalue|
17             subvalue.scan(/%\{(\w+)\}/) do
18               variables.push(Regexp.last_match(1))
19             end
20           end
21         else
22           default_value.scan(/%\{(\w+)\}/) do
23             variables.push(Regexp.last_match(1))
24           end
25         end
26
27         variables.push("attribute") if key =~ /^(active(model|record)\.)?errors\./
28
29         value = I18n.t(key, :locale => locale, :fallback => true)
30
31         if value.is_a?(Hash)
32           value.each do |subkey, subvalue|
33             # assert plural_keys.include?(subkey), "#{key}.#{subkey} is not a valid plural key"
34
35             next if subvalue.nil?
36
37             subvalue.scan(/%\{(\w+)\}/) do
38               assert variables.include?(Regexp.last_match(1)), "#{key}.#{subkey} uses unknown interpolation variable #{Regexp.last_match(1)}"
39             end
40           end
41         else
42           assert value.is_a?(String), "#{key} is not a string"
43
44           value.scan(/%\{(\w+)\}/) do
45             assert variables.include?(Regexp.last_match(1)), "#{key} uses unknown interpolation variable #{Regexp.last_match(1)}"
46           end
47         end
48       end
49
50       assert %w[ltr rtl].include?(I18n.t("html.dir", :locale => locale)), "html.dir must be ltr or rtl"
51     end
52   end
53
54   private
55
56   def translation_keys(scope = nil)
57     plural_keys = plural_keys(I18n.default_locale)
58
59     I18n.t(scope || ".", :locale => I18n.default_locale).map do |key, value|
60       scoped_key = scope ? "#{scope}.#{key}" : key
61
62       if value.is_a?(Hash)
63         if value.keys - plural_keys == []
64           scoped_key
65         else
66           translation_keys(scoped_key)
67         end
68       elsif value.is_a?(String)
69         scoped_key
70       end
71     end.flatten
72   end
73
74   def plural_keys(locale)
75     I18n.t("i18n.plural.keys", :locale => locale, :raise => true) + [:zero]
76   rescue I18n::MissingTranslationData
77     [:zero, :one, :other]
78   end
79 end