Module: Babelphish::Translator

Defined in:
lib/babelphish/translator.rb

Class Method Summary collapse

Class Method Details

.translate(text, to, from = 'en') ⇒ Object



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
# File 'lib/babelphish/translator.rb', line 71

def translate(text, to, from = 'en')

  return if to == from

  require 'cgi'
  require 'json'
  require 'net/http'

  base = 'http://ajax.googleapis.com/ajax/services/language/translate' 
  # assemble query params
  params = {
    :langpair => "#{from}|#{to}", 
    :q => text,
    :v => 1.0  
  }
  query = params.map{ |k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
  # send get request
  response = Net::HTTP.get_response( URI.parse( "#{base}?#{query}" ) )
  json = JSON.parse( response.body )
  if json['responseStatus'] == 200
    json['responseData']['translatedText']
  else
    puts "A problem occured while translating from #{from} to #{to}.  To retry only this translation try: babelphish -o -y #{@yml} -t #{to} to babelphish.  Response: #{response}"
  end
end

.translate_and_write_yml(yml, to, from, overwrite) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/babelphish/translator.rb', line 31

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
  translated_yml = YAML.load_file(yml)
  translate_keys(translated_yml, to, from)
  # change the top level key from the source language to the destination language
  translated_yml[to] = translated_yml[from]
  translated_yml.delete(from)
  File.open(translated_filename, 'w') { |f| f.write(translated_yml.ya2yaml) }
end

.translate_keys(translate_hash, to, from) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/babelphish/translator.rb', line 44

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]
        # pull out all the string substitutions so that google doesn't translate those
        pattern = /\{\{.+\}\}/
        holder = '{{---}}'
        replacements = translate_hash[key].scan(pattern)
        translate_hash[key].gsub!(pattern, holder)
        translate_hash[key] = translate(translate_hash[key], to, from)
        replacements.each do |r|
          translate_hash[key].sub!(holder, r)
        end
      else
        puts "Key #{key} contains no data"
      end
    end
  end
end

.translate_yaml(yml, overwrite = false, translate_to = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/babelphish/translator.rb', line 10

def translate_yaml(yml, overwrite = false, translate_to = nil)
  @yml = yml
  $KCODE = 'UTF8'
  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
    Babelphish::GoogleTranslate::LANGUAGES.each do |to|
      puts "Translating #{language} to #{to}"
      translate_and_write_yml(yml, to, language, overwrite)
      puts "Finished translating #{language} to #{to}"
    end
  end
end