ottra 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +9 -0
- data/README.textile +56 -0
- data/Rakefile +14 -0
- data/lib/config/languages.yml +43 -0
- data/lib/ottra.rb +44 -0
- data/lib/translator_base.rb +12 -0
- data/lib/translators/google_translate.rb +16 -0
- data/lib/translators/shvets_google_translate.rb +17 -0
- data/lib/translators/tranexp_translate.rb +19 -0
- data/ottra.gemspec +39 -0
- metadata +104 -0
data/Manifest
ADDED
data/README.textile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
h1. Ottra
|
2
|
+
|
3
|
+
One Translator To Rule them All!
|
4
|
+
|
5
|
+
h2. The Problem
|
6
|
+
|
7
|
+
The problem with language translation in ruby right now is that everyone does it differently. If you want to use an external source to translate text in your application then you need to write you code for a specific gem. What do you do if you realize, post implementation, that the gem doesn't support the language combinations or types that you need? That leaves you in quite a pickle.
|
8
|
+
|
9
|
+
h2. The Game Changer
|
10
|
+
|
11
|
+
Ottra is different - it uses a bunch of different gems in an attempt to translate your text. When you first create a new instance of Ottra, it runs a test translation to each of the different gems it supports and then orders them by whichever was quickest. Then it goes through each of them trying to translate your text. The best part? You write your code for Ottra and then let the gem handle the oddities among the other gems. It's that simple.
|
12
|
+
|
13
|
+
h2. Sample Code
|
14
|
+
|
15
|
+
Default, simple example.
|
16
|
+
<pre><code>require 'ottra'
|
17
|
+
t = Ottra.new
|
18
|
+
puts t.translate_text('Hello world!', 'it')
|
19
|
+
=> Ciao mondo!
|
20
|
+
</code></pre>
|
21
|
+
|
22
|
+
Maybe you only want to translate a single string and then send t off to the garbage collector. It might be best to avoid sorting the translators by speed.
|
23
|
+
<pre><code>t = Ottra.new({:no_sorting => true})
|
24
|
+
puts t.translate_text('Hello world!', 'it')
|
25
|
+
=> Ciao mondo!
|
26
|
+
</code></pre>
|
27
|
+
|
28
|
+
If your starting language is not English then you need to specify it as the second parameter.
|
29
|
+
<pre><code>puts t.translate_text('Ciao mondo!', 'en', 'it')
|
30
|
+
=> Hello World!
|
31
|
+
</code></pre>
|
32
|
+
|
33
|
+
h2. Supported Translation Gems
|
34
|
+
|
35
|
+
Currently, Ottra supports shvets-google_translate, googletranslate, and tranexp. There are tons of other translation gems out there, but I haven't gotten a chance to build in support yet.
|
36
|
+
|
37
|
+
h2. But...
|
38
|
+
|
39
|
+
Github is social coding! Got a favorite translation gem? Fork this project, add your support, and send me a pull request!
|
40
|
+
|
41
|
+
h2. What About Tests?
|
42
|
+
|
43
|
+
I didn't write any.
|
44
|
+
|
45
|
+
h2. But But But! That's Blasphemy!
|
46
|
+
|
47
|
+
Too bad. Fork, write, and send me a pull request.
|
48
|
+
|
49
|
+
h2. Other Notes
|
50
|
+
|
51
|
+
This stuff was originally written as part of the Whatever You Say project (rails app that provides chats with on the fly translation) and then ported to an individual gem. It's pretty rough around the edges in all aspects. It is missing things like:
|
52
|
+
1. Tests
|
53
|
+
2. Installing the supported gems when you install this gem
|
54
|
+
3. Bacon
|
55
|
+
|
56
|
+
Otherwise, it works pretty well. Give it a try and if you find something you can't/won't fix then add a github ticket.
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('ottra', '0.1.0') do |p|
|
6
|
+
p.description = 'One Translator (gem) To Rule (them) All! Uses various translation gems to do language translating.'
|
7
|
+
p.url = 'http://github.com/patricktulskie/ottra'
|
8
|
+
p.author = 'Patrick Tulskie'
|
9
|
+
p.email = 'PatrickTulskie@gmail.com'
|
10
|
+
p.ignore_pattern = ['tmp/*', 'script/*', 'lib/main.rb']
|
11
|
+
p.dependencies = ['shvets-google_translate', 'googletranslate', 'tranexp']
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
@@ -0,0 +1,43 @@
|
|
1
|
+
uk: Ukrainian
|
2
|
+
it: Italian
|
3
|
+
zh-CN: Chinese Simplified
|
4
|
+
no: Norwegian
|
5
|
+
ja: Japanese
|
6
|
+
hi: Hindi
|
7
|
+
de: German
|
8
|
+
fr: French
|
9
|
+
sk: Slovak
|
10
|
+
hu: Hungarian
|
11
|
+
tl: Filipino
|
12
|
+
zh: Chinese
|
13
|
+
es: Spanish
|
14
|
+
sv: Swedish
|
15
|
+
sl: Slovenian
|
16
|
+
ru: Russian
|
17
|
+
iw: Hebrew
|
18
|
+
fi: Finnish
|
19
|
+
zh-TW: Chinese Traditional
|
20
|
+
pt: Portuguese
|
21
|
+
ko: Korean
|
22
|
+
et: Estonian
|
23
|
+
id: Indonesian
|
24
|
+
gl: Galician
|
25
|
+
cs: Czech
|
26
|
+
bg: Bulgarian
|
27
|
+
pl: Polish
|
28
|
+
ar: Arabic
|
29
|
+
ro: Romanian
|
30
|
+
mt: Maltese
|
31
|
+
el: Greek
|
32
|
+
da: Danish
|
33
|
+
vi: Vietnamese
|
34
|
+
tr: Turkish
|
35
|
+
lt: Lithuanian
|
36
|
+
nl: Dutch
|
37
|
+
ca: Catalan
|
38
|
+
sq: Albanian
|
39
|
+
th: Thai
|
40
|
+
sr: Serbian
|
41
|
+
en: English
|
42
|
+
lv: Latvian
|
43
|
+
hr: Croatian
|
data/lib/ottra.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'translator_base'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
class Ottra
|
5
|
+
|
6
|
+
attr_accessor :translators, :response_times, :languages
|
7
|
+
|
8
|
+
def initialize(options = { })
|
9
|
+
# Let's set up our languages. I don't like global variables, but I
|
10
|
+
$ottra_languages = YAML.load_file(File.expand_path(File.dirname(__FILE__)) + '/config/languages.yml')
|
11
|
+
|
12
|
+
# Let's get our translators setup
|
13
|
+
translator_classes = Dir.glob(File.expand_path(File.dirname(__FILE__)) + '/translators/*')
|
14
|
+
translator_classes.each { |translator| require translator }
|
15
|
+
|
16
|
+
# Now let's build a list of translator classes we can interact with
|
17
|
+
@translators = translator_classes.map do |translator|
|
18
|
+
# This is really a terrible way to do this...
|
19
|
+
t = translator.split(/\/|\\/).last.gsub('.rb', '').gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
20
|
+
Object.const_get(t)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Let's find out how each of them are responding so we know which one to try first, unless we want to just get going
|
24
|
+
options[:no_sorting] ? @translators = @translators.sort { |a, b| a.to_s <=> b.to_s } : sort_by_response_times
|
25
|
+
end
|
26
|
+
|
27
|
+
def translate_text(text, destination, source="en")
|
28
|
+
result = text
|
29
|
+
|
30
|
+
@translators.each do |translator|
|
31
|
+
t = translator.new
|
32
|
+
result = t.translate(text, source, destination)
|
33
|
+
break unless text == result || (result.match(/Error: Translation from /))
|
34
|
+
end
|
35
|
+
|
36
|
+
return result
|
37
|
+
end
|
38
|
+
|
39
|
+
def sort_by_response_times
|
40
|
+
@response_times = @translators.inject({}) { |times, translator| times[translator.to_s] = translator.response_time; times }
|
41
|
+
@translators = @translators.sort { |a, b| @response_times[a.to_s] <=> @response_times[b.to_s] }
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rtranslate'
|
2
|
+
|
3
|
+
class GoogleTranslate < TranslatorBase
|
4
|
+
|
5
|
+
def translate(text, source, destination)
|
6
|
+
result = text
|
7
|
+
begin
|
8
|
+
result = Translate.t(text, source, destination)
|
9
|
+
rescue
|
10
|
+
# Nothing!
|
11
|
+
ensure
|
12
|
+
return result
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'google_translate'
|
2
|
+
|
3
|
+
class ShvetsGoogleTranslate < TranslatorBase
|
4
|
+
|
5
|
+
def translate(text, source, destination)
|
6
|
+
result = text
|
7
|
+
begin
|
8
|
+
shvet_google = Google::Translator.new
|
9
|
+
result = shvet_google.translate(source.to_sym, destination.to_sym, text)
|
10
|
+
rescue
|
11
|
+
# Just do nothing...
|
12
|
+
ensure
|
13
|
+
return result
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'tranexp'
|
2
|
+
|
3
|
+
class TranexpTranslate < TranslatorBase
|
4
|
+
|
5
|
+
def translate(text, source, destination)
|
6
|
+
result = text
|
7
|
+
begin
|
8
|
+
tran_source = eval("Tranexp::Http::#{$ottra_languages[source]}")
|
9
|
+
tran_dest = eval("Tranexp::Http::#{$ottra_languages[source]}")
|
10
|
+
tranexp = Tranexp::Http.new
|
11
|
+
result = tranexp.translate(text, tran_source, tran_dest)
|
12
|
+
rescue
|
13
|
+
# just do nothing
|
14
|
+
ensure
|
15
|
+
return result
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/ottra.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{ottra}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Patrick Tulskie"]
|
9
|
+
s.date = %q{2009-10-28}
|
10
|
+
s.description = %q{One Translator (gem) To Rule (them) All! Uses various translation gems to do language translating.}
|
11
|
+
s.email = %q{PatrickTulskie@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["README.textile", "lib/config/languages.yml", "lib/ottra.rb", "lib/translator_base.rb", "lib/translators/google_translate.rb", "lib/translators/shvets_google_translate.rb", "lib/translators/tranexp_translate.rb"]
|
13
|
+
s.files = ["Manifest", "README.textile", "Rakefile", "lib/config/languages.yml", "lib/ottra.rb", "lib/translator_base.rb", "lib/translators/google_translate.rb", "lib/translators/shvets_google_translate.rb", "lib/translators/tranexp_translate.rb", "ottra.gemspec"]
|
14
|
+
s.homepage = %q{http://github.com/patricktulskie/ottra}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ottra", "--main", "README.textile"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{ottra}
|
18
|
+
s.rubygems_version = %q{1.3.5}
|
19
|
+
s.summary = %q{One Translator (gem) To Rule (them) All! Uses various translation gems to do language translating.}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
+
s.add_runtime_dependency(%q<shvets-google_translate>, [">= 0"])
|
27
|
+
s.add_runtime_dependency(%q<googletranslate>, [">= 0"])
|
28
|
+
s.add_runtime_dependency(%q<tranexp>, [">= 0"])
|
29
|
+
else
|
30
|
+
s.add_dependency(%q<shvets-google_translate>, [">= 0"])
|
31
|
+
s.add_dependency(%q<googletranslate>, [">= 0"])
|
32
|
+
s.add_dependency(%q<tranexp>, [">= 0"])
|
33
|
+
end
|
34
|
+
else
|
35
|
+
s.add_dependency(%q<shvets-google_translate>, [">= 0"])
|
36
|
+
s.add_dependency(%q<googletranslate>, [">= 0"])
|
37
|
+
s.add_dependency(%q<tranexp>, [">= 0"])
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ottra
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Patrick Tulskie
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-28 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: shvets-google_translate
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: googletranslate
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: tranexp
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
description: One Translator (gem) To Rule (them) All! Uses various translation gems to do language translating.
|
46
|
+
email: PatrickTulskie@gmail.com
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- README.textile
|
53
|
+
- lib/config/languages.yml
|
54
|
+
- lib/ottra.rb
|
55
|
+
- lib/translator_base.rb
|
56
|
+
- lib/translators/google_translate.rb
|
57
|
+
- lib/translators/shvets_google_translate.rb
|
58
|
+
- lib/translators/tranexp_translate.rb
|
59
|
+
files:
|
60
|
+
- Manifest
|
61
|
+
- README.textile
|
62
|
+
- Rakefile
|
63
|
+
- lib/config/languages.yml
|
64
|
+
- lib/ottra.rb
|
65
|
+
- lib/translator_base.rb
|
66
|
+
- lib/translators/google_translate.rb
|
67
|
+
- lib/translators/shvets_google_translate.rb
|
68
|
+
- lib/translators/tranexp_translate.rb
|
69
|
+
- ottra.gemspec
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://github.com/patricktulskie/ottra
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options:
|
76
|
+
- --line-numbers
|
77
|
+
- --inline-source
|
78
|
+
- --title
|
79
|
+
- Ottra
|
80
|
+
- --main
|
81
|
+
- README.textile
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: "0"
|
89
|
+
version:
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: "1.2"
|
95
|
+
version:
|
96
|
+
requirements: []
|
97
|
+
|
98
|
+
rubyforge_project: ottra
|
99
|
+
rubygems_version: 1.3.5
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: One Translator (gem) To Rule (them) All! Uses various translation gems to do language translating.
|
103
|
+
test_files: []
|
104
|
+
|