Class: I18n::Processes::Data::Router::ConservativeRouter
- Inherits:
-
PatternRouter
- Object
- PatternRouter
- I18n::Processes::Data::Router::ConservativeRouter
- Defined in:
- lib/i18n/processes/data/router/conservative_router.rb
Overview
Keep the path, or infer from base locale
Constant Summary
Constants included from KeyPatternMatching
KeyPatternMatching::MATCH_NOTHING
Instance Attribute Summary
Attributes inherited from PatternRouter
Instance Method Summary collapse
-
#initialize(adapter, config) ⇒ ConservativeRouter
constructor
A new instance of ConservativeRouter.
- #route(locale, forest, &block) ⇒ Object
Methods included from KeyPatternMatching
compile_key_pattern, compile_patterns_re, key_pattern_re_body
Constructor Details
#initialize(adapter, config) ⇒ ConservativeRouter
Returns a new instance of ConservativeRouter.
9 10 11 12 13 14 |
# File 'lib/i18n/processes/data/router/conservative_router.rb', line 9 def initialize(adapter, config) @adapter = adapter @base_locale = config[:base_locale] @locales = config[:locales] super end |
Instance Method Details
#route(locale, forest, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/i18n/processes/data/router/conservative_router.rb', line 16 def route(locale, forest, &block) return to_enum(:route, locale, forest) unless block out = Hash.new { |hash, key| hash[key] = Set.new } not_found = Set.new forest.keys do |key, _node| path = key_path(locale, key) $stderr.puts Rainbow("path in route: #{path}").green # infer from another locale unless path inferred_from = (locales - [locale]).detect { |loc| path = key_path(loc, key) } path = LocalePathname.replace_locale(path, inferred_from, locale) if inferred_from end key_with_locale = "#{locale}.#{key}" $stderr.puts Rainbow("key_with_locale in route: #{key_with_locale}").green if path out[path] << key_with_locale else not_found << key_with_locale end end if not_found.present? # fall back to pattern router not_found_tree = forest.select_keys(root: true) { |key, _| not_found.include?(key) } super(locale, not_found_tree) do |path, tree| out[path] += tree.key_names(root: true) end end out.each do |dest, keys| # $stderr.puts Rainbow("dest in route: #{dest}").green # $stderr.puts Rainbow("dest in route: #{dest}").green block.yield dest, forest.select_keys(root: true) { |key, _| keys.include?(key) } end end |