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',
  'isolating_router' => 'I18n::Tasks::Data::Router::IsolatingRouter',
  '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.



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

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.



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

def base_locale
  @base_locale
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#localesObject

Returns the value of attribute locales.



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

def locales
  @locales
end

#routerObject



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

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



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

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)


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

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)


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

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

#merge!(forest) ⇒ Object



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

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



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

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



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

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

#remove_by_key!(forest) ⇒ Object



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

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)


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

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



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

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



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

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

#write(forest) ⇒ Object



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

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