Class: YamlTranslate::Translator
- Inherits:
-
Object
- Object
- YamlTranslate::Translator
- Defined in:
- lib/yaml_translate/translator.rb
Overview
Class handling the translation
Constant Summary collapse
- SUBSTITUTION_PLACE_HOLDER =
'%---%'
- SUBSTITUTION_PLACE_HOLDER_2 =
'-.-.-'
Instance Method Summary collapse
- #add_substitutions(translate_text) ⇒ Object
-
#initialize(from, to, yml) ⇒ Translator
constructor
Initializes a new YamlTranslate::Translator instance when provided with *
from
- the source language (e,g, ‘en’) *to
- an Array containing the destinations languages (e.g. [‘de’, ‘fr’]) *yml
- the filename of the yaml-file to translate. - #remove_substitutions(translate_text) ⇒ Object
- #translate! ⇒ Object
- #translate_keys(translate_hash, to, from) ⇒ Object
Constructor Details
#initialize(from, to, yml) ⇒ Translator
Initializes a new YamlTranslate::Translator instance when provided with
-
from
- the source language (e,g, ‘en’) -
to
- an Array containing the destinations languages (e.g. [‘de’, ‘fr’]) -
yml
- the filename of the yaml-file to translate
35 36 37 38 39 40 |
# File 'lib/yaml_translate/translator.rb', line 35 def initialize from, to, yml @from = from @to = to @yml = yml @translator = Google::Translator.new end |
Instance Method Details
#add_substitutions(translate_text) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/yaml_translate/translator.rb', line 101 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.to_s.scan(pattern) translate_text.to_s.gsub!(pattern, SUBSTITUTION_PLACE_HOLDER) # Pull out all the translation blocks so Google doesn't mess with those pattern = /%\{.+?\}/ @replacements2 = translate_text.to_s.scan(pattern) translate_text.to_s.gsub!(pattern, SUBSTITUTION_PLACE_HOLDER_2) end |
#remove_substitutions(translate_text) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/yaml_translate/translator.rb', line 113 def remove_substitutions(translate_text) return '' if !translate_text @replacements.each do |r| translate_text.to_s.gsub!(SUBSTITUTION_PLACE_HOLDER, r) end @replacements2.each do |r| translate_text.to_s.gsub!(SUBSTITUTION_PLACE_HOLDER_2, r) end end |
#translate! ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/yaml_translate/translator.rb', line 42 def translate! raise "Yaml file #{@yml} not found" unless File.exist? @yml begin @to.each do |lang| puts "\nTranslating to '#{lang}'" translated_filename = File.join(File.dirname(@yml), "#{lang}.yml") source = YAML.load_file @yml @keys = source.keys.size @running = 0 translate_keys(source, lang, @from) File.open(translated_filename, 'w') { |f| f.write(source.ya2yaml) } end rescue => e puts "\nTranslation process fucked up!!: #{e.backtrace}" end puts "\nDone, exiting" end |
#translate_keys(translate_hash, to, from) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/yaml_translate/translator.rb', line 62 def translate_keys(translate_hash, to, from) if translate_hash.is_a?(String) add_substitutions(translate_hash) translate_hash = @translator.translate(from, to, translate_hash)[0] remove_substitutions(translate_hash) elsif translate_hash.is_a?(Array) translate_hash.map! { |x| if !x.nil? then @translator.translate(from, to, x)[0] 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]) @running += 1 print "#{@running}/#{@keys}\r" #print "[#{key}] #{translate_hash[key]} : " res = @translator.translate(from, to, translate_hash[key])[0] translate_hash[key] = res #print " #{res}\n" remove_substitutions(translate_hash[key]) else puts "Key #{key} contains no data" end end end end end |