module ChefCore::Text

Constants

DEFAULT_LOCALIZATION_PATH

Public Class Methods

add_gem_localization(gem_name) click to toggle source
# 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) click to toggle source
# 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!() click to toggle source
# 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!() click to toggle source

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

# File lib/chef_core/text.rb, line 30
def self.reset!
  @localization_paths = []
  @raw_localization_paths = []
  add_localization(DEFAULT_LOCALIZATION_PATH)
end