Module: Babelphish::HtmlTranslator

Defined in:
lib/babelphish/html_translator.rb

Class Method Summary collapse

Class Method Details

.get_translated_file(page, to) ⇒ Object

Generate a file name for the newly translated content


95
96
97
98
99
100
# File 'lib/babelphish/html_translator.rb', line 95

def get_translated_file(page, to)
  new_page = page.gsub('.html', ".#{to}.html")
  new_page.gsub!('text.html', "text.#{to}.html")
  new_page.gsub!('text.plain', "text.#{to}.plain")
  new_page
end

.translate(path, translate_tos, translate_from = 'en', overwrite = false) ⇒ Object

Translates all files in the given path from the language specififed by 'translate_from' into the languages in 'translate_tos'. Translations that already exist will not be overwritten unless overwrite = true


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/babelphish/html_translator.rb', line 9

def translate(path, translate_tos, translate_from = 'en', overwrite = false)
  @path = path
  if !Babelphish::GoogleTranslate::LANGUAGES.include?(translate_from)
    STDERR.puts "#{translate_from} is not one of the available languages."
    return
  end
  if !File.exists?(path)
    STDERR.puts "#{path} does not exist."
    return
  end
  translate_and_write_pages(path, translate_tos, translate_from, overwrite)        
end

.translate_and_write_page(source_page, tos, from, overwrite) ⇒ Object

Translate a single page from the language specified in 'from' into the languages specified by 'tos'


34
35
36
37
38
39
40
41
42
43
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/babelphish/html_translator.rb', line 34

def translate_and_write_page(source_page, tos, from, overwrite)
  if File.exist?(source_page)
    STDERR.puts "Translating: #{source_page}"
  else
    STDERR.puts "Could not find file: #{source_page}"
    return
  end
  
  if !translate_file?(source_page)
    STDERR.puts "Not translating file: #{source_page}"
    return
  end
  
  text = IO.read(source_page)
  
  begins_with_html = text['<html>']
  
  # Pull out all the code blocks so Google doesn't mess with those
  pattern = /\<\%.+\%\>/
  holder = '{{---}}'
  replacements = text.scan(pattern)
  text.gsub!(pattern, holder)
  
  # Pull out all the new lines so Google doesn't mess with those
  pattern = /\n/
  newline_holder = '<brr />'
  newline_replacements = text.scan(pattern)
  text.gsub!(pattern, newline_holder)
  
  # Send to Google for translations
  translations = Babelphish::Translator.multiple_translate(text, tos, from)
  
  # Put the code back
  translations.each_key do |locale|
    replacements.each do |r|
      translations[locale].sub!(holder, r)
    end
  end

  # Put the newlines back in
  translations.each_key do |locale|
    newline_replacements.each do |r|
      translations[locale].sub!(newline_holder, r)
      if translations[locale]['<html>']
        # Google translate can insert '<html>' at the beginning of the result.  Remove it.
        translations[locale]['<html>']= '' unless begins_with_html
      end
    end
  end
  
  # Write the new file
  translations.each_key do |locale|
    translated_filename = get_translated_file(source_page, locale)
    if (locale != from) && (overwrite || !File.exists?(translated_filename))
      File.open(translated_filename, 'w') { |f| f.write(translations[locale]) }
    end
  end

end

.translate_and_write_pages(path, tos, from, overwrite) ⇒ Object


22
23
24
25
26
27
28
29
30
# File 'lib/babelphish/html_translator.rb', line 22

def translate_and_write_pages(path, tos, from, overwrite)
  Dir.glob("#{path}/*").each do |next_file|
    if File.directory?(next_file)
      translate_and_write_pages(next_file, language)
    else
      translate_and_write_page(next_file, tos, from, overwrite)
    end
  end
end

.translate_file?(page) ⇒ Boolean

This is a hack but all the translated files live in the same directory as the original file so we have to have some way of not translating the translated files. this should return true for test.html.erb, test.text.html.erb and test.text.plain.erb and false for test.es.html.erb, test.text.es.html.erb and test.text.es.plain.erb

Returns:

  • (Boolean)

109
110
111
112
113
114
115
116
# File 'lib/babelphish/html_translator.rb', line 109

def translate_file?(page)
  test = page
  test = test.gsub('./', '') if page[0..1] == './'
  test = test.gsub('.text.html.erb', '')
  test = test.gsub('.text.plain.erb', '')
  test = test.gsub('.html.erb', '')
  test.split('.').length == 1
end