]> git.openstreetmap.org Git - rails.git/blob - test/unit/i18n_test.rb
Upgrade passwords to the latest hashing scheme on login
[rails.git] / test / unit / i18n_test.rb
1 require File.dirname(__FILE__) + '/../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 do |subkey,subvalue|
17             subvalue.scan(/%\{(\w+)\}/) do
18               variables.push($1)
19             end
20           end
21         else
22           default_value.scan(/%\{(\w+)\}/) do
23             variables.push($1)
24           end
25         end
26
27         value = I18n.t(key, :locale => locale, :fallback => true)
28
29         if value.is_a?(Hash)
30           value.each do |subkey,subvalue|
31 #            assert plural_keys.include?(subkey), "#{key}.#{subkey} is not a valid plural key"
32
33             unless subvalue.nil?
34               subvalue.scan(/%\{(\w+)\}/) do
35                 assert variables.include?($1), "#{key}.#{subkey} uses unknown interpolation variable #{$1}"
36               end
37             end
38           end
39         else
40           assert value.is_a?(String), "#{key} is not a string"
41
42           value.scan(/%\{(\w+)\}/) do
43             assert variables.include?($1), "#{key} uses unknown interpolation variable #{$1}"
44           end
45         end
46       end
47     end
48   end
49 private
50   def translation_keys(scope = nil)
51     plural_keys = plural_keys(I18n.default_locale)
52
53     I18n.t(scope || ".", :locale => I18n.default_locale).map do |key,value|
54       scoped_key = scope ? "#{scope}.#{key}" : key
55
56       if value.kind_of?(Hash)
57         if value.keys - plural_keys == []
58           scoped_key
59         else
60           translation_keys(scoped_key)
61         end
62       elsif value.kind_of?(String)
63         scoped_key
64       end
65     end.flatten
66   end
67
68   def plural_keys(locale)
69     I18n.t("i18n.plural.keys", :locale => locale, :raise => true) + [ :zero ]
70   rescue I18n::MissingTranslationData
71     [ :zero, :one, :other ]
72   end
73 end