babelphish 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/babelphish +5 -7
- data/lib/babelphish.rb +1 -1
- data/lib/babelphish/html_translator.rb +30 -4
- data/lib/tasks/babelphish.rake +3 -1
- data/test/test_html_translator.rb +43 -0
- metadata +1 -1
data/bin/babelphish
CHANGED
@@ -14,10 +14,8 @@ OptionParser.new do |opts|
|
|
14
14
|
opts.on('-t', '--translate_to to', "Only translate from the source file to a specific language (ie -t es will only translate to Spanish)") { |to| ENV['translate_to'] = to }
|
15
15
|
end.parse!
|
16
16
|
|
17
|
-
Babelphish.load_tasks
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
# STDERR.puts "Can't find Rakefile. Are we in a Rails folder?"
|
23
|
-
# end
|
17
|
+
if Babelphish.load_tasks
|
18
|
+
Rake::Task[task].invoke
|
19
|
+
else
|
20
|
+
STDERR.puts "Can't find Rakefile. Are we in a Rails folder?"
|
21
|
+
end
|
data/lib/babelphish.rb
CHANGED
@@ -32,8 +32,18 @@ module Babelphish
|
|
32
32
|
# Translate a single page from the language specified in 'from'
|
33
33
|
# into the languages specified by 'tos'
|
34
34
|
def translate_and_write_page(source_page, tos, from, overwrite)
|
35
|
-
|
36
|
-
|
35
|
+
if File.exist?(source_page)
|
36
|
+
STDERR.puts "Translating: #{source_page}"
|
37
|
+
else
|
38
|
+
STDERR.puts "Could not find file: #{source_page}"
|
39
|
+
return
|
40
|
+
end
|
41
|
+
|
42
|
+
if !translate_file?(source_page)
|
43
|
+
STDERR.puts "Not translating file: #{source_page}"
|
44
|
+
return
|
45
|
+
end
|
46
|
+
|
37
47
|
text = IO.read(source_page)
|
38
48
|
|
39
49
|
# Pull out all the code blocks to Google doesn't mess with those
|
@@ -62,12 +72,28 @@ module Babelphish
|
|
62
72
|
|
63
73
|
end
|
64
74
|
|
75
|
+
# Generate a file name for the newly translated content
|
65
76
|
def get_translated_file(page, to)
|
66
|
-
page.gsub('.html', ".#{to}.html")
|
77
|
+
new_page = page.gsub('.html', ".#{to}.html")
|
78
|
+
new_page.gsub!('text.html', "text.#{to}.html")
|
79
|
+
new_page.gsub!('text.plain', "text.#{to}.plain")
|
80
|
+
new_page
|
67
81
|
end
|
68
82
|
|
83
|
+
# This is a hack but all the translated files live in the same directory
|
84
|
+
# as the original file so we have to have some way of not translating the
|
85
|
+
# translated files.
|
86
|
+
# this should return true for
|
87
|
+
# test.html.erb, test.text.html.erb and test.text.plain.erb
|
88
|
+
# and false for
|
89
|
+
# test.es.html.erb, test.text.es.html.erb and test.text.es.plain.erb
|
69
90
|
def translate_file?(page)
|
70
|
-
|
91
|
+
test = page
|
92
|
+
test = test.gsub('./', '') if page[0..1] == './'
|
93
|
+
test = test.gsub('.text.html.erb', '')
|
94
|
+
test = test.gsub('.text.plain.erb', '')
|
95
|
+
test = test.gsub('.html.erb', '')
|
96
|
+
test.split('.').length == 1
|
71
97
|
end
|
72
98
|
|
73
99
|
end
|
data/lib/tasks/babelphish.rake
CHANGED
@@ -9,12 +9,14 @@ task :babelphish do
|
|
9
9
|
translate_to = ENV['translate_to'] || nil
|
10
10
|
|
11
11
|
if yml
|
12
|
+
STDERR.puts "Translating #{yml}"
|
12
13
|
Babelphish::YmlTranslator.translate(yml, overwrite, translate_to)
|
13
14
|
elsif html
|
14
|
-
if language.
|
15
|
+
if language.nil?
|
15
16
|
STDERR.puts "No source language specified when using 'html' option. Defaulting to 'en'"
|
16
17
|
language = 'en'
|
17
18
|
end
|
19
|
+
STDERR.puts "Translating files in #{html}"
|
18
20
|
Babelphish::HtmlTranslator.translate(html, Babelphish::GoogleTranslate::LANGUAGES, language, overwrite)
|
19
21
|
else
|
20
22
|
STDERR.puts "Please specify the directory where your files live. i.e. babelphish -y ./my/locales or babelphish -h ./some/directory/with/erb_or_html/files"
|
@@ -19,4 +19,47 @@ class TestHtmlTranslator < Test::Unit::TestCase
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
+
def test_text_html_translation
|
23
|
+
overwrite = true
|
24
|
+
translate_from = 'en'
|
25
|
+
directory = File.join(File.dirname(__FILE__), 'html_translations')
|
26
|
+
Babelphish::HtmlTranslator.translate(directory, Babelphish::GoogleTranslate::LANGUAGES, translate_from, overwrite)
|
27
|
+
Babelphish::GoogleTranslate::LANGUAGES.each do |to|
|
28
|
+
if to != translate_from
|
29
|
+
# This will make sure the file was created
|
30
|
+
translated_html = File.join(File.dirname(__FILE__), 'html_translations', "test.text.#{to}.html.erb")
|
31
|
+
text = IO.read(translated_html)
|
32
|
+
# Make sure the translation didn't remove code
|
33
|
+
assert text.include?("<%= 'Some ruby code' %>")
|
34
|
+
assert text.include?('<%= "test something else #{variable}" %>')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_plain_translation
|
40
|
+
overwrite = true
|
41
|
+
translate_from = 'en'
|
42
|
+
directory = File.join(File.dirname(__FILE__), 'html_translations')
|
43
|
+
Babelphish::HtmlTranslator.translate(directory, Babelphish::GoogleTranslate::LANGUAGES, translate_from, overwrite)
|
44
|
+
Babelphish::GoogleTranslate::LANGUAGES.each do |to|
|
45
|
+
if to != translate_from
|
46
|
+
# This will make sure the file was created
|
47
|
+
translated_html = File.join(File.dirname(__FILE__), 'html_translations', "test.text.#{to}.plain.erb")
|
48
|
+
text = IO.read(translated_html)
|
49
|
+
# Make sure the translation didn't remove code
|
50
|
+
assert text.include?("<%= 'Some ruby code' %>")
|
51
|
+
assert text.include?('<%= "test something else #{variable}" %>')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_translate_file
|
57
|
+
assert Babelphish::HtmlTranslator.translate_file?('test.html.erb')
|
58
|
+
assert Babelphish::HtmlTranslator.translate_file?('test.text.html.erb')
|
59
|
+
assert Babelphish::HtmlTranslator.translate_file?('test.text.plain.erb')
|
60
|
+
assert !Babelphish::HtmlTranslator.translate_file?('test.es.html.erb')
|
61
|
+
assert !Babelphish::HtmlTranslator.translate_file?('test.text.es.html.erb')
|
62
|
+
assert !Babelphish::HtmlTranslator.translate_file?('test.text.es.plain.erb')
|
63
|
+
end
|
64
|
+
|
22
65
|
end
|