Module: I18n::Tasks::Data

Included in:
BaseTask
Defined in:
lib/i18n/tasks.rb,
lib/i18n/tasks/data.rb,
lib/i18n/tasks/data/file_system.rb,
lib/i18n/tasks/data/file_formats.rb,
lib/i18n/tasks/data/file_system_base.rb,
lib/i18n/tasks/data/adapter/json_adapter.rb,
lib/i18n/tasks/data/adapter/yaml_adapter.rb

Defined Under Namespace

Modules: Adapter, FileFormats, Router, Tree Classes: FileSystem, FileSystemBase

Constant Summary collapse

DATA_DEFAULTS =
{
  adapter: 'I18n::Tasks::Data::FileSystem'
}.freeze

Instance Method Summary collapse

Instance Method Details

#build_tree(hash) ⇒ Object



48
49
50
# File 'lib/i18n/tasks/data.rb', line 48

def build_tree(hash)
  I18n::Tasks::Data::Tree::Siblings.from_nested_hash(hash)
end

#dataObject

I18n data provider

See Also:



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/i18n/tasks/data.rb', line 13

def data
  @data ||= begin
    data_config = (config[:data] || {}).deep_symbolize_keys
    data_config[:base_locale] = base_locale
    data_config[:locales] = config[:locales]
    adapter_class = data_config[:adapter].presence || data_config[:class].presence || DATA_DEFAULTS[:adapter]
    adapter_class = adapter_class.to_s
    adapter_class = 'I18n::Tasks::Data::FileSystem' if adapter_class == 'file_system'
    data_config.except!(:adapter, :class)
    ActiveSupport::Inflector.constantize(adapter_class).new data_config
  end
end

#data_forest(locales = self.locales) ⇒ Object



30
31
32
33
34
# File 'lib/i18n/tasks/data.rb', line 30

def data_forest(locales = self.locales)
  locales.inject(empty_forest) do |tree, locale|
    tree.merge! data[locale]
  end
end

#empty_forestObject



26
27
28
# File 'lib/i18n/tasks/data.rb', line 26

def empty_forest
  ::I18n::Tasks::Data::Tree::Siblings.new
end

#external_key?(key, locale = base_locale) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/i18n/tasks/data.rb', line 62

def external_key?(key, locale = base_locale)
  data.external(locale)[locale.to_s][key]
end

#key_value?(key, locale = base_locale) ⇒ Boolean

whether the value for key exists in locale (defaults: base_locale)

Returns:

  • (Boolean)


58
59
60
# File 'lib/i18n/tasks/data.rb', line 58

def key_value?(key, locale = base_locale)
  !t(key, locale).nil?
end

#node(key, locale = base_locale) ⇒ Object



44
45
46
# File 'lib/i18n/tasks/data.rb', line 44

def node(key, locale = base_locale)
  data[locale]["#{locale}.#{key}"]
end

#non_normalized_paths(locales: nil) ⇒ Array<String>

Returns paths to data that requires normalization.

Parameters:

  • locales (Array<String>) (defaults to: nil)

    locales to check. Default: all.

Returns:

  • (Array<String>)

    paths to data that requires normalization



83
84
85
# File 'lib/i18n/tasks/data.rb', line 83

def non_normalized_paths(locales: nil)
  Array(locales || self.locales).flat_map { |locale| data.non_normalized_paths(locale) }
end

#normalize_store!(locales: nil, force_pattern_router: false) ⇒ Object

Normalize all the locale data in the store (by writing to the store).

Parameters:

  • locales (Array<String>) (defaults to: nil)

    locales to normalize. Default: all.

  • force_pattern_router (Boolean) (defaults to: false)

    Whether to use pattern router regardless of the config.



70
71
72
73
74
75
76
77
78
79
# File 'lib/i18n/tasks/data.rb', line 70

def normalize_store!(locales: nil, force_pattern_router: false)
  locales ||= self.locales
  router = force_pattern_router ? ::I18n::Tasks::Data::Router::PatternRouter.new(data, data.config) : data.router
  data.with_router(router) do
    Array(locales).each do |target_locale|
      # The store handles actual normalization:
      data[target_locale] = data[target_locale]
    end
  end
end

#t(key, locale = base_locale) ⇒ Object



36
37
38
# File 'lib/i18n/tasks/data.rb', line 36

def t(key, locale = base_locale)
  data.t(key, locale)
end

#t_proc(locale = base_locale) ⇒ Object



52
53
54
55
# File 'lib/i18n/tasks/data.rb', line 52

def t_proc(locale = base_locale)
  @t_proc         ||= {}
  @t_proc[locale] ||= proc { |key| t(key, locale) }
end

#tree(sel) ⇒ Object



40
41
42
# File 'lib/i18n/tasks/data.rb', line 40

def tree(sel)
  data[split_key(sel, 2).first][sel].try(:children)
end