Class: I18nTasks::Plugin::ViewComponent::Filesystem

Inherits:
I18n::Tasks::Data::FileSystemBase
  • Object
show all
Defined in:
lib/i18n_tasks/plugin/view_component/filesystem.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Filesystem

rubocop:disable Metrics/MethodLength



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/i18n_tasks/plugin/view_component/filesystem.rb', line 12

def initialize(config) # rubocop:disable Metrics/MethodLength
  @view_component_root = config.fetch(:view_component_root, 'app/components').gsub(%r{/$}, '')

  erb_conf = I18n::Tasks::UsedKeys::SEARCH_DEFAULTS[:scanners].find do |s|
    s[0] == '::I18n::Tasks::Scanners::ErbAstScanner'
  end

  if erb_conf
    erb_conf[1][:exclude] ||= []
    erb_conf[1][:exclude].push(component_root_erb_pattern)
  end

  super

  I18n::Tasks.add_scanner(
    'I18nTasks::Plugin::ViewComponent::ComponentErbScanner',
    only: [component_root_erb_pattern]
  )
end

Instance Method Details

#load_file(path) ⇒ Object

Overloaded from ‘I18n::Tasks::Data::FileFormats` (included in `I18n::Tasks::Data::FileSystemBase`)

In component-local locale files, it adds the component’s module hierarchy to the locale scope, as does ‘ViewComponent::Translatable`.

Locale files outside the ViewComponent directory are not modified.



40
41
42
43
44
45
46
47
48
49
# File 'lib/i18n_tasks/plugin/view_component/filesystem.rb', line 40

def load_file(path)
  result = super

  return result unless component_locale_data?(path)

  locale    = result.keys.first
  key_parts = [locale, *component_keys(path)]

  key_parts.reverse.inject(result[locale]) { |akk, key| { key => akk } }
end

#normalized?(path, tree) ⇒ Boolean

Overloaded from ‘I18n::Tasks::Data::FileFormats` (included in `I18n::Tasks::Data::FileSystemBase`)

This is the reverse method of ‘#load_file` above. It takes a locale tree and removes the scope of the component.

As above, locale files outside the ViewComponent directory are not modified.

Returns:



59
60
61
62
63
64
65
# File 'lib/i18n_tasks/plugin/view_component/filesystem.rb', line 59

def normalized?(path, tree)
  if component_locale_data?(path)
    super(path, tree_without_component_keys(path, tree))
  else
    super
  end
end

#write_tree(path, tree, sort = nil) ⇒ Object

Overloaded from ‘I18n::Tasks::Data::FileFormats` (included in `I18n::Tasks::Data::FileSystemBase`)

This is needed to reverse the effects i18n-task’s standard router has in locale files inside ViewComponent’s directories when invoking ‘i18n-tasks normalize`.

As above, locale files outside the ViewComponent directory are not modified.



75
76
77
78
79
80
81
# File 'lib/i18n_tasks/plugin/view_component/filesystem.rb', line 75

def write_tree(path, tree, sort = nil)
  if component_locale_data?(path)
    super(path, tree_without_component_keys(path, tree))
  else
    super
  end
end