Module: ChefCore::Text

Defined in:
lib/chef_core/text.rb,
lib/chef_core/text/text_wrapper.rb,
lib/chef_core/text/error_translation.rb

Defined Under Namespace

Classes: ErrorTranslation, TextWrapper

Constant Summary collapse

DEFAULT_LOCALIZATION_PATH =
File.absolute_path(File.join(File.dirname(__FILE__), "..", "..", "i18n"))

Class Method Summary collapse

Class Method Details

.add_gem_localization(gem_name) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/chef_core/text.rb', line 49

def self.add_gem_localization(gem_name)
  spec = Gem::Specification.find_by_name(gem_name)
  path = File.join(spec.gem_dir, "i18n")
  if File.directory? path
    add_localization(path)
  end
end

.add_localization(base_path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chef_core/text.rb', line 36

def self.add_localization(base_path)
  return if @raw_localization_paths.include? base_path

  # @localization_paths will get modified by R18n, so we'll
  # keep them as strings as well, to ensure we can avoid duplicate loading.
  errors_path = File.join(base_path, "errors")
  @localization_paths << base_path
  @localization_paths << errors_path
  @raw_localization_paths << base_path
  @raw_localization_paths << errors_path
  reload!
end

.reload!Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chef_core/text.rb', line 57

def self.reload!
  R18n.reset!
  R18n.from_env(@localization_paths)
  t = R18n.get.t
  t.translation_keys.each do |k|
    k = k.to_sym
    define_singleton_method k do |*args|
      # If this is a top-level entry without children
      # (such as 'cancel') it will have no translation
      # keys and doees not need a wrapper
      tree = t.send(k, *args)
      if tree.methods.include?(:translation_keys)
        TextWrapper.new(tree)
      else
        tree.to_s
      end
    end
  end
end

.reset!Object

Set up this gem’s localization as the only one present



30
31
32
33
34
# File 'lib/chef_core/text.rb', line 30

def self.reset!
  @localization_paths = []
  @raw_localization_paths = []
  add_localization(DEFAULT_LOCALIZATION_PATH)
end