class I18nTyml::TranslationManager

Public Class Methods

new(options) click to toggle source
# File lib/i18n_tyml.rb, line 8
def initialize(options)
  @dest_path = options[:path]
  @remove_default = options[:remove_default]
  @existed_translations = Hash.new
end

Public Instance Methods

search_missing(lang=nil) click to toggle source
# File lib/i18n_tyml.rb, line 14
def search_missing(lang=nil)
  collect_translations

  languages = lang ? [lang] : existed_trans.keys
  get_missing(collect_trans_in_erb(), languages )
end

Private Instance Methods

add_translations(trs) click to toggle source
# File lib/i18n_tyml.rb, line 27
def add_translations(trs)
  existed_trans.deep_safe_merge!(trs)
end
collect_trans_in_erb() click to toggle source
# File lib/i18n_tyml.rb, line 44
def collect_trans_in_erb
  target_files().each_with_object({}) do |file, trans|
    trs_in_file = translation_refs_in(file)
    if trs_in_file.any?
      trans[file] = trs_in_file
    end
  end
end
collect_translations() click to toggle source
# File lib/i18n_tyml.rb, line 32
def collect_translations
  locales_pathes = ["config/locales/**/*.yml",
                    "vendor/plugins/**/config/locales/**/*yml",
                    "vendor/plugins/**/locale/**/*yml"]
  locales_pathes.each do |path|
    Dir.glob(path) do |file|
      file_content = translations_in_file(file)
      add_translations(file_content) if file_content
    end
  end
end
existed_trans() click to toggle source
# File lib/i18n_tyml.rb, line 23
def existed_trans
  @existed_translations
end
get_missing(trans_in_code, languages) click to toggle source
# File lib/i18n_tyml.rb, line 88
def get_missing(trans_in_code, languages)
  languages.each_with_object({}) do |lang, missing|
    get_missing_for_lang(trans_in_code, lang).each do |file, translation_refs|
      missing[file] ||= []
      missing[file].concat(translation_refs).uniq!
    end
  end
end
get_missing_for_lang(trans_in_code, lang) click to toggle source
# File lib/i18n_tyml.rb, line 97
def get_missing_for_lang(trans_in_code, lang)
  trans_in_code.map do |file, trans_with_defaults|
    unmatched = trans_with_defaults.select {|q| has_trans?(lang, q[0]) == false  }
    [file, unmatched.map { |q| [i18n_label(lang, q[0]), q[1]] }] if unmatched.size > 0
  end.compact
end
has_trans?(lang, i18n_key) click to toggle source
# File lib/i18n_tyml.rb, line 105
def has_trans?(lang, i18n_key)
  existed_trans[lang].has_nested_key?(i18n_key)
end
i18n_label(lang, query) click to toggle source
# File lib/i18n_tyml.rb, line 109
def i18n_label(lang, query)
  "#{lang}.#{query}"
end
read_content_of_file(file) click to toggle source
# File lib/i18n_tyml.rb, line 63
def read_content_of_file(file)
  File.read(File.expand_path(file))
end
target_files() click to toggle source
# File lib/i18n_tyml.rb, line 53
def target_files
  path = @dest_path
  path = path[0...-1] if path[-1..-1] == '/'
  if File.directory?(path)
    [Dir.glob("#{path}/**/*.erb"), Dir.glob("#{path}/**/*.rb") ].flatten
  else
    [path]
  end
end
translation_refs_in(file) click to toggle source
# File lib/i18n_tyml.rb, line 72
def translation_refs_in(file)
  file_content = read_content_of_file(file)
  results = I18nTyml::RubyI18nParser.get_locale_ref_array(file_content)
  write_content_to_file(file, I18nTyml::RubyI18nParser.remove_default(file_content)) if @remove_default
  results
end
translations_in_file(yaml_file) click to toggle source
# File lib/i18n_tyml.rb, line 83
def translations_in_file(yaml_file)
  open(yaml_file) { |f| YAML.load(f.read) }
end
write_content_to_file(file, output) click to toggle source
# File lib/i18n_tyml.rb, line 67
def write_content_to_file(file, output)
  File.open(file, "w") { |f| f.puts output  }
end