babelphish 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +23 -1
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/babelphish.gemspec +4 -5
- data/lib/babelphish/translator.rb +94 -45
- data/lib/babelphish/yml_translator.rb +23 -14
- data/lib/babelphish.rb +30 -4
- data/test/test_babelphish.rb +53 -6
- data/test/test_html_translator.rb +6 -6
- data/test/test_yml_translator.rb +36 -3
- metadata +7 -6
data/README.rdoc
CHANGED
@@ -6,6 +6,28 @@
|
|
6
6
|
|
7
7
|
babelphish makes it simple to translate you yaml files into all the languages supported by Google Translate
|
8
8
|
|
9
|
+
== NOTE:
|
10
|
+
|
11
|
+
detect_language does not currently supported v2 of the Google API - Google doesn't provide the functionality. But you can easily switch between
|
12
|
+
API version by calling Babelphish.set_settings({"api_key"=>"PUT YOUR KEY HERE", "version"=>"v2"}) or Babelphish.set_settings({"api_key"=>"", "version"=>"v1"}).
|
13
|
+
|
14
|
+
== CONFIG:
|
15
|
+
|
16
|
+
babelphish now supports version 2 of the Google API. To enable the new version you will need to get an API key from Google:
|
17
|
+
https://code.google.com/apis/console/?pli=1#welcome
|
18
|
+
|
19
|
+
Once you have the key create a file in your home directory called .babelphish.yml
|
20
|
+
The contents should look like this:
|
21
|
+
|
22
|
+
api_key: THE KEY YOU GET FROM GOOGLE GOES HERE
|
23
|
+
version: v2
|
24
|
+
|
25
|
+
You can also set the values programmatically:
|
26
|
+
|
27
|
+
Babelphish.set_settings({"api_key"=>"", "version"=>"v2"})
|
28
|
+
|
29
|
+
Google places a 100,000 character limit per day on each API key. Once you hit the limit Google will begin returning 403 errors.
|
30
|
+
|
9
31
|
== USAGE:
|
10
32
|
|
11
33
|
Translate the en.yml file into all supported languages:
|
@@ -146,7 +168,7 @@ VIETNAMESE = 'vi'
|
|
146
168
|
|
147
169
|
(The MIT License)
|
148
170
|
|
149
|
-
Copyright (c) 2009-
|
171
|
+
Copyright (c) 2009-2011 Justin Ball
|
150
172
|
|
151
173
|
Permission is hereby granted, free of charge, to any person obtaining
|
152
174
|
a copy of this software and associated documentation files (the
|
data/Rakefile
CHANGED
@@ -28,7 +28,7 @@ begin
|
|
28
28
|
gem.email = "justinball@gmail.com"
|
29
29
|
gem.homepage = "http://github.com/jbasdf/babelphish"
|
30
30
|
gem.description = "Babelphish helps you make a quick translation of your application using Google Translate."
|
31
|
-
gem.authors = ["Justin Ball"]
|
31
|
+
gem.authors = ["Justin Ball", "Michael Jenik"]
|
32
32
|
gem.rubyforge_project = "babelphish"
|
33
33
|
gem.add_dependency "ya2yaml"
|
34
34
|
gem.add_dependency "json"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/babelphish.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{babelphish}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Justin Ball"]
|
12
|
-
s.date = %q{2011-
|
11
|
+
s.authors = ["Justin Ball", "Michael Jenik"]
|
12
|
+
s.date = %q{2011-03-15}
|
13
13
|
s.default_executable = %q{babelphish}
|
14
14
|
s.description = %q{Babelphish helps you make a quick translation of your application using Google Translate.}
|
15
15
|
s.email = %q{justinball@gmail.com}
|
@@ -73,7 +73,7 @@ Gem::Specification.new do |s|
|
|
73
73
|
s.homepage = %q{http://github.com/jbasdf/babelphish}
|
74
74
|
s.require_paths = ["lib"]
|
75
75
|
s.rubyforge_project = %q{babelphish}
|
76
|
-
s.rubygems_version = %q{1.
|
76
|
+
s.rubygems_version = %q{1.6.0}
|
77
77
|
s.summary = %q{Translate with Google like a fule => 'fool'}
|
78
78
|
s.test_files = [
|
79
79
|
"test/test_babelphish.rb",
|
@@ -83,7 +83,6 @@ Gem::Specification.new do |s|
|
|
83
83
|
]
|
84
84
|
|
85
85
|
if s.respond_to? :specification_version then
|
86
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
87
86
|
s.specification_version = 3
|
88
87
|
|
89
88
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
@@ -6,27 +6,67 @@ module Babelphish
|
|
6
6
|
# from: http://ruby.geraldbauer.ca/google-translation-api.html
|
7
7
|
# translate text from 'from' to 'to'
|
8
8
|
def translate(text, to, from = 'en', tries = 0)
|
9
|
-
|
10
9
|
return if to == from
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
10
|
+
if text.is_a? Symbol
|
11
|
+
return text
|
12
|
+
end
|
13
|
+
|
14
|
+
if text.length > 1000 #actually the USI length limit is 2000
|
15
|
+
text_now = ""
|
16
|
+
text_rem = ""
|
17
|
+
text.split(".") do |text_chunk|
|
18
|
+
if text_now.length < 1000
|
19
|
+
text_now += (text_now.length == 0 ? "" : ".") + text_chunk
|
20
|
+
else
|
21
|
+
text_rem += (text_now.length == 0 ? "" : ".") + text_chunk
|
22
|
+
end
|
23
|
+
end
|
24
|
+
return translate(text_now, to, from) + "." + translate(text_rem, to, from)
|
25
|
+
end
|
26
|
+
|
27
|
+
base = Babelphish.google_ajax_url
|
28
|
+
if Babelphish.api_version == 'v2'
|
29
|
+
# assemble query params
|
30
|
+
params = {
|
31
|
+
:source => "#{from}",
|
32
|
+
:target => "#{to}",
|
33
|
+
:q => text,
|
34
|
+
:key => Babelphish.settings['api_key']
|
35
|
+
}
|
36
|
+
else
|
37
|
+
base << 'translate'
|
38
|
+
# assemble query params
|
39
|
+
params = {
|
40
|
+
:langpair => "#{from}|#{to}",
|
41
|
+
:source => "#{from}",
|
42
|
+
:target => "#{to}",
|
43
|
+
:q => text,
|
44
|
+
:v => 1.0
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
19
48
|
# send get request
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
49
|
+
uri = URI.parse(base)
|
50
|
+
uri.query = params.map{ |k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
|
51
|
+
http = Net::HTTP.new( uri.host, uri.port )
|
52
|
+
http.use_ssl = true if uri.scheme == "https" # enable SSL/TLS
|
53
|
+
response = nil
|
54
|
+
|
55
|
+
http.start {|http| response = http.request_get(uri.request_uri) }
|
56
|
+
|
57
|
+
if response.code == "200"
|
58
|
+
json = JSON.parse(response.body)
|
59
|
+
if Babelphish.api_version == 'v2'
|
60
|
+
json['data']['translations'][0]['translatedText']
|
61
|
+
else
|
62
|
+
json['responseData']['translatedText']
|
63
|
+
end
|
24
64
|
else
|
25
65
|
if tries <= Babelphish::MAX_RETRIES
|
26
66
|
# Try again a few more times
|
27
67
|
translate(text, to, from, tries+=1)
|
28
68
|
else
|
29
|
-
raise Exceptions::GoogleResponseError, "A problem occured while translating from #{from} to #{to}. #{
|
69
|
+
raise Exceptions::GoogleResponseError, "A problem occured while translating from #{from} to #{to}. #{response.body}"
|
30
70
|
end
|
31
71
|
end
|
32
72
|
end
|
@@ -45,50 +85,59 @@ module Babelphish
|
|
45
85
|
#
|
46
86
|
def multiple_translate(text, tos, from = 'en', tries = 0)
|
47
87
|
return {} if text.strip.empty? # Google doesn't like it when you send them an empty string
|
48
|
-
base = Babelphish::GOOGLE_AJAX_URL + 'translate'
|
49
|
-
# assemble query params
|
50
|
-
params = {
|
51
|
-
:q => text,
|
52
|
-
:v => 1.0
|
53
|
-
}
|
54
|
-
query = params.map{ |k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
|
55
|
-
|
56
|
-
tos.each do |to|
|
57
|
-
if !Babelphish::GoogleTranslate::LANGUAGES.include?(to)
|
58
|
-
raise Exceptions::GoogleResponseError, "#{to} is not a valid Google Translate code. Please be sure language codes are one of: #{Babelphish::GoogleTranslate::LANGUAGES.join(',')}"
|
59
|
-
end
|
60
|
-
query << "&langpair=" + CGI.escape("#{from}|#{to}")
|
61
|
-
end
|
62
88
|
|
63
|
-
|
64
|
-
json = JSON.parse( response.body )
|
65
|
-
|
66
|
-
if json['responseStatus'] == 200
|
89
|
+
if Babelphish.api_version == 'v2'
|
67
90
|
results = {}
|
68
|
-
|
69
|
-
|
70
|
-
results[tos[index]] = data['responseData']['translatedText']
|
71
|
-
else
|
72
|
-
# retry the single translation
|
73
|
-
translate(text, tos[index], from)
|
74
|
-
end
|
91
|
+
tos.each do |to|
|
92
|
+
results[to] = translate( text, to, from )
|
75
93
|
end
|
76
94
|
results
|
77
95
|
else
|
78
|
-
|
79
|
-
|
80
|
-
|
96
|
+
base = Babelphish.google_ajax_url + 'translate'
|
97
|
+
# assemble query params
|
98
|
+
params = {
|
99
|
+
:q => text,
|
100
|
+
:v => 1.0
|
101
|
+
}
|
102
|
+
query = params.map{ |k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
|
103
|
+
|
104
|
+
tos.each do |to|
|
105
|
+
if !Babelphish::GoogleTranslate::LANGUAGES.include?(to)
|
106
|
+
raise Exceptions::GoogleResponseError, "#{to} is not a valid Google Translate code. Please be sure language codes are one of: #{Babelphish::GoogleTranslate::LANGUAGES.join(',')}"
|
107
|
+
end
|
108
|
+
query << "&langpair=" + CGI.escape("#{from}|#{to}")
|
109
|
+
end
|
110
|
+
|
111
|
+
response = Net::HTTP.get_response( URI.parse( "#{base}?#{query}" ) )
|
112
|
+
json = JSON.parse( response.body )
|
113
|
+
|
114
|
+
if json['responseStatus'] == 200
|
115
|
+
results = {}
|
116
|
+
if json['responseData'].is_a?(Array)
|
117
|
+
json['responseData'].each_with_index do |data, index|
|
118
|
+
results[tos[index]] = data['responseData']['translatedText']
|
119
|
+
end
|
120
|
+
else
|
121
|
+
results[tos[0]] = json['responseData']['translatedText']
|
122
|
+
end
|
123
|
+
results
|
81
124
|
else
|
82
|
-
|
125
|
+
if tries <= Babelphish::MAX_RETRIES
|
126
|
+
# Try again a few more times
|
127
|
+
multiple_translate(text, tos, from, tries+=1)
|
128
|
+
else
|
129
|
+
raise Exceptions::GoogleResponseError, "A problem occured while translating. #{response} -- #{response.body} -- From: #{from} -- Text: #{text}"
|
130
|
+
end
|
83
131
|
end
|
84
132
|
end
|
133
|
+
|
85
134
|
end
|
86
135
|
|
87
136
|
# Sends a string to google to attempt to detect the language.
|
88
137
|
# Returns an array indicating success/fail and the resulting data from google in a hash:
|
89
138
|
# {"language"=>"en", "confidence"=>0.08594032, "isReliable"=>false}
|
90
139
|
def detect_language(text)
|
91
|
-
request = Babelphish
|
140
|
+
request = Babelphish.google_ajax_url + "detect?v=1.0&q=" + CGI.escape(text)
|
92
141
|
# send get request
|
93
142
|
response = Net::HTTP.get_response( URI.parse( request ) )
|
94
143
|
json = JSON.parse( response.body )
|
@@ -101,4 +150,4 @@ module Babelphish
|
|
101
150
|
|
102
151
|
end
|
103
152
|
end
|
104
|
-
end
|
153
|
+
end
|
@@ -44,20 +44,29 @@ module Babelphish
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def translate_keys(translate_hash, to, from)
|
47
|
-
translate_hash.
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
remove_substitutions(translate_hash[key])
|
47
|
+
if translate_hash.is_a?(String)
|
48
|
+
add_substitutions(translate_hash)
|
49
|
+
translate_hash = Babelphish::Translator.translate(translate_hash, to, from)
|
50
|
+
remove_substitutions(translate_hash)
|
51
|
+
elsif translate_hash.is_a?(Array)
|
52
|
+
translate_hash.map!{ |x| if !x.nil? then Babelphish::Translator.translate(x, to, from) else "" end }
|
53
|
+
elsif translate_hash.is_a?(Hash)
|
54
|
+
translate_hash.each_key do |key|
|
55
|
+
next if translate_hash[key].is_a?(Fixnum)
|
56
|
+
if translate_hash[key].is_a?(Hash) || translate_hash[key].is_a?(Array)
|
57
|
+
translate_keys(translate_hash[key], to, from)
|
59
58
|
else
|
60
|
-
|
59
|
+
if key == false
|
60
|
+
puts "Key #{key} was evaluated as false. Check your yml file and be sure to escape values like no with 'no'. ie 'no': 'No'"
|
61
|
+
elsif key == true
|
62
|
+
puts "Key #{key} was evaluated as true. Check your yml file and be sure to escape values like yes with 'yes'. ie 'yes': 'Yes'"
|
63
|
+
elsif !translate_hash[key].nil?
|
64
|
+
add_substitutions(translate_hash[key])
|
65
|
+
translate_hash[key] = Babelphish::Translator.translate(translate_hash[key], to, from)
|
66
|
+
remove_substitutions(translate_hash[key])
|
67
|
+
else
|
68
|
+
puts "Key #{key} contains no data"
|
69
|
+
end
|
61
70
|
end
|
62
71
|
end
|
63
72
|
end
|
@@ -139,4 +148,4 @@ module Babelphish
|
|
139
148
|
|
140
149
|
end
|
141
150
|
end
|
142
|
-
end
|
151
|
+
end
|
data/lib/babelphish.rb
CHANGED
@@ -4,7 +4,8 @@ $:.unshift(File.dirname(__FILE__)) unless
|
|
4
4
|
require 'yaml'
|
5
5
|
require 'cgi'
|
6
6
|
require 'json'
|
7
|
-
require 'net/http'
|
7
|
+
#require 'net/http'
|
8
|
+
require 'net/https'
|
8
9
|
|
9
10
|
begin
|
10
11
|
require 'jcode'
|
@@ -35,10 +36,35 @@ require File.dirname(__FILE__) + '/../lib/babelphish/html_translator'
|
|
35
36
|
$KCODE = 'UTF8'
|
36
37
|
|
37
38
|
module Babelphish
|
38
|
-
|
39
|
-
GOOGLE_AJAX_URL = "http://ajax.googleapis.com/ajax/services/language/"
|
39
|
+
|
40
40
|
MAX_RETRIES = 3
|
41
41
|
|
42
|
+
def self.google_ajax_url
|
43
|
+
if api_version == 'v2'
|
44
|
+
"https://www.googleapis.com/language/translate/v2"
|
45
|
+
else
|
46
|
+
"http://ajax.googleapis.com/ajax/services/language/"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.api_version
|
51
|
+
self.settings['version']
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.settings
|
55
|
+
return @settings if @settings
|
56
|
+
babelphish_settings_file = File.join(File.expand_path("~"), ".babelphish.yml")
|
57
|
+
if File.exist?(babelphish_settings_file)
|
58
|
+
@settings = YAML.load_file(babelphish_settings_file)
|
59
|
+
else
|
60
|
+
@settings = {"api_key"=>"", "version"=>"v1"}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.set_settings(settings)
|
65
|
+
@settings = settings
|
66
|
+
end
|
67
|
+
|
42
68
|
def self.load_tasks
|
43
69
|
if File.exists?('Rakefile')
|
44
70
|
load 'Rakefile'
|
@@ -49,4 +75,4 @@ module Babelphish
|
|
49
75
|
end
|
50
76
|
end
|
51
77
|
|
52
|
-
end
|
78
|
+
end
|
data/test/test_babelphish.rb
CHANGED
@@ -3,12 +3,15 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
3
3
|
class TestBabelphish < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def test_translate
|
6
|
+
Babelphish.set_settings(nil)
|
6
7
|
translation = Babelphish::Translator.translate('hello', 'es', 'en')
|
7
|
-
assert_equal '
|
8
|
+
assert_equal 'hola', translation
|
8
9
|
end
|
9
10
|
|
10
11
|
def test_run_each_language
|
11
|
-
Babelphish
|
12
|
+
Babelphish.set_settings(nil)
|
13
|
+
#Babelphish::GoogleTranslate::LANGUAGES.each do |language| # Running all languages is time consuming and probably pisses off google
|
14
|
+
['es', 'fr'].each do |language|
|
12
15
|
begin
|
13
16
|
translation = Babelphish::Translator.translate('hello', language)
|
14
17
|
rescue => ex
|
@@ -18,21 +21,65 @@ class TestBabelphish < Test::Unit::TestCase
|
|
18
21
|
end
|
19
22
|
|
20
23
|
def test_multiple_translate
|
21
|
-
|
22
|
-
|
24
|
+
Babelphish.set_settings(nil)
|
25
|
+
translations = Babelphish::Translator.multiple_translate('hello', [Babelphish::GoogleTranslate::SPANISH,
|
26
|
+
Babelphish::GoogleTranslate::GERMAN,
|
27
|
+
Babelphish::GoogleTranslate::ITALIAN,
|
28
|
+
Babelphish::GoogleTranslate::ROMANIAN,
|
29
|
+
Babelphish::GoogleTranslate::JAPANESE], 'en')
|
30
|
+
assert_equal 'hola', translations[Babelphish::GoogleTranslate::SPANISH]
|
23
31
|
assert_equal 'hallo', translations[Babelphish::GoogleTranslate::GERMAN]
|
24
32
|
assert_equal 'ciao', translations[Babelphish::GoogleTranslate::ITALIAN]
|
25
33
|
assert_equal 'alo', translations[Babelphish::GoogleTranslate::ROMANIAN]
|
26
34
|
assert_equal 'こんにちは', translations[Babelphish::GoogleTranslate::JAPANESE]
|
27
35
|
end
|
28
36
|
|
29
|
-
def
|
37
|
+
def test_supported_languages
|
38
|
+
Babelphish.set_settings(nil)
|
39
|
+
assert_equal Babelphish::Translator.supported_languages, Babelphish::GoogleTranslate::LANGUAGES
|
40
|
+
end
|
41
|
+
|
42
|
+
# run same tests with v1 of the api
|
43
|
+
def test_translate_v1
|
44
|
+
Babelphish.set_settings({"api_key"=>"", "version"=>"v1"})
|
45
|
+
translation = Babelphish::Translator.translate('hello', 'es', 'en')
|
46
|
+
assert_equal 'hola', translation
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_run_each_language_v1
|
50
|
+
Babelphish.set_settings({"api_key"=>"", "version"=>"v1"})
|
51
|
+
['es'].each do |language|
|
52
|
+
begin
|
53
|
+
translation = Babelphish::Translator.translate('hello', language)
|
54
|
+
rescue => ex
|
55
|
+
puts "There was a problem translating to #{language}: #{ex}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_multiple_translate_v1
|
61
|
+
Babelphish.set_settings({"api_key"=>"", "version"=>"v1"})
|
62
|
+
translations = Babelphish::Translator.multiple_translate('hello', [Babelphish::GoogleTranslate::SPANISH,
|
63
|
+
Babelphish::GoogleTranslate::GERMAN,
|
64
|
+
Babelphish::GoogleTranslate::ITALIAN,
|
65
|
+
Babelphish::GoogleTranslate::ROMANIAN,
|
66
|
+
Babelphish::GoogleTranslate::JAPANESE], 'en')
|
67
|
+
assert_equal 'hola', translations[Babelphish::GoogleTranslate::SPANISH]
|
68
|
+
assert_equal 'hallo', translations[Babelphish::GoogleTranslate::GERMAN]
|
69
|
+
assert_equal 'ciao', translations[Babelphish::GoogleTranslate::ITALIAN]
|
70
|
+
assert_equal 'alo', translations[Babelphish::GoogleTranslate::ROMANIAN]
|
71
|
+
assert_equal 'こんにちは', translations[Babelphish::GoogleTranslate::JAPANESE]
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_detect_language_v1
|
75
|
+
Babelphish.set_settings({"api_key"=>"", "version"=>"v1"})
|
30
76
|
success, result = Babelphish::Translator.detect_language('hello world')
|
31
77
|
assert success, "Failed to detect language"
|
32
78
|
assert_equal Babelphish::GoogleTranslate::ENGLISH, result['language']
|
33
79
|
end
|
34
80
|
|
35
|
-
def
|
81
|
+
def test_supported_languages_v1
|
82
|
+
Babelphish.set_settings({"api_key"=>"", "version"=>"v1"})
|
36
83
|
assert_equal Babelphish::Translator.supported_languages, Babelphish::GoogleTranslate::LANGUAGES
|
37
84
|
end
|
38
85
|
|
@@ -6,8 +6,8 @@ class TestHtmlTranslator < Test::Unit::TestCase
|
|
6
6
|
overwrite = true
|
7
7
|
translate_from = 'en'
|
8
8
|
directory = File.join(File.dirname(__FILE__), 'html_translations')
|
9
|
-
Babelphish::HtmlTranslator.translate(directory,
|
10
|
-
|
9
|
+
Babelphish::HtmlTranslator.translate(directory, ['es'], translate_from, overwrite)
|
10
|
+
['es'].each do |to|
|
11
11
|
if to != translate_from
|
12
12
|
# This will make sure the file was created
|
13
13
|
translated_html = File.join(File.dirname(__FILE__), 'html_translations', "test.#{to}.html.erb")
|
@@ -23,8 +23,8 @@ class TestHtmlTranslator < Test::Unit::TestCase
|
|
23
23
|
overwrite = true
|
24
24
|
translate_from = 'en'
|
25
25
|
directory = File.join(File.dirname(__FILE__), 'html_translations')
|
26
|
-
Babelphish::HtmlTranslator.translate(directory,
|
27
|
-
|
26
|
+
Babelphish::HtmlTranslator.translate(directory, ['es'], translate_from, overwrite)
|
27
|
+
['es'].each do |to|
|
28
28
|
if to != translate_from
|
29
29
|
# This will make sure the file was created
|
30
30
|
translated_html = File.join(File.dirname(__FILE__), 'html_translations', "test.text.#{to}.html.erb")
|
@@ -40,8 +40,8 @@ class TestHtmlTranslator < Test::Unit::TestCase
|
|
40
40
|
overwrite = true
|
41
41
|
translate_from = 'en'
|
42
42
|
directory = File.join(File.dirname(__FILE__), 'html_translations')
|
43
|
-
Babelphish::HtmlTranslator.translate(directory,
|
44
|
-
|
43
|
+
Babelphish::HtmlTranslator.translate(directory, ['es'], translate_from, overwrite)
|
44
|
+
['es'].each do |to|
|
45
45
|
if to != translate_from
|
46
46
|
# This will make sure the file was created
|
47
47
|
translated_html = File.join(File.dirname(__FILE__), 'html_translations', "test.text.#{to}.plain.erb")
|
data/test/test_yml_translator.rb
CHANGED
@@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
3
3
|
class TestYmlTranslator < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def test_single_yml_translation
|
6
|
+
Babelphish.set_settings(nil) # Make sure the .babelphish.yml settings are used
|
6
7
|
overwrite = true
|
7
8
|
translate_to = 'es'
|
8
9
|
yml = File.join(File.dirname(__FILE__), 'translations', 'en.yml')
|
@@ -12,14 +13,46 @@ class TestYmlTranslator < Test::Unit::TestCase
|
|
12
13
|
assert translation['es']
|
13
14
|
assert translation['es']['babelphish']
|
14
15
|
assert_equal "Se trata de un nivel inferior", translation['es']['babelphish']['more']['test_more']
|
15
|
-
assert_equal "
|
16
|
-
assert_equal "
|
16
|
+
assert_equal "Esta es una cadena de prueba", translation['es']['babelphish']['test']
|
17
|
+
assert_equal "Esto es una cadena con incrustación {{insert}}", translation['es']['babelphish']['test_embedded']
|
17
18
|
end
|
18
19
|
|
19
20
|
def test_multiple_yml_translation
|
21
|
+
Babelphish.set_settings(nil) # Make sure the .babelphish.yml settings are used
|
20
22
|
overwrite = true
|
21
23
|
yml = File.join(File.dirname(__FILE__), 'translations', 'en.yml')
|
22
|
-
tos =
|
24
|
+
tos = ['es']
|
25
|
+
Babelphish::YmlTranslator.translate(yml, overwrite, nil, tos)
|
26
|
+
tos.each do |to|
|
27
|
+
translated_yml = File.join(File.dirname(__FILE__), 'translations', "#{to}.yml")
|
28
|
+
translation = YAML.load_file(translated_yml)
|
29
|
+
assert translation[to]
|
30
|
+
assert translation[to]['babelphish']['test_embedded'].include?("{{insert}}")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
# run same tests with v1 of the api
|
36
|
+
def test_single_yml_translation_v1
|
37
|
+
Babelphish.set_settings({"api_key"=>"", "version"=>"v1"})
|
38
|
+
overwrite = true
|
39
|
+
translate_to = 'es'
|
40
|
+
yml = File.join(File.dirname(__FILE__), 'translations', 'en.yml')
|
41
|
+
Babelphish::YmlTranslator.translate(yml, overwrite, translate_to)
|
42
|
+
translated_yml = File.join(File.dirname(__FILE__), 'translations', "#{translate_to}.yml")
|
43
|
+
translation = YAML.load_file(translated_yml)
|
44
|
+
assert translation['es']
|
45
|
+
assert translation['es']['babelphish']
|
46
|
+
assert_equal "Se trata de un nivel inferior", translation['es']['babelphish']['more']['test_more']
|
47
|
+
assert_equal "Esta es una cadena de prueba", translation['es']['babelphish']['test']
|
48
|
+
assert_equal "Esto es una cadena con incrustación {{insert}}", translation['es']['babelphish']['test_embedded']
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_multiple_yml_translation_v1
|
52
|
+
Babelphish.set_settings({"api_key"=>"", "version"=>"v1"})
|
53
|
+
overwrite = true
|
54
|
+
yml = File.join(File.dirname(__FILE__), 'translations', 'en.yml')
|
55
|
+
tos = ['es']
|
23
56
|
Babelphish::YmlTranslator.translate(yml, overwrite, nil, tos)
|
24
57
|
tos.each do |to|
|
25
58
|
translated_yml = File.join(File.dirname(__FILE__), 'translations', "#{to}.yml")
|
metadata
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: babelphish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 15
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 4
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Justin Ball
|
14
|
+
- Michael Jenik
|
14
15
|
autorequire:
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2011-
|
19
|
+
date: 2011-03-15 00:00:00 -06:00
|
19
20
|
default_executable: babelphish
|
20
21
|
dependencies:
|
21
22
|
- !ruby/object:Gem::Dependency
|
@@ -136,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
137
|
requirements: []
|
137
138
|
|
138
139
|
rubyforge_project: babelphish
|
139
|
-
rubygems_version: 1.
|
140
|
+
rubygems_version: 1.6.0
|
140
141
|
signing_key:
|
141
142
|
specification_version: 3
|
142
143
|
summary: Translate with Google like a fule => 'fool'
|