Module: I18n::Backend::Flatten

Included in:
KeyValue::Implementation
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/i18n-1.12.0/lib/i18n/backend/flatten.rb

Overview

This module contains several helpers to assist flattening translations. You may want to flatten translations for:

1) speed up lookups, as in the Memoize backend;
2) In case you want to store translations in a data store, as in ActiveRecord backend;

You can check both backends above for some examples. This module also keeps all links in a hash so they can be properly resolved when flattened.

Constant Summary collapse

SEPARATOR_ESCAPE_CHAR =
"\001"
FLATTEN_SEPARATOR =
"."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape_default_separator(key) ⇒ Object

Receives a string and escape the default separator.



38
39
40
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/i18n-1.12.0/lib/i18n/backend/flatten.rb', line 38

def self.escape_default_separator(key) #:nodoc:
  key.to_s.tr(FLATTEN_SEPARATOR, SEPARATOR_ESCAPE_CHAR)
end

.normalize_flat_keys(locale, key, scope, separator) ⇒ Object

normalize_keys the flatten way. This method is significantly faster and creates way less objects than the one at I18n.normalize_keys. It also handles escaping the translation keys.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/i18n-1.12.0/lib/i18n/backend/flatten.rb', line 20

def self.normalize_flat_keys(locale, key, scope, separator)
  keys = [scope, key]
  keys.flatten!
  keys.compact!

  separator ||= I18n.default_separator

  if separator != FLATTEN_SEPARATOR
    from_str = "#{FLATTEN_SEPARATOR}#{separator}"
    to_str = "#{SEPARATOR_ESCAPE_CHAR}#{FLATTEN_SEPARATOR}"

    keys.map! { |k| k.to_s.tr from_str, to_str }
  end

  keys.join(".")
end

Instance Method Details

#flatten_keys(hash, escape, prev_key = nil, &block) ⇒ Object

Flatten keys for nested Hashes by chaining up keys:

>> { "a" => { "b" => { "c" => "d", "e" => "f" }, "g" => "h" }, "i" => "j"}.wind
=> { "a.b.c" => "d", "a.b.e" => "f", "a.g" => "h", "i" => "j" }


59
60
61
62
63
64
65
66
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/i18n-1.12.0/lib/i18n/backend/flatten.rb', line 59

def flatten_keys(hash, escape, prev_key=nil, &block)
  hash.each_pair do |key, value|
    key = escape_default_separator(key) if escape
    curr_key = [prev_key, key].compact.join(FLATTEN_SEPARATOR).to_sym
    yield curr_key, value
    flatten_keys(value, escape, curr_key, &block) if value.is_a?(Hash)
  end
end

#flatten_translations(locale, data, escape, subtree) ⇒ Object

Receives a hash of translations (where the key is a locale and the value is another hash) and return a hash with all translations flattened.

Nested hashes are included in the flattened hash just if subtree is true and Symbols are automatically stored as links.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/i18n-1.12.0/lib/i18n/backend/flatten.rb', line 74

def flatten_translations(locale, data, escape, subtree)
  hash = {}
  flatten_keys(data, escape) do |key, value|
    if value.is_a?(Hash)
      hash[key] = value if subtree
    else
      store_link(locale, key, value) if value.is_a?(Symbol)
      hash[key] = value
    end
  end
  hash
end

Store flattened links.



50
51
52
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/i18n-1.12.0/lib/i18n/backend/flatten.rb', line 50

def links
  @links ||= I18n.new_double_nested_cache
end

#normalize_flat_keys(locale, key, scope, separator) ⇒ Object

Shortcut to I18n::Backend::Flatten.normalize_flat_keys and then resolve_links.



44
45
46
47
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/i18n-1.12.0/lib/i18n/backend/flatten.rb', line 44

def normalize_flat_keys(locale, key, scope, separator)
  key = I18n::Backend::Flatten.normalize_flat_keys(locale, key, scope, separator)
  resolve_link(locale, key)
end