]> git.openstreetmap.org Git - rails.git/blob - config/initializers/i18n.rb
Monkey patch I18n::JS to ensure translations are in a fixed order
[rails.git] / config / initializers / i18n.rb
1 module I18n
2   module Backend
3     module Fallbacks
4       def find_first_string_or_lambda_default(defaults)
5         defaults.each_with_index { |default, ix| return ix if default && !default.is_a?(Symbol) }
6         nil
7       end
8     end
9
10     module PluralizationFallback
11       def pluralize(locale, entry, count)
12         super
13       rescue InvalidPluralizationData => ex
14         raise ex unless ex.entry.has_key?(:other)
15         ex.entry[:other]
16       end
17     end
18   end
19
20   module JS
21     class << self
22       def make_ordered(unordered)
23         ordered = ActiveSupport::OrderedHash.new
24
25         unordered.keys.sort { |a,b| a.to_s <=> b.to_s }.each do |key|
26           value = unordered[key]
27
28           if value.is_a?(Hash)
29             ordered[key] = make_ordered(value)
30           else
31             ordered[key] = value
32           end
33         end
34
35         ordered
36       end
37
38       def filtered_translations_with_order
39         make_ordered(filtered_translations_without_order)
40       end
41
42       alias_method_chain :filtered_translations, :order
43     end
44   end
45 end
46
47 I18n::Backend::Simple.include(I18n::Backend::PluralizationFallback)
48 I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
49
50 I18n.fallbacks.map("no" => "nb")