3 # Extends the I18n module to hold a fallbacks instance which is set to an
4 # instance of I18n::Locale::Fallbacks by default but can be swapped with a
5 # different implementation.
7 # Locale fallbacks will compute a number of fallback locales for a given locale.
11 # I18n.fallbacks[:"es-MX"] # => [:"es-MX", :es, :en] </code></pre>
13 # Locale fallbacks always fall back to
15 # * all parent locales of a given locale (e.g. :es for :"es-MX") first,
16 # * the current default locales and all of their parents second
18 # The default locales are set to [I18n.default_locale] by default but can be
19 # set to something else.
21 # One can additionally add any number of additional fallback locales manually.
22 # These will be added before the default locales to the fallback chain. For
25 # # using the default locale as default fallback locale
27 # I18n.default_locale = :"en-US"
28 # I18n.fallbacks = I18n::Locale::Fallbacks.new(:"de-AT" => :"de-DE")
29 # I18n.fallbacks[:"de-AT"] # => [:"de-AT", :"de-DE", :de, :"en-US", :en]
31 # # using a custom locale as default fallback locale
33 # I18n.fallbacks = I18n::Locale::Fallbacks.new(:"en-GB", :"de-AT" => :de, :"de-CH" => :de)
34 # I18n.fallbacks[:"de-AT"] # => [:"de-AT", :de, :"en-GB", :en]
35 # I18n.fallbacks[:"de-CH"] # => [:"de-CH", :de, :"en-GB", :en]
37 # # mapping fallbacks to an existing instance
39 # # people speaking Catalan also speak Spanish as spoken in Spain
40 # fallbacks = I18n.fallbacks
41 # fallbacks.map(:ca => :"es-ES")
42 # fallbacks[:ca] # => [:ca, :"es-ES", :es, :"en-US", :en]
44 # # people speaking Arabian as spoken in Palestine also speak Hebrew as spoken in Israel
45 # fallbacks.map(:"ar-PS" => :"he-IL")
46 # fallbacks[:"ar-PS"] # => [:"ar-PS", :ar, :"he-IL", :he, :"en-US", :en]
47 # fallbacks[:"ar-EG"] # => [:"ar-EG", :ar, :"en-US", :en]
49 # # people speaking Sami as spoken in Finnland also speak Swedish and Finnish as spoken in Finnland
50 # fallbacks.map(:sms => [:"se-FI", :"fi-FI"])
51 # fallbacks[:sms] # => [:sms, :"se-FI", :se, :"fi-FI", :fi, :"en-US", :en]
55 class Fallbacks < Hash
56 def initialize(*mappings)
58 map(mappings.pop) if mappings.last.is_a?(Hash)
59 self.defaults = mappings.empty? ? [I18n.default_locale.to_sym] : mappings
62 def defaults=(defaults)
63 @defaults = defaults.map { |default| compute(default, false) }.flatten
68 raise InvalidLocale.new(locale) if locale.nil?
69 locale = locale.to_sym
70 super || store(locale, compute(locale))
74 mappings.each do |from, to|
75 from, to = from.to_sym, Array(to)
78 @map[from] << _to.to_sym
85 def compute(tags, include_defaults = true, exclude = [])
86 result = Array(tags).collect do |tag|
87 tags = I18n::Locale::Tag.tag(tag).self_and_parents.map! { |t| t.to_sym } - exclude
88 tags.each { |_tag| tags += compute(@map[_tag], false, exclude + tags) if @map[_tag] }
91 result.push(*defaults) if include_defaults