Module: WebStuff::YamlTranslations

Defined in:
lib/web_stuff/yaml_translations.rb

Class Method Summary collapse

Class Method Details

.translations_locale_dir(dir_path, from_locale, to_locale) ⇒ Object

Raises:

  • (Exception)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/web_stuff/yaml_translations.rb', line 8

def self.translations_locale_dir(dir_path, from_locale, to_locale)
  raise Exception.new("Invalid dir path") unless File.directory? dir_path
  puts "translations_locale_dir"

  Dir[File.join(dir_path, "**/*.#{from_locale}.yml"), File.join(dir_path, "**/#{from_locale}.yml")].each do |locale_path|
    if File.basename(locale_path) == "#{from_locale}.yml"
      dest_file = File.join(dir_path, "#{to_locale}.yml")
    else
      dest_file = File.join(dir_path, "#{File.basename(locale_path, ".#{from_locale}.yml")}.#{to_locale}.yml")
    end
    if File.exists?(dest_file)
      puts "[skip] #{locale_path}"
      next
    end
    puts "---#{locale_path}"
    hash = YAML.load_file(locale_path)
    result_hash = self.traverse_hash(hash, from_locale, to_locale)
    Common::Util.write_file(dest_file, Common::Util.yaml(result_hash))
  end
end