Module: Babelphish::YmlTranslator
- Defined in:
- lib/babelphish/yml_translator.rb
Constant Summary collapse
- SUBSTITUTION_PLACE_HOLDER =
'{{---}}'
- SUBSTITUTION_PLACE_HOLDER_2 =
'-.-.-'
Class Method Summary collapse
- .add_substitutions(translate_text) ⇒ Object
- .extract_yml_translation(source, translated_source, extracted, language) ⇒ Object
- .remove_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
.add_substitutions(translate_text) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/babelphish/yml_translator.rb', line 128 def add_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) # Pull out all the translation blocks so Google doesn't mess with those pattern = /%\{.+?\}/ @replacements2 = translate_text.scan(pattern) translate_text.gsub!(pattern, SUBSTITUTION_PLACE_HOLDER_2) end |
.extract_yml_translation(source, translated_source, extracted, language) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/babelphish/yml_translator.rb', line 94 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 |
.remove_substitutions(translate_text) ⇒ Object
140 141 142 143 144 145 146 147 148 |
# File 'lib/babelphish/yml_translator.rb', line 140 def remove_substitutions(translate_text) return '' if !translate_text @replacements.each do |r| translate_text.sub!(SUBSTITUTION_PLACE_HOLDER, r) end @replacements2.each do |r| translate_text.sub!(SUBSTITUTION_PLACE_HOLDER_2, r) end 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.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/babelphish/yml_translator.rb', line 17 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/babelphish/yml_translator.rb', line 75 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
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/babelphish/yml_translator.rb', line 33 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/babelphish/yml_translator.rb', line 46 def translate_keys(translate_hash, to, from) if translate_hash.is_a?(String) add_substitutions(translate_hash) translate_hash = Babelphish::Translator.translate(translate_hash, to, from) remove_substitutions(translate_hash) elsif translate_hash.is_a?(Array) translate_hash.map!{ |x| if !x.nil? then Babelphish::Translator.translate(x, to, from) else "" end } elsif translate_hash.is_a?(Hash) translate_hash.each_key do |key| next if translate_hash[key].is_a?(Fixnum) if translate_hash[key].is_a?(Hash) || translate_hash[key].is_a?(Array) 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? add_substitutions(translate_hash[key]) translate_hash[key] = Babelphish::Translator.translate(translate_hash[key], to, from) remove_substitutions(translate_hash[key]) else puts "Key #{key} contains no data" end end end end end |
.translate_many_yml_keys(translate_hash, tos, from) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/babelphish/yml_translator.rb', line 105 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? add_substitutions(translate_hash[key]) translations = Babelphish::Translator.multiple_translate(translate_hash[key], tos, from) translations.each_key do |locale| remove_substitutions(translations[locale]) end translate_hash[key] = translations else puts "Key #{key} contains no data" end end end end |