class I18nChecker::Locale::File

Translation file for each language

@attr_reader [String] file_name Translation file path @attr_reader [String] lang language @attr_reader [Hash<String,String>] locale_texts translation texts

Attributes

file_name[R]
lang[R]
locale_texts[R]

Public Class Methods

load_yaml_file(yaml_file) click to toggle source

Read translation file

@param yaml_file [String] Translation file path @return [I18nChecker::Locale::File] Translation file

# File lib/i18n_checker/locale/file.rb, line 18
def load_yaml_file(yaml_file)
  new(yaml_file, YAML.load(::File.open(yaml_file, &:read)))
end
new(yaml_file, locale_texts = {}) click to toggle source

Create a translation file

@param yaml_file [String] Translation file path @param locale_texts [Hash<String,String>] translation text

# File lib/i18n_checker/locale/file.rb, line 27
def initialize(yaml_file, locale_texts = {})
  lang = locale_texts.keys.first
  @lang = lang.to_sym
  @locale_texts = compact_of(locale_texts[lang] || {})
  @file_name = yaml_file
end

Public Instance Methods

empty?() click to toggle source
# File lib/i18n_checker/locale/file.rb, line 70
def empty?
  locale_texts.empty?
end
include?(locale_text) click to toggle source

Check for translated text

@param locale_text [Stirng] Translation text key @return [Boolean] Returns true if there is translated text

# File lib/i18n_checker/locale/file.rb, line 38
def include?(locale_text)
  @locale_texts.key?(locale_text.text)
end
remove_texts(locale_texts) click to toggle source

Deletes the specified translation text

@param locale_text [Array<Stirng>] Translation text key @return [I18nChecker::Locale::File] New translation file

# File lib/i18n_checker/locale/file.rb, line 46
def remove_texts(locale_texts)
  registry = locale_texts.map { |locale_text| [locale_text, true] }.to_h

  current_locale_texts = @locale_texts.dup
  current_locale_texts.delete_if { |locale_text| registry.key?(locale_text) }

  remain_texts = {}
  remain_texts[@lang] = tree_of(current_locale_texts)

  self.class.new(file_name, remain_texts)
end
save() click to toggle source

Save the translation file

@return [I18nChecker::Locale::File] translation file

# File lib/i18n_checker/locale/file.rb, line 61
def save
  locale = {}
  locale[@lang.to_s] = tree_of(locale_texts)
  yaml_file = ::File.open(file_name, 'w')
  yaml_file.write(YAML.dump(locale))
  yaml_file.close
  self
end

Private Instance Methods

compact_of(values = {}, path = KeyPath.new) click to toggle source
# File lib/i18n_checker/locale/file.rb, line 76
def compact_of(values = {}, path = KeyPath.new)
  result = {}
  values.each_with_index do |(i, v)|
    path.move_to(i)
    if v.is_a?(Hash)
      result.merge!(compact_of(v, path))
    else
      result[path.to_s] = v
    end
    path.leave
  end
  result
end
tree_of(values = []) click to toggle source
# File lib/i18n_checker/locale/file.rb, line 90
def tree_of(values = [])
  result = {}
  values.each do |path, value|
    dest = nil
    paths = path.split('.')
    last_key = paths.pop
    dest = result
    paths.each do |p|
      dest[p] = {} unless dest.key?(p)
      dest = dest[p]
    end
    dest[last_key] = value
  end
  result
end