babelphish 0.1.5 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +1 -0
- data/README.rdoc +2 -0
- data/Rakefile +17 -1
- data/lib/babelphish.rb +35 -2
- data/lib/babelphish/exceptions.rb +5 -0
- data/lib/babelphish/languages.rb +104 -45
- data/lib/babelphish/translator.rb +150 -26
- data/test/test_babelphish.rb +57 -3
- data/test/test_helper.rb +5 -0
- metadata +4 -3
data/Manifest.txt
CHANGED
data/README.rdoc
CHANGED
@@ -16,6 +16,8 @@ Translate the en.yml file into all supported languages and overwrite other yml f
|
|
16
16
|
|
17
17
|
== INSTALL:
|
18
18
|
|
19
|
+
ya2yaml should be automatically install, but in case they aren't:
|
20
|
+
|
19
21
|
ya2yaml is required to properly write utf8 yaml files. Install it first.
|
20
22
|
|
21
23
|
* sudo gem install ya2yaml
|
data/Rakefile
CHANGED
@@ -10,7 +10,7 @@ $hoe = Hoe.new('babelphish', Babelphish::VERSION) do |p|
|
|
10
10
|
p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
11
11
|
p.rubyforge_name = 'babelphish'
|
12
12
|
p.url = "http://github.com/jbasdf/babelphish"
|
13
|
-
p.summary = "Translate with Google like a fule"
|
13
|
+
p.summary = "Translate with Google like a fule => 'fool'"
|
14
14
|
p.description = "Babelphish helps you make a quick translation of your application using Google Translate."
|
15
15
|
|
16
16
|
p.extra_deps = [
|
@@ -31,3 +31,19 @@ Dir['tasks/**/*.rake'].each { |t| load t }
|
|
31
31
|
|
32
32
|
# TODO - want other tests/tasks run by default? Add them to the list
|
33
33
|
# task :default => [:spec, :features]
|
34
|
+
|
35
|
+
# TODO get rcov working
|
36
|
+
begin
|
37
|
+
require 'rcov/rcovtask'
|
38
|
+
Rcov::RcovTask.new do |t|
|
39
|
+
t.libs << 'lib'
|
40
|
+
t.pattern = 'test/*_test.rb'
|
41
|
+
t.verbose = true
|
42
|
+
# t.output_dir = 'coverage'
|
43
|
+
# t.rcov_opts << '--exclude "gems/*"'
|
44
|
+
end
|
45
|
+
rescue LoadError
|
46
|
+
task :rcov do
|
47
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
48
|
+
end
|
49
|
+
end
|
data/lib/babelphish.rb
CHANGED
@@ -1,9 +1,42 @@
|
|
1
1
|
$:.unshift(File.dirname(__FILE__)) unless
|
2
2
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
require 'yaml'
|
5
|
+
require 'cgi'
|
6
|
+
require 'json'
|
7
|
+
require 'net/http'
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'jcode'
|
11
|
+
rescue LoadError
|
12
|
+
begin
|
13
|
+
gem 'jcode'
|
14
|
+
rescue Gem::LoadError
|
15
|
+
puts "Please install the jcode gem"
|
16
|
+
end
|
17
|
+
end
|
6
18
|
|
19
|
+
begin
|
20
|
+
require 'ya2yaml'
|
21
|
+
rescue LoadError
|
22
|
+
begin
|
23
|
+
gem 'ya2yaml'
|
24
|
+
rescue Gem::LoadError
|
25
|
+
puts "Please install the ya2yaml gem"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
require File.dirname(__FILE__) + '/../lib/babelphish/translator'
|
30
|
+
require File.dirname(__FILE__) + '/../lib/babelphish/languages'
|
31
|
+
require File.dirname(__FILE__) + '/../lib/babelphish/exceptions'
|
32
|
+
|
33
|
+
$KCODE = 'UTF8'
|
34
|
+
|
35
|
+
module Babelphish
|
36
|
+
VERSION = '0.1.7'
|
37
|
+
GOOGLE_AJAX_URL = "http://ajax.googleapis.com/ajax/services/language/"
|
38
|
+
MAX_RETRIES = 3
|
39
|
+
|
7
40
|
def self.load_tasks
|
8
41
|
if File.exists?('Rakefile')
|
9
42
|
load 'Rakefile'
|
data/lib/babelphish/languages.rb
CHANGED
@@ -1,51 +1,110 @@
|
|
1
|
+
# a list of supported languages is available here:
|
2
|
+
# http://code.google.com/apis/ajaxlanguage/documentation/reference.html#_intro_fonje
|
1
3
|
module Babelphish
|
2
4
|
module GoogleTranslate
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
6
|
+
AFRIKAANS = 'af'
|
7
|
+
ALBANIAN = 'sq'
|
8
|
+
AMHARIC = 'am'
|
9
|
+
ARABIC = 'ar'
|
10
|
+
ARMENIAN = 'hy'
|
11
|
+
AZERBAIJANI = 'az'
|
12
|
+
BASQUE = 'eu'
|
13
|
+
BELARUSIAN = 'be'
|
14
|
+
BENGALI = 'bn'
|
15
|
+
BIHARI = 'bh'
|
16
|
+
BULGARIAN = 'bg'
|
17
|
+
BURMESE = 'my'
|
18
|
+
CATALAN = 'ca'
|
19
|
+
CHEROKEE = 'chr'
|
20
|
+
CHINESE = 'zh'
|
21
|
+
CHINESE_SIMPLIFIED = 'zh-CN'
|
22
|
+
CHINESE_TRADITIONAL = 'zh-TW'
|
23
|
+
CROATIAN = 'hr'
|
24
|
+
CZECH = 'cs'
|
25
|
+
DANISH = 'da'
|
26
|
+
DHIVEHI = 'dv'
|
27
|
+
DUTCH = 'nl'
|
28
|
+
ENGLISH = 'en'
|
29
|
+
ESPERANTO = 'eo'
|
30
|
+
ESTONIAN = 'et'
|
31
|
+
FILIPINO = 'tl'
|
32
|
+
FINNISH = 'fi'
|
33
|
+
FRENCH = 'fr'
|
34
|
+
GALICIAN = 'gl'
|
35
|
+
GEORGIAN = 'ka'
|
36
|
+
GERMAN = 'de'
|
37
|
+
GREEK = 'el'
|
38
|
+
GUARANI = 'gn'
|
39
|
+
GUJARATI = 'gu'
|
40
|
+
HEBREW = 'iw'
|
41
|
+
HINDI = 'hi'
|
42
|
+
HUNGARIAN = 'hu'
|
43
|
+
ICELANDIC = 'is'
|
44
|
+
INDONESIAN = 'id'
|
45
|
+
INUKTITUT = 'iu'
|
46
|
+
ITALIAN = 'it'
|
47
|
+
JAPANESE = 'ja'
|
48
|
+
KANNADA = 'kn'
|
49
|
+
KAZAKH = 'kk'
|
50
|
+
KHMER = 'km'
|
51
|
+
KOREAN = 'ko'
|
52
|
+
KURDISH = 'ku'
|
53
|
+
KYRGYZ = 'ky'
|
54
|
+
LAOTHIAN = 'lo'
|
55
|
+
LATVIAN = 'lv'
|
56
|
+
LITHUANIAN = 'lt'
|
57
|
+
MACEDONIAN = 'mk'
|
58
|
+
MALAY = 'ms'
|
59
|
+
MALAYALAM = 'ml'
|
60
|
+
MALTESE = 'mt'
|
61
|
+
MARATHI = 'mr'
|
62
|
+
MONGOLIAN = 'mn'
|
63
|
+
NEPALI = 'ne'
|
64
|
+
NORWEGIAN = 'no'
|
65
|
+
ORIYA = 'or'
|
66
|
+
PASHTO = 'ps'
|
67
|
+
PERSIAN = 'fa'
|
68
|
+
POLISH = 'pl'
|
69
|
+
PORTUGUESE = 'pt-PT'
|
70
|
+
PUNJABI = 'pa'
|
71
|
+
ROMANIAN = 'ro'
|
72
|
+
RUSSIAN = 'ru'
|
73
|
+
SANSKRIT = 'sa'
|
74
|
+
SERBIAN = 'sr'
|
75
|
+
SINDHI = 'sd'
|
76
|
+
SINHALESE = 'si'
|
77
|
+
SLOVAK = 'sk'
|
78
|
+
SLOVENIAN = 'sl'
|
79
|
+
SPANISH = 'es'
|
80
|
+
SWAHILI = 'sw'
|
81
|
+
SWEDISH = 'sv'
|
82
|
+
TAJIK = 'tg'
|
83
|
+
TAMIL = 'ta'
|
84
|
+
TAGALOG = 'tl'
|
85
|
+
TELUGU = 'te'
|
86
|
+
THAI = 'th'
|
87
|
+
TIBETAN = 'bo'
|
88
|
+
TURKISH = 'tr'
|
89
|
+
UKRAINIAN = 'uk'
|
90
|
+
URDU = 'ur'
|
91
|
+
UZBEK = 'uz'
|
92
|
+
UIGHUR = 'ug'
|
93
|
+
VIETNAMESE = 'vi'
|
40
94
|
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
95
|
+
# all languages
|
96
|
+
# LANGUAGES = [AFRIKAANS,ALBANIAN,AMHARIC,ARABIC,ARMENIAN,AZERBAIJANI,BASQUE,BELARUSIAN,BENGALI,BIHARI,BULGARIAN,BURMESE,CATALAN,CHEROKEE,
|
97
|
+
# CHINESE,CHINESE_SIMPLIFIED,CHINESE_TRADITIONAL,CROATIAN,CZECH,DANISH,DHIVEHI,DUTCH,ENGLISH,ESPERANTO,ESTONIAN,FILIPINO,FINNISH,
|
98
|
+
# FRENCH,GALICIAN,GEORGIAN,GERMAN,GREEK,GUARANI,GUJARATI,HEBREW,HINDI,HUNGARIAN,ICELANDIC,INDONESIAN,INUKTITUT,ITALIAN,
|
99
|
+
# JAPANESE,KANNADA,KAZAKH,KHMER,KOREAN,KURDISH,KYRGYZ,LAOTHIAN,LATVIAN,LITHUANIAN,MACEDONIAN,MALAY,MALAYALAM,MALTESE,MARATHI,
|
100
|
+
# MONGOLIAN,NEPALI,NORWEGIAN,ORIYA,PASHTO,PERSIAN,POLISH,PORTUGUESE,PUNJABI,ROMANIAN,RUSSIAN,SANSKRIT,SERBIAN,SINDHI,SINHALESE,
|
101
|
+
# SLOVAK,SLOVENIAN,SPANISH,SWAHILI,SWEDISH,TAJIK,TAMIL,TAGALOG,TELUGU,THAI,TIBETAN,TURKISH,UKRAINIAN,URDU,UZBEK,UIGHUR,VIETNAMESE]
|
102
|
+
|
103
|
+
# Google doesn't support translations to all languages. These are the translations available. See:
|
104
|
+
# http://code.google.com/apis/ajaxlanguage/documentation/#SupportedPairs
|
105
|
+
LANGUAGES = [ALBANIAN, ARABIC, BULGARIAN, CATALAN, CHINESE, CHINESE_SIMPLIFIED,CHINESE_TRADITIONAL, CROATIAN,
|
106
|
+
CZECH, DANISH, DUTCH, ENGLISH, ESTONIAN, FILIPINO, FINNISH, FRENCH, GALICIAN, GERMAN, GREEK, HEBREW, HINDI, HUNGARIAN,
|
107
|
+
INDONESIAN, ITALIAN, JAPANESE, KOREAN, LATVIAN, LITHUANIAN, MALTESE, NORWEGIAN, PERSIAN, POLISH, PORTUGUESE,
|
108
|
+
ROMANIAN, RUSSIAN, SERBIAN, SLOVAK, SLOVENIAN, SPANISH, SWEDISH, THAI, TURKISH, UKRAINIAN, VIETNAMESE]
|
50
109
|
end
|
51
110
|
end
|
@@ -1,15 +1,10 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
require 'ya2yaml'
|
3
|
-
require 'babelphish/languages'
|
4
|
-
require 'jcode'
|
5
|
-
|
6
1
|
module Babelphish
|
7
2
|
module Translator
|
3
|
+
|
8
4
|
class << self
|
9
5
|
|
10
6
|
def translate_yaml(yml, overwrite = false, translate_to = nil)
|
11
7
|
@yml = yml
|
12
|
-
$KCODE = 'UTF8'
|
13
8
|
language = File.basename(yml, ".yml")
|
14
9
|
if !Babelphish::GoogleTranslate::LANGUAGES.include?(language)
|
15
10
|
STDERR.puts "#{language} is not one of the available languages. Please choose a standard localized yml file. i.e. en.yml."
|
@@ -20,25 +15,86 @@ module Babelphish
|
|
20
15
|
translate_and_write_yml(yml, translate_to, language, overwrite)
|
21
16
|
puts "Finished translating #{language} to #{translate_to}"
|
22
17
|
else
|
23
|
-
Babelphish::GoogleTranslate::LANGUAGES.each do |to|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
18
|
+
# Babelphish::GoogleTranslate::LANGUAGES.each do |to|
|
19
|
+
# puts "Translating #{language} to #{to}"
|
20
|
+
# translate_and_write_yml(yml, to, language, overwrite)
|
21
|
+
# puts "Finished translating #{language} to #{to}"
|
22
|
+
# end
|
23
|
+
tos = Babelphish::GoogleTranslate::LANGUAGES
|
24
|
+
translate_and_write_many_yml(yml, tos, language, overwrite)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def translate_and_write_many_yml(yml, tos, from, overwrite)
|
29
|
+
return unless File.exist?(yml)
|
30
|
+
source = YAML.load_file(yml)
|
31
|
+
translated_source = YAML.load_file(yml)
|
32
|
+
translate_many_keys(translated_source, tos, from)
|
33
|
+
# At this point translated_source contains a translation for every language. Cut it apart into individual hashes
|
34
|
+
tos.each do |to|
|
35
|
+
next if to == from # don't want to overwrite the source file
|
36
|
+
extracted_translation = {}
|
37
|
+
extract_translation(source, translated_source, extracted_translation, to)
|
38
|
+
# change the top level key from the source language to the destination language
|
39
|
+
translated_filename = File.join(File.dirname(yml), "#{to}.yml")
|
40
|
+
return if File.exist?(translated_filename) && !overwrite
|
41
|
+
extracted_translation[to] = extracted_translation[from]
|
42
|
+
extracted_translation.delete(from)
|
43
|
+
File.open(translated_filename, 'w') { |f| f.write(extracted_translation.ya2yaml) }
|
28
44
|
end
|
29
45
|
end
|
30
46
|
|
47
|
+
def extract_translation(source, translated_source, extracted, language)
|
48
|
+
source.each_key do |key|
|
49
|
+
if source[key].is_a?(Hash)
|
50
|
+
extracted[key] = {}
|
51
|
+
extract_translation(source[key], translated_source[key], extracted[key], language)
|
52
|
+
else
|
53
|
+
extracted[key] = translated_source[key][language]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def translate_many_keys(translate_hash, tos, from)
|
59
|
+
translate_hash.each_key do |key|
|
60
|
+
if translate_hash[key].is_a?(Hash)
|
61
|
+
translate_many_keys(translate_hash[key], tos, from)
|
62
|
+
else
|
63
|
+
if key == false
|
64
|
+
puts "Key #{key} was evaluated as false. Check your yml file and be sure it does not include values like no: No"
|
65
|
+
elsif key == true
|
66
|
+
puts "Key #{key} was evaluated as true. Check your yml file and be sure it does not include values like yes: Yes"
|
67
|
+
elsif !translate_hash[key].nil?
|
68
|
+
# pull out all the string substitutions so that google doesn't translate those
|
69
|
+
pattern = /\{\{.+\}\}/
|
70
|
+
holder = '{{---}}'
|
71
|
+
replacements = translate_hash[key].scan(pattern)
|
72
|
+
translate_hash[key].gsub!(pattern, holder)
|
73
|
+
translations = multiple_translate(translate_hash[key], tos, from)
|
74
|
+
translations.each_key do |locale|
|
75
|
+
replacements.each do |r|
|
76
|
+
translations[locale].sub!(holder, r)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
translate_hash[key] = translations
|
80
|
+
else
|
81
|
+
puts "Key #{key} contains no data"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
31
87
|
def translate_and_write_yml(yml, to, from, overwrite)
|
32
88
|
return if to == from
|
33
89
|
return unless File.exist?(yml)
|
34
90
|
translated_filename = File.join(File.dirname(yml), "#{to}.yml")
|
35
91
|
return if File.exist?(translated_filename) && !overwrite
|
36
|
-
|
37
|
-
translate_keys(
|
92
|
+
source = YAML.load_file(yml)
|
93
|
+
translate_keys(source, to, from)
|
38
94
|
# change the top level key from the source language to the destination language
|
39
|
-
|
40
|
-
|
41
|
-
File.open(translated_filename, 'w') { |f| f.write(
|
95
|
+
source[to] = source[from]
|
96
|
+
source.delete(from)
|
97
|
+
File.open(translated_filename, 'w') { |f| f.write(source.ya2yaml) }
|
42
98
|
end
|
43
99
|
|
44
100
|
def translate_keys(translate_hash, to, from)
|
@@ -50,7 +106,7 @@ module Babelphish
|
|
50
106
|
puts "Key #{key} was evaluated as false. Check your yml file and be sure to escape values like no with 'no'. ie 'no': 'No'"
|
51
107
|
elsif key == true
|
52
108
|
puts "Key #{key} was evaluated as true. Check your yml file and be sure to escape values like yes with 'yes'. ie 'yes': 'Yes'"
|
53
|
-
elsif translate_hash[key]
|
109
|
+
elsif !translate_hash[key].nil?
|
54
110
|
# pull out all the string substitutions so that google doesn't translate those
|
55
111
|
pattern = /\{\{.+\}\}/
|
56
112
|
holder = '{{---}}'
|
@@ -66,17 +122,12 @@ module Babelphish
|
|
66
122
|
end
|
67
123
|
end
|
68
124
|
end
|
69
|
-
|
125
|
+
|
70
126
|
# from: http://ruby.geraldbauer.ca/google-translation-api.html
|
71
|
-
def translate(text, to, from = 'en')
|
127
|
+
def translate(text, to, from = 'en', tries = 0)
|
72
128
|
|
73
129
|
return if to == from
|
74
|
-
|
75
|
-
require 'cgi'
|
76
|
-
require 'json'
|
77
|
-
require 'net/http'
|
78
|
-
|
79
|
-
base = 'http://ajax.googleapis.com/ajax/services/language/translate'
|
130
|
+
base = Babelphish::GOOGLE_AJAX_URL + 'translate'
|
80
131
|
# assemble query params
|
81
132
|
params = {
|
82
133
|
:langpair => "#{from}|#{to}",
|
@@ -90,10 +141,83 @@ module Babelphish
|
|
90
141
|
if json['responseStatus'] == 200
|
91
142
|
json['responseData']['translatedText']
|
92
143
|
else
|
93
|
-
|
144
|
+
if tries <= Babelphish::MAX_RETRIES
|
145
|
+
# Try again a few more times
|
146
|
+
translate(text, to, from, tries+=1)
|
147
|
+
else
|
148
|
+
puts "A problem occured while translating from #{from} to #{to}. "
|
149
|
+
puts "#{json['responseDetails']} "
|
150
|
+
puts "To retry only this translation try: 'babelphish -o -y #{@yml} -t #{to}' to retry the translation. Response: #{response}" if defined?(@yml)
|
151
|
+
raise Exceptions::GoogleResponseError, "#{json['responseDetails']}"
|
152
|
+
end
|
94
153
|
end
|
95
154
|
end
|
96
155
|
|
156
|
+
# translate from the 'from' language into all available languages
|
157
|
+
|
158
|
+
# multiple strings and multiple languages
|
159
|
+
#
|
160
|
+
# http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello%20world&langpair=en|it&q=goodbye&langpair=en|fr
|
161
|
+
# results from google look like this:
|
162
|
+
# {"responseData": [{"responseData":{"translatedText":"ciao mondo"},"responseDetails":null,"responseStatus":200},{"responseData":{"translatedText":"au revoir"},"responseDetails":null,"responseStatus":200}], "responseDetails": null, "responseStatus": 200}
|
163
|
+
#
|
164
|
+
# One string into multiple languages
|
165
|
+
# http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello%20world&langpair=en|it&langpair=en|fr
|
166
|
+
# results from google look like this:
|
167
|
+
# {"responseData": [{"responseData":{"translatedText":"ciao mondo"},"responseDetails":null,"responseStatus":200},{"responseData":{"translatedText":"Bonjour le Monde"},"responseDetails":null,"responseStatus":200}], "responseDetails": null, "responseStatus": 200}
|
168
|
+
#
|
169
|
+
def multiple_translate(text, tos, from = 'en', tries = 0)
|
170
|
+
base = Babelphish::GOOGLE_AJAX_URL + 'translate'
|
171
|
+
# assemble query params
|
172
|
+
params = {
|
173
|
+
:q => text,
|
174
|
+
:v => 1.0
|
175
|
+
}
|
176
|
+
query = params.map{ |k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
|
177
|
+
|
178
|
+
tos.each do |to|
|
179
|
+
query << "&langpair=" + CGI.escape("#{from}|#{to}")
|
180
|
+
end
|
181
|
+
|
182
|
+
response = Net::HTTP.get_response( URI.parse( "#{base}?#{query}" ) )
|
183
|
+
json = JSON.parse( response.body )
|
184
|
+
|
185
|
+
if json['responseStatus'] == 200
|
186
|
+
results = {}
|
187
|
+
json['responseData'].each_with_index do |data, index|
|
188
|
+
if data['responseStatus'] == 200
|
189
|
+
results[Babelphish::GoogleTranslate::LANGUAGES[index]] = data['responseData']['translatedText']
|
190
|
+
else
|
191
|
+
# retry the single translation
|
192
|
+
translate(text, Babelphish::GoogleTranslate::LANGUAGES[index], from)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
results
|
196
|
+
else
|
197
|
+
if tries <= Babelphish::MAX_RETRIES
|
198
|
+
# Try again a few more times
|
199
|
+
multiple_translate(text, tos, from, tries+=1)
|
200
|
+
else
|
201
|
+
puts "A problem occured while translating. Response: #{response}"
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
# Sends a string to google to attempt to detect the language.
|
207
|
+
# Returns an array indicating success/fail and the resulting data from google in a hash:
|
208
|
+
# {"language"=>"en", "confidence"=>0.08594032, "isReliable"=>false}
|
209
|
+
def detect_language(text)
|
210
|
+
request = Babelphish::GOOGLE_AJAX_URL + "detect?v=1.0&q=" + CGI.escape(text)
|
211
|
+
# send get request
|
212
|
+
response = Net::HTTP.get_response( URI.parse( request ) )
|
213
|
+
json = JSON.parse( response.body )
|
214
|
+
[json['responseStatus'] == 200, json['responseData']]
|
215
|
+
end
|
216
|
+
|
217
|
+
def supported_languages
|
218
|
+
Babelphish::GoogleTranslate::LANGUAGES
|
219
|
+
end
|
220
|
+
|
97
221
|
end
|
98
222
|
end
|
99
223
|
end
|
data/test/test_babelphish.rb
CHANGED
@@ -2,10 +2,64 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
2
2
|
|
3
3
|
class TestBabelphish < Test::Unit::TestCase
|
4
4
|
|
5
|
-
def
|
5
|
+
def test_translate
|
6
|
+
translation = Babelphish::Translator.translate('hello', 'es', 'en')
|
7
|
+
assert_equal 'hola', translation
|
6
8
|
end
|
7
9
|
|
8
|
-
def
|
9
|
-
|
10
|
+
def test_run_each_language
|
11
|
+
Babelphish::GoogleTranslate::LANGUAGES.each do |language|
|
12
|
+
begin
|
13
|
+
translation = Babelphish::Translator.translate('hello', language)
|
14
|
+
rescue => ex
|
15
|
+
puts "There was a problem translating to #{language}: #{ex}"
|
16
|
+
end
|
17
|
+
end
|
10
18
|
end
|
19
|
+
|
20
|
+
def test_multiple_translate
|
21
|
+
translations = Babelphish::Translator.multiple_translate('hello', Babelphish::GoogleTranslate::LANGUAGES, 'en')
|
22
|
+
assert_equal 'hola', translations[Babelphish::GoogleTranslate::SPANISH]
|
23
|
+
assert_equal 'hallo', translations[Babelphish::GoogleTranslate::GERMAN]
|
24
|
+
assert_equal 'ciao', translations[Babelphish::GoogleTranslate::ITALIAN]
|
25
|
+
assert_equal 'salut', translations[Babelphish::GoogleTranslate::ROMANIAN]
|
26
|
+
assert_equal 'こんにちは', translations[Babelphish::GoogleTranslate::JAPANESE]
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_detect_language
|
30
|
+
success, result = Babelphish::Translator.detect_language('hello world')
|
31
|
+
assert success, "Failed to detect language"
|
32
|
+
assert_equal Babelphish::GoogleTranslate::ENGLISH, result['language']
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_supported_languages
|
36
|
+
assert_equal Babelphish::Translator.supported_languages, Babelphish::GoogleTranslate::LANGUAGES
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_single_yml_translation
|
40
|
+
overwrite = true
|
41
|
+
translate_to = 'es'
|
42
|
+
yml = File.join(File.dirname(__FILE__), 'translations', 'en.yml')
|
43
|
+
Babelphish::Translator.translate_yaml(yml, overwrite, translate_to)
|
44
|
+
translated_yml = File.join(File.dirname(__FILE__), 'translations', "#{translate_to}.yml")
|
45
|
+
translation = YAML.load_file(translated_yml)
|
46
|
+
assert translation['es']
|
47
|
+
assert translation['es']['babelphish']
|
48
|
+
assert_equal "Este es un nivel más bajo", translation['es']['babelphish']['more']['test_more']
|
49
|
+
assert_equal "Esto es una prueba de cadenas", translation['es']['babelphish']['test']
|
50
|
+
assert_equal "Esta es una cadena con la incorporación {{insert}}", translation['es']['babelphish']['test_embedded']
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_multiple_yml_translation
|
54
|
+
overwrite = true
|
55
|
+
yml = File.join(File.dirname(__FILE__), 'translations', 'en.yml')
|
56
|
+
Babelphish::Translator.translate_yaml(yml, overwrite)
|
57
|
+
Babelphish::GoogleTranslate::LANGUAGES.each do |to|
|
58
|
+
translated_yml = File.join(File.dirname(__FILE__), 'translations', "#{to}.yml")
|
59
|
+
translation = YAML.load_file(translated_yml)
|
60
|
+
assert translation[to]
|
61
|
+
assert translation[to]['babelphish']['test_embedded'].include?("{{insert}}")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
11
65
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: babelphish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Ball
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-07-04 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- lib/babelphish.rb
|
66
66
|
- lib/babelphish/languages.rb
|
67
67
|
- lib/babelphish/translator.rb
|
68
|
+
- lib/babelphish/exceptions.rb
|
68
69
|
- lib/tasks/babelphish.rake
|
69
70
|
- script/console
|
70
71
|
- script/destroy
|
@@ -97,7 +98,7 @@ rubyforge_project: babelphish
|
|
97
98
|
rubygems_version: 1.3.1
|
98
99
|
signing_key:
|
99
100
|
specification_version: 2
|
100
|
-
summary: Translate with Google like a fule
|
101
|
+
summary: Translate with Google like a fule => 'fool'
|
101
102
|
test_files:
|
102
103
|
- test/test_babelphish.rb
|
103
104
|
- test/test_helper.rb
|