babelphish 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/LICENSE +1 -1
- data/README.rdoc +91 -1
- data/Rakefile +17 -31
- data/VERSION +1 -0
- data/babelphish.gemspec +268 -0
- data/bin/babelphish +1 -0
- data/config/website.yml +2 -0
- data/doc/classes/Babelphish.html +206 -0
- data/doc/classes/Babelphish/Exceptions.html +111 -0
- data/doc/classes/Babelphish/Exceptions/GoogleResponseError.html +111 -0
- data/doc/classes/Babelphish/GoogleTranslate.html +565 -0
- data/doc/classes/Babelphish/HtmlTranslator.html +335 -0
- data/doc/classes/Babelphish/Translator.html +311 -0
- data/doc/classes/Babelphish/YmlTranslator.html +402 -0
- data/doc/created.rid +1 -0
- data/doc/files/History_txt.html +110 -0
- data/doc/files/PostInstall_txt.html +108 -0
- data/doc/files/README_rdoc.html +255 -0
- data/doc/files/bin/babelphish.html +109 -0
- data/doc/files/lib/babelphish/exceptions_rb.html +101 -0
- data/doc/files/lib/babelphish/html_translator_rb.html +101 -0
- data/doc/files/lib/babelphish/languages_rb.html +108 -0
- data/doc/files/lib/babelphish/translator_rb.html +101 -0
- data/doc/files/lib/babelphish/yml_translator_rb.html +101 -0
- data/doc/files/lib/babelphish_rb.html +113 -0
- data/doc/files/lib/tasks/babelphish_rake.html +147 -0
- data/doc/fr_class_index.html +33 -0
- data/doc/fr_file_index.html +37 -0
- data/doc/fr_method_index.html +43 -0
- data/doc/index.html +24 -0
- data/doc/rdoc-style.css +208 -0
- data/lib/babelphish.rb +1 -1
- data/lib/babelphish/html_translator.rb +6 -0
- data/lib/babelphish/translator.rb +8 -4
- data/lib/babelphish/yml_translator.rb +23 -14
- data/lib/tasks/babelphish.rake +25 -12
- data/script/txt2html +71 -0
- data/test/html_translations/test.ar.html.erb +4 -0
- data/test/html_translations/test.bg.html.erb +4 -0
- data/test/html_translations/test.ca.html.erb +4 -0
- data/test/html_translations/test.cs.html.erb +4 -0
- data/test/html_translations/test.da.html.erb +4 -0
- data/test/html_translations/test.de.html.erb +4 -0
- data/test/html_translations/test.el.html.erb +4 -0
- data/test/html_translations/test.es.html.erb +4 -0
- data/test/html_translations/test.et.html.erb +4 -0
- data/test/html_translations/test.fa.html.erb +4 -0
- data/test/html_translations/test.fi.html.erb +4 -0
- data/test/html_translations/test.fr.html.erb +4 -0
- data/test/html_translations/test.gl.html.erb +4 -0
- data/test/html_translations/test.hi.html.erb +4 -0
- data/test/html_translations/test.hr.html.erb +4 -0
- data/test/html_translations/test.html.erb +4 -0
- data/test/html_translations/test.hu.html.erb +4 -0
- data/test/html_translations/test.id.html.erb +4 -0
- data/test/html_translations/test.it.html.erb +4 -0
- data/test/html_translations/test.iw.html.erb +4 -0
- data/test/html_translations/test.ja.html.erb +4 -0
- data/test/html_translations/test.ko.html.erb +4 -0
- data/test/html_translations/test.lt.html.erb +4 -0
- data/test/html_translations/test.lv.html.erb +4 -0
- data/test/html_translations/test.mt.html.erb +4 -0
- data/test/html_translations/test.nl.html.erb +4 -0
- data/test/html_translations/test.no.html.erb +4 -0
- data/test/html_translations/test.pl.html.erb +4 -0
- data/test/html_translations/test.pt-PT.html.erb +4 -0
- data/test/html_translations/test.ro.html.erb +4 -0
- data/test/html_translations/test.ru.html.erb +4 -0
- data/test/html_translations/test.sk.html.erb +4 -0
- data/test/html_translations/test.sl.html.erb +4 -0
- data/test/html_translations/test.sq.html.erb +4 -0
- data/test/html_translations/test.sr.html.erb +4 -0
- data/test/html_translations/test.sv.html.erb +4 -0
- data/test/html_translations/test.text.ar.html.erb +5 -0
- data/test/html_translations/test.text.ar.plain.erb +8 -0
- data/test/html_translations/test.text.bg.html.erb +5 -0
- data/test/html_translations/test.text.bg.plain.erb +8 -0
- data/test/html_translations/test.text.ca.html.erb +5 -0
- data/test/html_translations/test.text.ca.plain.erb +8 -0
- data/test/html_translations/test.text.cs.html.erb +5 -0
- data/test/html_translations/test.text.cs.plain.erb +8 -0
- data/test/html_translations/test.text.da.html.erb +5 -0
- data/test/html_translations/test.text.da.plain.erb +8 -0
- data/test/html_translations/test.text.de.html.erb +5 -0
- data/test/html_translations/test.text.de.plain.erb +8 -0
- data/test/html_translations/test.text.el.html.erb +5 -0
- data/test/html_translations/test.text.el.plain.erb +8 -0
- data/test/html_translations/test.text.es.html.erb +5 -0
- data/test/html_translations/test.text.es.plain.erb +8 -0
- data/test/html_translations/test.text.et.html.erb +5 -0
- data/test/html_translations/test.text.et.plain.erb +8 -0
- data/test/html_translations/test.text.fa.html.erb +5 -0
- data/test/html_translations/test.text.fa.plain.erb +8 -0
- data/test/html_translations/test.text.fi.html.erb +5 -0
- data/test/html_translations/test.text.fi.plain.erb +8 -0
- data/test/html_translations/test.text.fr.html.erb +5 -0
- data/test/html_translations/test.text.fr.plain.erb +8 -0
- data/test/html_translations/test.text.gl.html.erb +5 -0
- data/test/html_translations/test.text.gl.plain.erb +8 -0
- data/test/html_translations/test.text.hi.html.erb +5 -0
- data/test/html_translations/test.text.hi.plain.erb +8 -0
- data/test/html_translations/test.text.hr.html.erb +5 -0
- data/test/html_translations/test.text.hr.plain.erb +8 -0
- data/test/html_translations/test.text.html.erb +5 -0
- data/test/html_translations/test.text.hu.html.erb +5 -0
- data/test/html_translations/test.text.hu.plain.erb +8 -0
- data/test/html_translations/test.text.id.html.erb +5 -0
- data/test/html_translations/test.text.id.plain.erb +8 -0
- data/test/html_translations/test.text.it.html.erb +5 -0
- data/test/html_translations/test.text.it.plain.erb +8 -0
- data/test/html_translations/test.text.iw.html.erb +5 -0
- data/test/html_translations/test.text.iw.plain.erb +8 -0
- data/test/html_translations/test.text.ja.html.erb +5 -0
- data/test/html_translations/test.text.ja.plain.erb +8 -0
- data/test/html_translations/test.text.ko.html.erb +5 -0
- data/test/html_translations/test.text.ko.plain.erb +8 -0
- data/test/html_translations/test.text.lt.html.erb +5 -0
- data/test/html_translations/test.text.lt.plain.erb +8 -0
- data/test/html_translations/test.text.lv.html.erb +5 -0
- data/test/html_translations/test.text.lv.plain.erb +8 -0
- data/test/html_translations/test.text.mt.html.erb +5 -0
- data/test/html_translations/test.text.mt.plain.erb +8 -0
- data/test/html_translations/test.text.nl.html.erb +5 -0
- data/test/html_translations/test.text.nl.plain.erb +8 -0
- data/test/html_translations/test.text.no.html.erb +5 -0
- data/test/html_translations/test.text.no.plain.erb +8 -0
- data/test/html_translations/test.text.pl.html.erb +5 -0
- data/test/html_translations/test.text.pl.plain.erb +8 -0
- data/test/html_translations/test.text.plain.erb +8 -0
- data/test/html_translations/test.text.pt-PT.html.erb +5 -0
- data/test/html_translations/test.text.pt-PT.plain.erb +8 -0
- data/test/html_translations/test.text.ro.html.erb +5 -0
- data/test/html_translations/test.text.ro.plain.erb +8 -0
- data/test/html_translations/test.text.ru.html.erb +5 -0
- data/test/html_translations/test.text.ru.plain.erb +8 -0
- data/test/html_translations/test.text.sk.html.erb +5 -0
- data/test/html_translations/test.text.sk.plain.erb +8 -0
- data/test/html_translations/test.text.sl.html.erb +5 -0
- data/test/html_translations/test.text.sl.plain.erb +8 -0
- data/test/html_translations/test.text.sq.html.erb +5 -0
- data/test/html_translations/test.text.sq.plain.erb +8 -0
- data/test/html_translations/test.text.sr.html.erb +5 -0
- data/test/html_translations/test.text.sr.plain.erb +8 -0
- data/test/html_translations/test.text.sv.html.erb +5 -0
- data/test/html_translations/test.text.sv.plain.erb +8 -0
- data/test/html_translations/test.text.th.html.erb +5 -0
- data/test/html_translations/test.text.th.plain.erb +8 -0
- data/test/html_translations/test.text.tl.html.erb +5 -0
- data/test/html_translations/test.text.tl.plain.erb +8 -0
- data/test/html_translations/test.text.tr.html.erb +5 -0
- data/test/html_translations/test.text.tr.plain.erb +8 -0
- data/test/html_translations/test.text.uk.html.erb +5 -0
- data/test/html_translations/test.text.uk.plain.erb +8 -0
- data/test/html_translations/test.text.vi.html.erb +5 -0
- data/test/html_translations/test.text.vi.plain.erb +8 -0
- data/test/html_translations/test.text.zh-CN.html.erb +5 -0
- data/test/html_translations/test.text.zh-CN.plain.erb +8 -0
- data/test/html_translations/test.text.zh-TW.html.erb +5 -0
- data/test/html_translations/test.text.zh-TW.plain.erb +8 -0
- data/test/html_translations/test.text.zh.html.erb +5 -0
- data/test/html_translations/test.text.zh.plain.erb +8 -0
- data/test/html_translations/test.th.html.erb +4 -0
- data/test/html_translations/test.tl.html.erb +4 -0
- data/test/html_translations/test.tr.html.erb +4 -0
- data/test/html_translations/test.uk.html.erb +4 -0
- data/test/html_translations/test.vi.html.erb +4 -0
- data/test/html_translations/test.zh-CN.html.erb +4 -0
- data/test/html_translations/test.zh-TW.html.erb +4 -0
- data/test/html_translations/test.zh.html.erb +4 -0
- data/test/test_yml_translator.rb +3 -2
- data/test/translations/ar.yml +7 -0
- data/test/translations/bg.yml +7 -0
- data/test/translations/ca.yml +7 -0
- data/test/translations/cs.yml +7 -0
- data/test/translations/da.yml +7 -0
- data/test/translations/de.yml +7 -0
- data/test/translations/el.yml +7 -0
- data/test/translations/en.yml +6 -0
- data/test/translations/es.yml +7 -0
- data/test/translations/et.yml +7 -0
- data/test/translations/fa.yml +7 -0
- data/test/translations/fi.yml +7 -0
- data/test/translations/fr.yml +7 -0
- data/test/translations/gl.yml +7 -0
- data/test/translations/hi.yml +7 -0
- data/test/translations/hr.yml +7 -0
- data/test/translations/hu.yml +7 -0
- data/test/translations/id.yml +7 -0
- data/test/translations/it.yml +7 -0
- data/test/translations/iw.yml +7 -0
- data/test/translations/ja.yml +7 -0
- data/test/translations/ko.yml +7 -0
- data/test/translations/lt.yml +7 -0
- data/test/translations/lv.yml +7 -0
- data/test/translations/mt.yml +7 -0
- data/test/translations/nl.yml +7 -0
- data/test/translations/no.yml +8 -0
- data/test/translations/pl.yml +7 -0
- data/test/translations/pt-PT.yml +7 -0
- data/test/translations/ro.yml +7 -0
- data/test/translations/ru.yml +7 -0
- data/test/translations/sk.yml +7 -0
- data/test/translations/sl.yml +7 -0
- data/test/translations/sq.yml +7 -0
- data/test/translations/sr.yml +7 -0
- data/test/translations/sv.yml +7 -0
- data/test/translations/th.yml +7 -0
- data/test/translations/tl.yml +7 -0
- data/test/translations/tr.yml +7 -0
- data/test/translations/uk.yml +7 -0
- data/test/translations/vi.yml +7 -0
- data/test/translations/zh-CN.yml +7 -0
- data/test/translations/zh-TW.yml +7 -0
- data/test/translations/zh.yml +7 -0
- metadata +222 -49
- data/History.txt +0 -3
- data/Manifest.txt +0 -19
- data/PostInstall.txt +0 -2
@@ -0,0 +1,335 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
|
6
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
+
<head>
|
8
|
+
<title>Module: Babelphish::HtmlTranslator</title>
|
9
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
+
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
+
<link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
+
<script type="text/javascript">
|
13
|
+
// <![CDATA[
|
14
|
+
|
15
|
+
function popupCode( url ) {
|
16
|
+
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
+
}
|
18
|
+
|
19
|
+
function toggleCode( id ) {
|
20
|
+
if ( document.getElementById )
|
21
|
+
elem = document.getElementById( id );
|
22
|
+
else if ( document.all )
|
23
|
+
elem = eval( "document.all." + id );
|
24
|
+
else
|
25
|
+
return false;
|
26
|
+
|
27
|
+
elemStyle = elem.style;
|
28
|
+
|
29
|
+
if ( elemStyle.display != "block" ) {
|
30
|
+
elemStyle.display = "block"
|
31
|
+
} else {
|
32
|
+
elemStyle.display = "none"
|
33
|
+
}
|
34
|
+
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Make codeblocks hidden by default
|
39
|
+
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
+
|
41
|
+
// ]]>
|
42
|
+
</script>
|
43
|
+
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<div id="classHeader">
|
50
|
+
<table class="header-table">
|
51
|
+
<tr class="top-aligned-row">
|
52
|
+
<td><strong>Module</strong></td>
|
53
|
+
<td class="class-name-in-header">Babelphish::HtmlTranslator</td>
|
54
|
+
</tr>
|
55
|
+
<tr class="top-aligned-row">
|
56
|
+
<td><strong>In:</strong></td>
|
57
|
+
<td>
|
58
|
+
<a href="../../files/lib/babelphish/html_translator_rb.html">
|
59
|
+
lib/babelphish/html_translator.rb
|
60
|
+
</a>
|
61
|
+
<br />
|
62
|
+
</td>
|
63
|
+
</tr>
|
64
|
+
|
65
|
+
</table>
|
66
|
+
</div>
|
67
|
+
<!-- banner header -->
|
68
|
+
|
69
|
+
<div id="bodyContent">
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
<div id="contextContent">
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
</div>
|
78
|
+
|
79
|
+
<div id="method-list">
|
80
|
+
<h3 class="section-bar">Methods</h3>
|
81
|
+
|
82
|
+
<div class="name-list">
|
83
|
+
<a href="#M000009">get_translated_file</a>
|
84
|
+
<a href="#M000006">translate</a>
|
85
|
+
<a href="#M000008">translate_and_write_page</a>
|
86
|
+
<a href="#M000007">translate_and_write_pages</a>
|
87
|
+
<a href="#M000010">translate_file?</a>
|
88
|
+
</div>
|
89
|
+
</div>
|
90
|
+
|
91
|
+
</div>
|
92
|
+
|
93
|
+
|
94
|
+
<!-- if includes -->
|
95
|
+
|
96
|
+
<div id="section">
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
<!-- if method_list -->
|
106
|
+
<div id="methods">
|
107
|
+
<h3 class="section-bar">Public Class methods</h3>
|
108
|
+
|
109
|
+
<div id="method-M000009" class="method-detail">
|
110
|
+
<a name="M000009"></a>
|
111
|
+
|
112
|
+
<div class="method-heading">
|
113
|
+
<a href="#M000009" class="method-signature">
|
114
|
+
<span class="method-name">get_translated_file</span><span class="method-args">(page, to)</span>
|
115
|
+
</a>
|
116
|
+
</div>
|
117
|
+
|
118
|
+
<div class="method-description">
|
119
|
+
<p>
|
120
|
+
Generate a file name for the newly translated content
|
121
|
+
</p>
|
122
|
+
<p><a class="source-toggle" href="#"
|
123
|
+
onclick="toggleCode('M000009-source');return false;">[Source]</a></p>
|
124
|
+
<div class="method-source-code" id="M000009-source">
|
125
|
+
<pre>
|
126
|
+
<span class="ruby-comment cmt"># File lib/babelphish/html_translator.rb, line 95</span>
|
127
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get_translated_file</span>(<span class="ruby-identifier">page</span>, <span class="ruby-identifier">to</span>)
|
128
|
+
<span class="ruby-identifier">new_page</span> = <span class="ruby-identifier">page</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">'.html'</span>, <span class="ruby-node">".#{to}.html"</span>)
|
129
|
+
<span class="ruby-identifier">new_page</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-value str">'text.html'</span>, <span class="ruby-node">"text.#{to}.html"</span>)
|
130
|
+
<span class="ruby-identifier">new_page</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-value str">'text.plain'</span>, <span class="ruby-node">"text.#{to}.plain"</span>)
|
131
|
+
<span class="ruby-identifier">new_page</span>
|
132
|
+
<span class="ruby-keyword kw">end</span>
|
133
|
+
</pre>
|
134
|
+
</div>
|
135
|
+
</div>
|
136
|
+
</div>
|
137
|
+
|
138
|
+
<div id="method-M000006" class="method-detail">
|
139
|
+
<a name="M000006"></a>
|
140
|
+
|
141
|
+
<div class="method-heading">
|
142
|
+
<a href="#M000006" class="method-signature">
|
143
|
+
<span class="method-name">translate</span><span class="method-args">(path, translate_tos, translate_from = 'en', overwrite = false)</span>
|
144
|
+
</a>
|
145
|
+
</div>
|
146
|
+
|
147
|
+
<div class="method-description">
|
148
|
+
<p>
|
149
|
+
Translates all files in the given path from the language specififed by
|
150
|
+
‘translate_from’ into the languages in
|
151
|
+
‘translate_tos’. Translations that already exist will not be
|
152
|
+
overwritten unless overwrite = true
|
153
|
+
</p>
|
154
|
+
<p><a class="source-toggle" href="#"
|
155
|
+
onclick="toggleCode('M000006-source');return false;">[Source]</a></p>
|
156
|
+
<div class="method-source-code" id="M000006-source">
|
157
|
+
<pre>
|
158
|
+
<span class="ruby-comment cmt"># File lib/babelphish/html_translator.rb, line 9</span>
|
159
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-identifier">translate</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">translate_tos</span>, <span class="ruby-identifier">translate_from</span> = <span class="ruby-value str">'en'</span>, <span class="ruby-identifier">overwrite</span> = <span class="ruby-keyword kw">false</span>)
|
160
|
+
<span class="ruby-ivar">@path</span> = <span class="ruby-identifier">path</span>
|
161
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-constant">Babelphish</span><span class="ruby-operator">::</span><span class="ruby-constant">GoogleTranslate</span><span class="ruby-operator">::</span><span class="ruby-constant">LANGUAGES</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">translate_from</span>)
|
162
|
+
<span class="ruby-constant">STDERR</span>.<span class="ruby-identifier">puts</span> <span class="ruby-node">"#{translate_from} is not one of the available languages."</span>
|
163
|
+
<span class="ruby-keyword kw">return</span>
|
164
|
+
<span class="ruby-keyword kw">end</span>
|
165
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">exists?</span>(<span class="ruby-identifier">path</span>)
|
166
|
+
<span class="ruby-constant">STDERR</span>.<span class="ruby-identifier">puts</span> <span class="ruby-node">"#{path} does not exist."</span>
|
167
|
+
<span class="ruby-keyword kw">return</span>
|
168
|
+
<span class="ruby-keyword kw">end</span>
|
169
|
+
<span class="ruby-identifier">translate_and_write_pages</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">translate_tos</span>, <span class="ruby-identifier">translate_from</span>, <span class="ruby-identifier">overwrite</span>)
|
170
|
+
<span class="ruby-keyword kw">end</span>
|
171
|
+
</pre>
|
172
|
+
</div>
|
173
|
+
</div>
|
174
|
+
</div>
|
175
|
+
|
176
|
+
<div id="method-M000008" class="method-detail">
|
177
|
+
<a name="M000008"></a>
|
178
|
+
|
179
|
+
<div class="method-heading">
|
180
|
+
<a href="#M000008" class="method-signature">
|
181
|
+
<span class="method-name">translate_and_write_page</span><span class="method-args">(source_page, tos, from, overwrite)</span>
|
182
|
+
</a>
|
183
|
+
</div>
|
184
|
+
|
185
|
+
<div class="method-description">
|
186
|
+
<p>
|
187
|
+
Translate a single page from the language specified in ‘from’
|
188
|
+
into the languages specified by ‘tos‘
|
189
|
+
</p>
|
190
|
+
<p><a class="source-toggle" href="#"
|
191
|
+
onclick="toggleCode('M000008-source');return false;">[Source]</a></p>
|
192
|
+
<div class="method-source-code" id="M000008-source">
|
193
|
+
<pre>
|
194
|
+
<span class="ruby-comment cmt"># File lib/babelphish/html_translator.rb, line 34</span>
|
195
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-identifier">translate_and_write_page</span>(<span class="ruby-identifier">source_page</span>, <span class="ruby-identifier">tos</span>, <span class="ruby-identifier">from</span>, <span class="ruby-identifier">overwrite</span>)
|
196
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span>(<span class="ruby-identifier">source_page</span>)
|
197
|
+
<span class="ruby-constant">STDERR</span>.<span class="ruby-identifier">puts</span> <span class="ruby-node">"Translating: #{source_page}"</span>
|
198
|
+
<span class="ruby-keyword kw">else</span>
|
199
|
+
<span class="ruby-constant">STDERR</span>.<span class="ruby-identifier">puts</span> <span class="ruby-node">"Could not find file: #{source_page}"</span>
|
200
|
+
<span class="ruby-keyword kw">return</span>
|
201
|
+
<span class="ruby-keyword kw">end</span>
|
202
|
+
|
203
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-identifier">translate_file?</span>(<span class="ruby-identifier">source_page</span>)
|
204
|
+
<span class="ruby-constant">STDERR</span>.<span class="ruby-identifier">puts</span> <span class="ruby-node">"Not translating file: #{source_page}"</span>
|
205
|
+
<span class="ruby-keyword kw">return</span>
|
206
|
+
<span class="ruby-keyword kw">end</span>
|
207
|
+
|
208
|
+
<span class="ruby-identifier">text</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">source_page</span>)
|
209
|
+
|
210
|
+
<span class="ruby-identifier">begins_with_html</span> = <span class="ruby-identifier">text</span>[<span class="ruby-value str">'<html>'</span>]
|
211
|
+
|
212
|
+
<span class="ruby-comment cmt"># Pull out all the code blocks so Google doesn't mess with those</span>
|
213
|
+
<span class="ruby-identifier">pattern</span> = <span class="ruby-regexp re">/\<\%.+\%\>/</span>
|
214
|
+
<span class="ruby-identifier">holder</span> = <span class="ruby-value str">'{{---}}'</span>
|
215
|
+
<span class="ruby-identifier">replacements</span> = <span class="ruby-identifier">text</span>.<span class="ruby-identifier">scan</span>(<span class="ruby-identifier">pattern</span>)
|
216
|
+
<span class="ruby-identifier">text</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-identifier">pattern</span>, <span class="ruby-identifier">holder</span>)
|
217
|
+
|
218
|
+
<span class="ruby-comment cmt"># Pull out all the new lines so Google doesn't mess with those</span>
|
219
|
+
<span class="ruby-identifier">pattern</span> = <span class="ruby-regexp re">/\n/</span>
|
220
|
+
<span class="ruby-identifier">newline_holder</span> = <span class="ruby-value str">'<brr />'</span>
|
221
|
+
<span class="ruby-identifier">newline_replacements</span> = <span class="ruby-identifier">text</span>.<span class="ruby-identifier">scan</span>(<span class="ruby-identifier">pattern</span>)
|
222
|
+
<span class="ruby-identifier">text</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-identifier">pattern</span>, <span class="ruby-identifier">newline_holder</span>)
|
223
|
+
|
224
|
+
<span class="ruby-comment cmt"># Send to Google for translations</span>
|
225
|
+
<span class="ruby-identifier">translations</span> = <span class="ruby-constant">Babelphish</span><span class="ruby-operator">::</span><span class="ruby-constant">Translator</span>.<span class="ruby-identifier">multiple_translate</span>(<span class="ruby-identifier">text</span>, <span class="ruby-identifier">tos</span>, <span class="ruby-identifier">from</span>)
|
226
|
+
|
227
|
+
<span class="ruby-comment cmt"># Put the code back</span>
|
228
|
+
<span class="ruby-identifier">translations</span>.<span class="ruby-identifier">each_key</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">locale</span><span class="ruby-operator">|</span>
|
229
|
+
<span class="ruby-identifier">replacements</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span>
|
230
|
+
<span class="ruby-identifier">translations</span>[<span class="ruby-identifier">locale</span>].<span class="ruby-identifier">sub!</span>(<span class="ruby-identifier">holder</span>, <span class="ruby-identifier">r</span>)
|
231
|
+
<span class="ruby-keyword kw">end</span>
|
232
|
+
<span class="ruby-keyword kw">end</span>
|
233
|
+
|
234
|
+
<span class="ruby-comment cmt"># Put the newlines back in</span>
|
235
|
+
<span class="ruby-identifier">translations</span>.<span class="ruby-identifier">each_key</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">locale</span><span class="ruby-operator">|</span>
|
236
|
+
<span class="ruby-identifier">newline_replacements</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span>
|
237
|
+
<span class="ruby-identifier">translations</span>[<span class="ruby-identifier">locale</span>].<span class="ruby-identifier">sub!</span>(<span class="ruby-identifier">newline_holder</span>, <span class="ruby-identifier">r</span>)
|
238
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-identifier">translations</span>[<span class="ruby-identifier">locale</span>][<span class="ruby-value str">'<html>'</span>]
|
239
|
+
<span class="ruby-comment cmt"># Google translate can insert '<html>' at the beginning of the result. Remove it.</span>
|
240
|
+
<span class="ruby-identifier">translations</span>[<span class="ruby-identifier">locale</span>][<span class="ruby-value str">'<html>'</span>]= <span class="ruby-value str">''</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">begins_with_html</span>
|
241
|
+
<span class="ruby-keyword kw">end</span>
|
242
|
+
<span class="ruby-keyword kw">end</span>
|
243
|
+
<span class="ruby-keyword kw">end</span>
|
244
|
+
|
245
|
+
<span class="ruby-comment cmt"># Write the new file</span>
|
246
|
+
<span class="ruby-identifier">translations</span>.<span class="ruby-identifier">each_key</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">locale</span><span class="ruby-operator">|</span>
|
247
|
+
<span class="ruby-identifier">translated_filename</span> = <span class="ruby-identifier">get_translated_file</span>(<span class="ruby-identifier">source_page</span>, <span class="ruby-identifier">locale</span>)
|
248
|
+
<span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">locale</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">from</span>) <span class="ruby-operator">&&</span> (<span class="ruby-identifier">overwrite</span> <span class="ruby-operator">||</span> <span class="ruby-operator">!</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">exists?</span>(<span class="ruby-identifier">translated_filename</span>))
|
249
|
+
<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">translated_filename</span>, <span class="ruby-value str">'w'</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">translations</span>[<span class="ruby-identifier">locale</span>]) }
|
250
|
+
<span class="ruby-keyword kw">end</span>
|
251
|
+
<span class="ruby-keyword kw">end</span>
|
252
|
+
|
253
|
+
<span class="ruby-keyword kw">end</span>
|
254
|
+
</pre>
|
255
|
+
</div>
|
256
|
+
</div>
|
257
|
+
</div>
|
258
|
+
|
259
|
+
<div id="method-M000007" class="method-detail">
|
260
|
+
<a name="M000007"></a>
|
261
|
+
|
262
|
+
<div class="method-heading">
|
263
|
+
<a href="#M000007" class="method-signature">
|
264
|
+
<span class="method-name">translate_and_write_pages</span><span class="method-args">(path, tos, from, overwrite)</span>
|
265
|
+
</a>
|
266
|
+
</div>
|
267
|
+
|
268
|
+
<div class="method-description">
|
269
|
+
<p><a class="source-toggle" href="#"
|
270
|
+
onclick="toggleCode('M000007-source');return false;">[Source]</a></p>
|
271
|
+
<div class="method-source-code" id="M000007-source">
|
272
|
+
<pre>
|
273
|
+
<span class="ruby-comment cmt"># File lib/babelphish/html_translator.rb, line 22</span>
|
274
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-identifier">translate_and_write_pages</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">tos</span>, <span class="ruby-identifier">from</span>, <span class="ruby-identifier">overwrite</span>)
|
275
|
+
<span class="ruby-constant">Dir</span>.<span class="ruby-identifier">glob</span>(<span class="ruby-node">"#{path}/*"</span>).<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">next_file</span><span class="ruby-operator">|</span>
|
276
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">directory?</span>(<span class="ruby-identifier">next_file</span>)
|
277
|
+
<span class="ruby-identifier">translate_and_write_pages</span>(<span class="ruby-identifier">next_file</span>, <span class="ruby-identifier">language</span>)
|
278
|
+
<span class="ruby-keyword kw">else</span>
|
279
|
+
<span class="ruby-identifier">translate_and_write_page</span>(<span class="ruby-identifier">next_file</span>, <span class="ruby-identifier">tos</span>, <span class="ruby-identifier">from</span>, <span class="ruby-identifier">overwrite</span>)
|
280
|
+
<span class="ruby-keyword kw">end</span>
|
281
|
+
<span class="ruby-keyword kw">end</span>
|
282
|
+
<span class="ruby-keyword kw">end</span>
|
283
|
+
</pre>
|
284
|
+
</div>
|
285
|
+
</div>
|
286
|
+
</div>
|
287
|
+
|
288
|
+
<div id="method-M000010" class="method-detail">
|
289
|
+
<a name="M000010"></a>
|
290
|
+
|
291
|
+
<div class="method-heading">
|
292
|
+
<a href="#M000010" class="method-signature">
|
293
|
+
<span class="method-name">translate_file?</span><span class="method-args">(page)</span>
|
294
|
+
</a>
|
295
|
+
</div>
|
296
|
+
|
297
|
+
<div class="method-description">
|
298
|
+
<p>
|
299
|
+
This is a hack but all the translated files live in the same directory as
|
300
|
+
the original file so we have to have some way of not translating the
|
301
|
+
translated files. this should return true for test.html.erb,
|
302
|
+
test.text.html.erb and test.text.plain.erb and false for test.es.html.erb,
|
303
|
+
test.text.es.html.erb and test.text.es.plain.erb
|
304
|
+
</p>
|
305
|
+
<p><a class="source-toggle" href="#"
|
306
|
+
onclick="toggleCode('M000010-source');return false;">[Source]</a></p>
|
307
|
+
<div class="method-source-code" id="M000010-source">
|
308
|
+
<pre>
|
309
|
+
<span class="ruby-comment cmt"># File lib/babelphish/html_translator.rb, line 109</span>
|
310
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-identifier">translate_file?</span>(<span class="ruby-identifier">page</span>)
|
311
|
+
<span class="ruby-identifier">test</span> = <span class="ruby-identifier">page</span>
|
312
|
+
<span class="ruby-identifier">test</span> = <span class="ruby-identifier">test</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">'./'</span>, <span class="ruby-value str">''</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">page</span>[<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">1</span>] <span class="ruby-operator">==</span> <span class="ruby-value str">'./'</span>
|
313
|
+
<span class="ruby-identifier">test</span> = <span class="ruby-identifier">test</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">'.text.html.erb'</span>, <span class="ruby-value str">''</span>)
|
314
|
+
<span class="ruby-identifier">test</span> = <span class="ruby-identifier">test</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">'.text.plain.erb'</span>, <span class="ruby-value str">''</span>)
|
315
|
+
<span class="ruby-identifier">test</span> = <span class="ruby-identifier">test</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">'.html.erb'</span>, <span class="ruby-value str">''</span>)
|
316
|
+
<span class="ruby-identifier">test</span>.<span class="ruby-identifier">split</span>(<span class="ruby-value str">'.'</span>).<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span>
|
317
|
+
<span class="ruby-keyword kw">end</span>
|
318
|
+
</pre>
|
319
|
+
</div>
|
320
|
+
</div>
|
321
|
+
</div>
|
322
|
+
|
323
|
+
|
324
|
+
</div>
|
325
|
+
|
326
|
+
|
327
|
+
</div>
|
328
|
+
|
329
|
+
|
330
|
+
<div id="validator-badges">
|
331
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
332
|
+
</div>
|
333
|
+
|
334
|
+
</body>
|
335
|
+
</html>
|
@@ -0,0 +1,311 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
|
6
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
+
<head>
|
8
|
+
<title>Module: Babelphish::Translator</title>
|
9
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
+
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
+
<link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
+
<script type="text/javascript">
|
13
|
+
// <![CDATA[
|
14
|
+
|
15
|
+
function popupCode( url ) {
|
16
|
+
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
+
}
|
18
|
+
|
19
|
+
function toggleCode( id ) {
|
20
|
+
if ( document.getElementById )
|
21
|
+
elem = document.getElementById( id );
|
22
|
+
else if ( document.all )
|
23
|
+
elem = eval( "document.all." + id );
|
24
|
+
else
|
25
|
+
return false;
|
26
|
+
|
27
|
+
elemStyle = elem.style;
|
28
|
+
|
29
|
+
if ( elemStyle.display != "block" ) {
|
30
|
+
elemStyle.display = "block"
|
31
|
+
} else {
|
32
|
+
elemStyle.display = "none"
|
33
|
+
}
|
34
|
+
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Make codeblocks hidden by default
|
39
|
+
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
+
|
41
|
+
// ]]>
|
42
|
+
</script>
|
43
|
+
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<div id="classHeader">
|
50
|
+
<table class="header-table">
|
51
|
+
<tr class="top-aligned-row">
|
52
|
+
<td><strong>Module</strong></td>
|
53
|
+
<td class="class-name-in-header">Babelphish::Translator</td>
|
54
|
+
</tr>
|
55
|
+
<tr class="top-aligned-row">
|
56
|
+
<td><strong>In:</strong></td>
|
57
|
+
<td>
|
58
|
+
<a href="../../files/lib/babelphish/translator_rb.html">
|
59
|
+
lib/babelphish/translator.rb
|
60
|
+
</a>
|
61
|
+
<br />
|
62
|
+
</td>
|
63
|
+
</tr>
|
64
|
+
|
65
|
+
</table>
|
66
|
+
</div>
|
67
|
+
<!-- banner header -->
|
68
|
+
|
69
|
+
<div id="bodyContent">
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
<div id="contextContent">
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
</div>
|
78
|
+
|
79
|
+
<div id="method-list">
|
80
|
+
<h3 class="section-bar">Methods</h3>
|
81
|
+
|
82
|
+
<div class="name-list">
|
83
|
+
<a href="#M000004">detect_language</a>
|
84
|
+
<a href="#M000003">multiple_translate</a>
|
85
|
+
<a href="#M000005">supported_languages</a>
|
86
|
+
<a href="#M000002">translate</a>
|
87
|
+
</div>
|
88
|
+
</div>
|
89
|
+
|
90
|
+
</div>
|
91
|
+
|
92
|
+
|
93
|
+
<!-- if includes -->
|
94
|
+
|
95
|
+
<div id="section">
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
<!-- if method_list -->
|
105
|
+
<div id="methods">
|
106
|
+
<h3 class="section-bar">Public Class methods</h3>
|
107
|
+
|
108
|
+
<div id="method-M000004" class="method-detail">
|
109
|
+
<a name="M000004"></a>
|
110
|
+
|
111
|
+
<div class="method-heading">
|
112
|
+
<a href="#M000004" class="method-signature">
|
113
|
+
<span class="method-name">detect_language</span><span class="method-args">(text)</span>
|
114
|
+
</a>
|
115
|
+
</div>
|
116
|
+
|
117
|
+
<div class="method-description">
|
118
|
+
<p>
|
119
|
+
Sends a string to google to attempt to detect the language. Returns an
|
120
|
+
array indicating success/fail and the resulting data from google in a hash:
|
121
|
+
{"language"=>"en",
|
122
|
+
"confidence"=>0.08594032, "isReliable"=>false}
|
123
|
+
</p>
|
124
|
+
<p><a class="source-toggle" href="#"
|
125
|
+
onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
|
126
|
+
<div class="method-source-code" id="M000004-source">
|
127
|
+
<pre>
|
128
|
+
<span class="ruby-comment cmt"># File lib/babelphish/translator.rb, line 90</span>
|
129
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-identifier">detect_language</span>(<span class="ruby-identifier">text</span>)
|
130
|
+
<span class="ruby-identifier">request</span> = <span class="ruby-constant">Babelphish</span><span class="ruby-operator">::</span><span class="ruby-constant">GOOGLE_AJAX_URL</span> <span class="ruby-operator">+</span> <span class="ruby-value str">"detect?v=1.0&q="</span> <span class="ruby-operator">+</span> <span class="ruby-constant">CGI</span>.<span class="ruby-identifier">escape</span>(<span class="ruby-identifier">text</span>)
|
131
|
+
<span class="ruby-comment cmt"># send get request</span>
|
132
|
+
<span class="ruby-identifier">response</span> = <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTP</span>.<span class="ruby-identifier">get_response</span>( <span class="ruby-constant">URI</span>.<span class="ruby-identifier">parse</span>( <span class="ruby-identifier">request</span> ) )
|
133
|
+
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>( <span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span> )
|
134
|
+
[<span class="ruby-identifier">json</span>[<span class="ruby-value str">'responseStatus'</span>] <span class="ruby-operator">==</span> <span class="ruby-value">200</span>, <span class="ruby-identifier">json</span>[<span class="ruby-value str">'responseData'</span>]]
|
135
|
+
<span class="ruby-keyword kw">end</span>
|
136
|
+
</pre>
|
137
|
+
</div>
|
138
|
+
</div>
|
139
|
+
</div>
|
140
|
+
|
141
|
+
<div id="method-M000003" class="method-detail">
|
142
|
+
<a name="M000003"></a>
|
143
|
+
|
144
|
+
<div class="method-heading">
|
145
|
+
<a href="#M000003" class="method-signature">
|
146
|
+
<span class="method-name">multiple_translate</span><span class="method-args">(text, tos, from = 'en', tries = 0)</span>
|
147
|
+
</a>
|
148
|
+
</div>
|
149
|
+
|
150
|
+
<div class="method-description">
|
151
|
+
<p>
|
152
|
+
<a href="Translator.html#M000002">translate</a> from the ‘from’
|
153
|
+
language into all available languages multiple strings and multiple
|
154
|
+
languages
|
155
|
+
</p>
|
156
|
+
<p>
|
157
|
+
<a
|
158
|
+
href="http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello%20world&langpair=en|it&q=goodbye&langpair=en|fr">ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello%20world&langpair=en|it&q=goodbye&langpair=en|fr</a>
|
159
|
+
results from google look like this: {"responseData":
|
160
|
+
[{"responseData":{"translatedText":"ciao
|
161
|
+
mondo"},"responseDetails":null,"responseStatus":200},{"responseData":{"translatedText":"au
|
162
|
+
revoir"},"responseDetails":null,"responseStatus":200}],
|
163
|
+
"responseDetails": null, "responseStatus": 200}
|
164
|
+
</p>
|
165
|
+
<p>
|
166
|
+
One string into multiple languages <a
|
167
|
+
href="http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello%20world&langpair=en|it&langpair=en|fr">ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello%20world&langpair=en|it&langpair=en|fr</a>
|
168
|
+
results from google look like this: {"responseData":
|
169
|
+
[{"responseData":{"translatedText":"ciao
|
170
|
+
mondo"},"responseDetails":null,"responseStatus":200},{"responseData":{"translatedText":"Bonjour
|
171
|
+
le
|
172
|
+
Monde"},"responseDetails":null,"responseStatus":200}],
|
173
|
+
"responseDetails": null, "responseStatus": 200}
|
174
|
+
</p>
|
175
|
+
<p><a class="source-toggle" href="#"
|
176
|
+
onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
|
177
|
+
<div class="method-source-code" id="M000003-source">
|
178
|
+
<pre>
|
179
|
+
<span class="ruby-comment cmt"># File lib/babelphish/translator.rb, line 46</span>
|
180
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-identifier">multiple_translate</span>(<span class="ruby-identifier">text</span>, <span class="ruby-identifier">tos</span>, <span class="ruby-identifier">from</span> = <span class="ruby-value str">'en'</span>, <span class="ruby-identifier">tries</span> = <span class="ruby-value">0</span>)
|
181
|
+
<span class="ruby-keyword kw">return</span> {} <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">text</span>.<span class="ruby-identifier">strip</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-comment cmt"># Google doesn't like it when you send them an empty string</span>
|
182
|
+
<span class="ruby-identifier">base</span> = <span class="ruby-constant">Babelphish</span><span class="ruby-operator">::</span><span class="ruby-constant">GOOGLE_AJAX_URL</span> <span class="ruby-operator">+</span> <span class="ruby-value str">'translate'</span>
|
183
|
+
<span class="ruby-comment cmt"># assemble query params</span>
|
184
|
+
<span class="ruby-identifier">params</span> = {
|
185
|
+
<span class="ruby-identifier">:q</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">text</span>,
|
186
|
+
<span class="ruby-identifier">:v</span> =<span class="ruby-operator">></span> <span class="ruby-value">1.0</span>
|
187
|
+
}
|
188
|
+
<span class="ruby-identifier">query</span> = <span class="ruby-identifier">params</span>.<span class="ruby-identifier">map</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>,<span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-node">"#{k}=#{CGI.escape(v.to_s)}"</span> }.<span class="ruby-identifier">join</span>(<span class="ruby-value str">'&'</span>)
|
189
|
+
|
190
|
+
<span class="ruby-identifier">tos</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">to</span><span class="ruby-operator">|</span>
|
191
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-constant">Babelphish</span><span class="ruby-operator">::</span><span class="ruby-constant">GoogleTranslate</span><span class="ruby-operator">::</span><span class="ruby-constant">LANGUAGES</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">to</span>)
|
192
|
+
<span class="ruby-identifier">raise</span> <span class="ruby-constant">Exceptions</span><span class="ruby-operator">::</span><span class="ruby-constant">GoogleResponseError</span>, <span class="ruby-node">"#{to} is not a valid Google Translate code. Please be sure language codes are one of: #{Babelphish::GoogleTranslate::LANGUAGES.join(',')}"</span>
|
193
|
+
<span class="ruby-keyword kw">end</span>
|
194
|
+
<span class="ruby-identifier">query</span> <span class="ruby-operator"><<</span> <span class="ruby-value str">"&langpair="</span> <span class="ruby-operator">+</span> <span class="ruby-constant">CGI</span>.<span class="ruby-identifier">escape</span>(<span class="ruby-node">"#{from}|#{to}"</span>)
|
195
|
+
<span class="ruby-keyword kw">end</span>
|
196
|
+
|
197
|
+
<span class="ruby-identifier">response</span> = <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTP</span>.<span class="ruby-identifier">get_response</span>( <span class="ruby-constant">URI</span>.<span class="ruby-identifier">parse</span>( <span class="ruby-node">"#{base}?#{query}"</span> ) )
|
198
|
+
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>( <span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span> )
|
199
|
+
|
200
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-identifier">json</span>[<span class="ruby-value str">'responseStatus'</span>] <span class="ruby-operator">==</span> <span class="ruby-value">200</span>
|
201
|
+
<span class="ruby-identifier">results</span> = {}
|
202
|
+
<span class="ruby-identifier">json</span>[<span class="ruby-value str">'responseData'</span>].<span class="ruby-identifier">each_with_index</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">data</span>, <span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
|
203
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-identifier">data</span>[<span class="ruby-value str">'responseStatus'</span>] <span class="ruby-operator">==</span> <span class="ruby-value">200</span>
|
204
|
+
<span class="ruby-identifier">results</span>[<span class="ruby-identifier">tos</span>[<span class="ruby-identifier">index</span>]] = <span class="ruby-identifier">data</span>[<span class="ruby-value str">'responseData'</span>][<span class="ruby-value str">'translatedText'</span>]
|
205
|
+
<span class="ruby-keyword kw">else</span>
|
206
|
+
<span class="ruby-comment cmt"># retry the single translation</span>
|
207
|
+
<span class="ruby-identifier">translate</span>(<span class="ruby-identifier">text</span>, <span class="ruby-identifier">tos</span>[<span class="ruby-identifier">index</span>], <span class="ruby-identifier">from</span>)
|
208
|
+
<span class="ruby-keyword kw">end</span>
|
209
|
+
<span class="ruby-keyword kw">end</span>
|
210
|
+
<span class="ruby-identifier">results</span>
|
211
|
+
<span class="ruby-keyword kw">else</span>
|
212
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-identifier">tries</span> <span class="ruby-operator"><=</span> <span class="ruby-constant">Babelphish</span><span class="ruby-operator">::</span><span class="ruby-constant">MAX_RETRIES</span>
|
213
|
+
<span class="ruby-comment cmt"># Try again a few more times</span>
|
214
|
+
<span class="ruby-identifier">multiple_translate</span>(<span class="ruby-identifier">text</span>, <span class="ruby-identifier">tos</span>, <span class="ruby-identifier">from</span>, <span class="ruby-identifier">tries</span><span class="ruby-operator">+=</span><span class="ruby-value">1</span>)
|
215
|
+
<span class="ruby-keyword kw">else</span>
|
216
|
+
<span class="ruby-identifier">raise</span> <span class="ruby-constant">Exceptions</span><span class="ruby-operator">::</span><span class="ruby-constant">GoogleResponseError</span>, <span class="ruby-node">"A problem occured while translating. #{response} -- #{response.body} -- From: #{from} -- Text: #{text}"</span>
|
217
|
+
<span class="ruby-keyword kw">end</span>
|
218
|
+
<span class="ruby-keyword kw">end</span>
|
219
|
+
<span class="ruby-keyword kw">end</span>
|
220
|
+
</pre>
|
221
|
+
</div>
|
222
|
+
</div>
|
223
|
+
</div>
|
224
|
+
|
225
|
+
<div id="method-M000005" class="method-detail">
|
226
|
+
<a name="M000005"></a>
|
227
|
+
|
228
|
+
<div class="method-heading">
|
229
|
+
<a href="#M000005" class="method-signature">
|
230
|
+
<span class="method-name">supported_languages</span><span class="method-args">()</span>
|
231
|
+
</a>
|
232
|
+
</div>
|
233
|
+
|
234
|
+
<div class="method-description">
|
235
|
+
<p><a class="source-toggle" href="#"
|
236
|
+
onclick="toggleCode('M000005-source');return false;">[Source]</a></p>
|
237
|
+
<div class="method-source-code" id="M000005-source">
|
238
|
+
<pre>
|
239
|
+
<span class="ruby-comment cmt"># File lib/babelphish/translator.rb, line 98</span>
|
240
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-identifier">supported_languages</span>
|
241
|
+
<span class="ruby-constant">Babelphish</span><span class="ruby-operator">::</span><span class="ruby-constant">GoogleTranslate</span><span class="ruby-operator">::</span><span class="ruby-constant">LANGUAGES</span>
|
242
|
+
<span class="ruby-keyword kw">end</span>
|
243
|
+
</pre>
|
244
|
+
</div>
|
245
|
+
</div>
|
246
|
+
</div>
|
247
|
+
|
248
|
+
<div id="method-M000002" class="method-detail">
|
249
|
+
<a name="M000002"></a>
|
250
|
+
|
251
|
+
<div class="method-heading">
|
252
|
+
<a href="#M000002" class="method-signature">
|
253
|
+
<span class="method-name">translate</span><span class="method-args">(text, to, from = 'en', tries = 0)</span>
|
254
|
+
</a>
|
255
|
+
</div>
|
256
|
+
|
257
|
+
<div class="method-description">
|
258
|
+
<p>
|
259
|
+
from: <a
|
260
|
+
href="http://ruby.geraldbauer.ca/google-translation-api.html">ruby.geraldbauer.ca/google-translation-api.html</a>
|
261
|
+
<a href="Translator.html#M000002">translate</a> text from
|
262
|
+
‘from’ to ‘to‘
|
263
|
+
</p>
|
264
|
+
<p><a class="source-toggle" href="#"
|
265
|
+
onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
|
266
|
+
<div class="method-source-code" id="M000002-source">
|
267
|
+
<pre>
|
268
|
+
<span class="ruby-comment cmt"># File lib/babelphish/translator.rb, line 8</span>
|
269
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-identifier">translate</span>(<span class="ruby-identifier">text</span>, <span class="ruby-identifier">to</span>, <span class="ruby-identifier">from</span> = <span class="ruby-value str">'en'</span>, <span class="ruby-identifier">tries</span> = <span class="ruby-value">0</span>)
|
270
|
+
|
271
|
+
<span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">to</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">from</span>
|
272
|
+
<span class="ruby-identifier">base</span> = <span class="ruby-constant">Babelphish</span><span class="ruby-operator">::</span><span class="ruby-constant">GOOGLE_AJAX_URL</span> <span class="ruby-operator">+</span> <span class="ruby-value str">'translate'</span>
|
273
|
+
<span class="ruby-comment cmt"># assemble query params</span>
|
274
|
+
<span class="ruby-identifier">params</span> = {
|
275
|
+
<span class="ruby-identifier">:langpair</span> =<span class="ruby-operator">></span> <span class="ruby-node">"#{from}|#{to}"</span>,
|
276
|
+
<span class="ruby-identifier">:q</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">text</span>,
|
277
|
+
<span class="ruby-identifier">:v</span> =<span class="ruby-operator">></span> <span class="ruby-value">1.0</span>
|
278
|
+
}
|
279
|
+
<span class="ruby-identifier">query</span> = <span class="ruby-identifier">params</span>.<span class="ruby-identifier">map</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>,<span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-node">"#{k}=#{CGI.escape(v.to_s)}"</span> }.<span class="ruby-identifier">join</span>(<span class="ruby-value str">'&'</span>)
|
280
|
+
<span class="ruby-comment cmt"># send get request</span>
|
281
|
+
<span class="ruby-identifier">response</span> = <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTP</span>.<span class="ruby-identifier">get_response</span>( <span class="ruby-constant">URI</span>.<span class="ruby-identifier">parse</span>( <span class="ruby-node">"#{base}?#{query}"</span> ) )
|
282
|
+
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>( <span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span> )
|
283
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-identifier">json</span>[<span class="ruby-value str">'responseStatus'</span>] <span class="ruby-operator">==</span> <span class="ruby-value">200</span>
|
284
|
+
<span class="ruby-identifier">json</span>[<span class="ruby-value str">'responseData'</span>][<span class="ruby-value str">'translatedText'</span>]
|
285
|
+
<span class="ruby-keyword kw">else</span>
|
286
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-identifier">tries</span> <span class="ruby-operator"><=</span> <span class="ruby-constant">Babelphish</span><span class="ruby-operator">::</span><span class="ruby-constant">MAX_RETRIES</span>
|
287
|
+
<span class="ruby-comment cmt"># Try again a few more times</span>
|
288
|
+
<span class="ruby-identifier">translate</span>(<span class="ruby-identifier">text</span>, <span class="ruby-identifier">to</span>, <span class="ruby-identifier">from</span>, <span class="ruby-identifier">tries</span><span class="ruby-operator">+=</span><span class="ruby-value">1</span>)
|
289
|
+
<span class="ruby-keyword kw">else</span>
|
290
|
+
<span class="ruby-identifier">raise</span> <span class="ruby-constant">Exceptions</span><span class="ruby-operator">::</span><span class="ruby-constant">GoogleResponseError</span>, <span class="ruby-node">"A problem occured while translating from #{from} to #{to}. #{json['responseDetails']}"</span>
|
291
|
+
<span class="ruby-keyword kw">end</span>
|
292
|
+
<span class="ruby-keyword kw">end</span>
|
293
|
+
<span class="ruby-keyword kw">end</span>
|
294
|
+
</pre>
|
295
|
+
</div>
|
296
|
+
</div>
|
297
|
+
</div>
|
298
|
+
|
299
|
+
|
300
|
+
</div>
|
301
|
+
|
302
|
+
|
303
|
+
</div>
|
304
|
+
|
305
|
+
|
306
|
+
<div id="validator-badges">
|
307
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
308
|
+
</div>
|
309
|
+
|
310
|
+
</body>
|
311
|
+
</html>
|