Class: I18n::Tasks::Data::FileSystemBase

Inherits:
Object
  • Object
show all
Includes:
FileFormats, Logging
Defined in:
lib/i18n/tasks/data/file_system_base.rb

Overview

rubocop:disable Metrics/ClassLength

Direct Known Subclasses

FileSystem

Constant Summary collapse

DEFAULTS =
{
  read: ['config/locales/%{locale}.yml'],
  write: ['config/locales/%{locale}.yml']
}.freeze
ROUTER_NAME_ALIASES =
{
  'conservative_router' => 'I18n::Tasks::Data::Router::ConservativeRouter',
  'pattern_router' => 'I18n::Tasks::Data::Router::PatternRouter'
}.freeze

Constants included from Logging

Logging::MUTEX, Logging::PROGRAM_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

log_error, log_stderr, log_verbose, log_warn, program_name, warn_deprecated

Methods included from FileFormats

#adapter_dump, #adapter_op, #adapter_parse, included

Constructor Details

#initialize(config = {}) ⇒ FileSystemBase

Returns a new instance of FileSystemBase.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/i18n/tasks/data/file_system_base.rb', line 24

def initialize(config = {})
  self.config = config.except(:base_locale, :locales)
  self.config[:sort] = !config[:keep_order]
  @base_locale = config[:base_locale]
  locales = config[:locales].presence
  @locales = LocaleList.normalize_locale_list(locales || available_locales, base_locale, true)
  if locales.present?
    log_verbose "locales read from config #{@locales * ', '}"
  else
    log_verbose "locales inferred from data: #{@locales * ', '}"
  end
end

Instance Attribute Details

#base_localeObject (readonly)

Returns the value of attribute base_locale.



16
17
18
# File 'lib/i18n/tasks/data/file_system_base.rb', line 16

def base_locale
  @base_locale
end

#configObject

Returns the value of attribute config.



16
17
18
# File 'lib/i18n/tasks/data/file_system_base.rb', line 16

def config
  @config
end

#localesObject

Returns the value of attribute locales.



15
16
17
# File 'lib/i18n/tasks/data/file_system_base.rb', line 15

def locales
  @locales
end

#routerObject



156
157
158
159
160
161
162
163
# File 'lib/i18n/tasks/data/file_system_base.rb', line 156

def router
  @router ||= begin
    name = @config[:router].presence || 'conservative_router'
    name = ROUTER_NAME_ALIASES[name] || name
    router_class = ActiveSupport::Inflector.constantize(name)
    router_class.new(self, @config.merge(base_locale: base_locale, locales: locales))
  end
end

Instance Method Details

#available_localesObject

Get available locales from the list of file names to read



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/i18n/tasks/data/file_system_base.rb', line 115

def available_locales
  @available_locales ||= begin
    locales = Set.new
    Array(config[:read]).map do |pattern|
      [pattern, Dir.glob(format(pattern, locale: '*'))] if pattern.include?('%{locale}')
    end.compact.each do |pattern, paths|
      p  = pattern.gsub('\\', '\\\\').gsub('/', '\/').gsub('.', '\.')
      p  = p.gsub(/(\*+)/) { Regexp.last_match(1) == '**' ? '.*' : '[^/]*?' }.gsub('%{locale}', '([^/.]+)')
      re = /\A#{p}\z/
      paths.each do |path|
        locales << Regexp.last_match(1) if re =~ path
      end
    end
    locales
  end
end

#external(locale) ⇒ ::I18n::Tasks::Data::Siblings

Parameters:

  • locale (String, Symbol)

Returns:

  • (::I18n::Tasks::Data::Siblings)


49
50
51
52
53
# File 'lib/i18n/tasks/data/file_system_base.rb', line 49

def external(locale)
  locale = locale.to_s
  @external ||= {}
  @external[locale] ||= read_locale(locale, paths: config[:external])
end

#get(locale) ⇒ ::I18n::Tasks::Data::Siblings Also known as: []

Parameters:

  • locale (String, Symbol)

Returns:

  • (::I18n::Tasks::Data::Siblings)


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

def get(locale)
  locale = locale.to_s
  @trees ||= {}
  @trees[locale] ||= read_locale(locale)
end

#merge!(forest) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/i18n/tasks/data/file_system_base.rb', line 88

def merge!(forest)
  forest.inject(Tree::Siblings.new) do |result, root|
    locale = root.key
    merged = get(locale).merge(root)
    set locale, merged
    result.merge! merged
  end
end

#non_normalized_paths(locale) ⇒ Array<String>

Returns paths to files that are not normalized.

Parameters:

  • locale (String)

Returns:

  • (Array<String>)

    paths to files that are not normalized



78
79
80
81
82
# File 'lib/i18n/tasks/data/file_system_base.rb', line 78

def non_normalized_paths(locale)
  router.route(locale, get(locale))
        .reject { |path, tree_slice| normalized?(path, tree_slice) }
        .map(&:first)
end

#reloadObject

Returns self.

Returns:

  • self



108
109
110
111
112
# File 'lib/i18n/tasks/data/file_system_base.rb', line 108

def reload
  @trees             = nil
  @available_locales = nil
  self
end

#remove_by_key!(forest) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/i18n/tasks/data/file_system_base.rb', line 97

def remove_by_key!(forest)
  forest.inject(Tree::Siblings.new) do |removed, root|
    locale = root.key
    locale_data = get(locale)
    subtracted = locale_data.subtract_by_key(forest)
    set locale, subtracted
    removed.merge! locale_data.subtract_by_key(subtracted)
  end
end

#set(locale, tree) ⇒ Object Also known as: []=

set locale tree

Parameters:

  • locale (String, Symbol)
  • tree (::I18n::Tasks::Data::Siblings)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/i18n/tasks/data/file_system_base.rb', line 58

def set(locale, tree)
  locale = locale.to_s
  @trees&.delete(locale)
  paths_before = Set.new(get(locale)[locale].leaves.map { |node| node.data[:path] })
  paths_after = Set.new([])
  router.route locale, tree do |path, tree_slice|
    paths_after << path
    write_tree path, tree_slice, config[:sort]
  end
  (paths_before - paths_after).each do |path|
    FileUtils.remove_file(path) if File.exist?(path)
  end
  @trees&.delete(locale)
  @available_locales = nil
end

#t(key, locale) ⇒ Object



132
133
134
135
136
137
# File 'lib/i18n/tasks/data/file_system_base.rb', line 132

def t(key, locale)
  tree = self[locale.to_s]
  return unless tree

  tree[locale][key].try(:value_or_children_hash)
end

#with_router(router) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/i18n/tasks/data/file_system_base.rb', line 144

def with_router(router)
  router_was  = self.router
  self.router = router
  yield
ensure
  self.router = router_was
end

#write(forest) ⇒ Object



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

def write(forest)
  forest.each { |root| set(root.key, root.to_siblings) }
end