Module: Babelphish::YmlTranslator
- Defined in:
- lib/babelphish/yml_translator.rb
Constant Summary collapse
- SUBSTITUTION_PLACE_HOLDER =
'{{---}}'
Class Method Summary collapse
- .extract_yml_translation(source, translated_source, extracted, language) ⇒ Object
- .parse_substitutions(translate_text) ⇒ Object
-
.translate(yml, overwrite = false, translate_to = nil, tos = nil) ⇒ Object
Translates the given yml file into the specified languages.
- .translate_and_write_many_yml(yml, tos, from, overwrite) ⇒ Object
- .translate_and_write_yml(yml, to, from, overwrite) ⇒ Object
- .translate_keys(translate_hash, to, from) ⇒ Object
- .translate_many_yml_keys(translate_hash, tos, from) ⇒ Object
Class Method Details
.extract_yml_translation(source, translated_source, extracted, language) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/babelphish/yml_translator.rb', line 86 def extract_yml_translation(source, translated_source, extracted, language) source.each_key do |key| if source[key].is_a?(Hash) extracted[key] = {} extract_yml_translation(source[key], translated_source[key], extracted[key], language) else extracted[key] = translated_source[key][language] end end end |
.parse_substitutions(translate_text) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/babelphish/yml_translator.rb', line 122 def parse_substitutions(translate_text) # pull out all the string substitutions so that google doesn't translate those pattern = /\{\{.+?\}\}/ # non greedy pattern match so that we properly match strings like: "{{name}} on {{application_name}}" replacements = translate_text.scan(pattern) translate_text.gsub!(pattern, SUBSTITUTION_PLACE_HOLDER) replacements end |
.translate(yml, overwrite = false, translate_to = nil, tos = nil) ⇒ Object
Translates the given yml file into the specified languages. Will attempt to auto detect the from langauge using the name of the yml file. yml - Path to the yml file to be translated overwrite - Boolean indicating whether or not existing translations should be overwritten. translate_to - A single language to translate the file into. (Valid values are specified in languages.rb)
When this value is nil tos or Babelphish::GoogleTranslate::LANGUAGES is used to determine the languages
tos - An array containing the languages to translate the yml file into. If nil or not specified then
Babelphish::GoogleTranslate::LANGUAGES is used.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/babelphish/yml_translator.rb', line 16 def translate(yml, overwrite = false, translate_to = nil, tos = nil) @yml = yml language = File.basename(yml, ".yml") if !Babelphish::GoogleTranslate::LANGUAGES.include?(language) STDERR.puts "#{language} is not one of the available languages. Please choose a standard localized yml file. i.e. en.yml." return end if translate_to puts "Translating #{language} to #{translate_to}" translate_and_write_yml(yml, translate_to, language, overwrite) puts "Finished translating #{language} to #{translate_to}" else translate_and_write_many_yml(yml, tos, language, overwrite) end end |
.translate_and_write_many_yml(yml, tos, from, overwrite) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/babelphish/yml_translator.rb', line 67 def translate_and_write_many_yml(yml, tos, from, overwrite) return unless File.exist?(yml) source = YAML.load_file(yml) translated_source = YAML.load_file(yml) translate_many_yml_keys(translated_source, tos, from) # At this point translated_source contains a translation for every language. Cut it apart into individual hashes tos.each do |to| next if to == from # don't want to overwrite the source file extracted_translation = {} extract_yml_translation(source, translated_source, extracted_translation, to) # change the top level key from the source language to the destination language translated_filename = File.join(File.dirname(yml), "#{to}.yml") return if File.exist?(translated_filename) && !overwrite extracted_translation[to] = extracted_translation[from] extracted_translation.delete(from) File.open(translated_filename, 'w') { |f| f.write(extracted_translation.ya2yaml) } end end |
.translate_and_write_yml(yml, to, from, overwrite) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/babelphish/yml_translator.rb', line 32 def translate_and_write_yml(yml, to, from, overwrite) return if to == from return unless File.exist?(yml) translated_filename = File.join(File.dirname(yml), "#{to}.yml") return if File.exist?(translated_filename) && !overwrite source = YAML.load_file(yml) translate_keys(source, to, from) # change the top level key from the source language to the destination language source[to] = source[from] source.delete(from) File.open(translated_filename, 'w') { |f| f.write(source.ya2yaml) } end |
.translate_keys(translate_hash, to, from) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/babelphish/yml_translator.rb', line 45 def translate_keys(translate_hash, to, from) translate_hash.each_key do |key| if translate_hash[key].is_a?(Hash) translate_keys(translate_hash[key], to, from) else if key == false puts "Key #{key} was evaluated as false. Check your yml file and be sure to escape values like no with 'no'. ie 'no': 'No'" elsif key == true puts "Key #{key} was evaluated as true. Check your yml file and be sure to escape values like yes with 'yes'. ie 'yes': 'Yes'" elsif !translate_hash[key].nil? replacements = parse_substitutions(translate_hash[key]) translate_hash[key] = Babelphish::Translator.translate(translate_hash[key], to, from) replacements.each do |r| translate_hash[key].sub!(SUBSTITUTION_PLACE_HOLDER, r) end else puts "Key #{key} contains no data" end end end end |
.translate_many_yml_keys(translate_hash, tos, from) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/babelphish/yml_translator.rb', line 97 def translate_many_yml_keys(translate_hash, tos, from) translate_hash.each_key do |key| if translate_hash[key].is_a?(Hash) translate_many_yml_keys(translate_hash[key], tos, from) else if key == false puts "Key #{key} was evaluated as false. Check your yml file and be sure it does not include values like no: No" elsif key == true puts "Key #{key} was evaluated as true. Check your yml file and be sure it does not include values like yes: Yes" elsif !translate_hash[key].nil? replacements = parse_substitutions(translate_hash[key]) translations = Babelphish::Translator.multiple_translate(translate_hash[key], tos, from) translations.each_key do |locale| replacements.each do |r| translations[locale].sub!(SUBSTITUTION_PLACE_HOLDER, r) end end translate_hash[key] = translations else puts "Key #{key} contains no data" end end end end |