rbabel 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +13 -0
- data/COPYING +18 -0
- data/README +31 -0
- data/Rakefile +129 -0
- data/bin/rbabel +23 -0
- data/lib/rbabel.rb +6 -0
- data/lib/rbabel/core_ext.rb +12 -0
- data/lib/rbabel/translation.rb +32 -0
- data/spec/core_ext_spec.rb +12 -0
- data/spec/rcov.opts +4 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +1 -0
- metadata +87 -0
data/CHANGELOG
ADDED
data/COPYING
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2008 Sharon Rosner
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to
|
5
|
+
deal in the Software without restriction, including without limitation the
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
16
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
== Babel: Automatic language translation
|
2
|
+
|
3
|
+
Babel is tiny library for translating between languages using Google's automatic language translation ({more info}[http://www.google.com/intl/en/help/faq_translation.html]).
|
4
|
+
|
5
|
+
=== Installation
|
6
|
+
|
7
|
+
sudo gem install rbabel
|
8
|
+
|
9
|
+
=== Usage
|
10
|
+
|
11
|
+
require 'rbabel'
|
12
|
+
Babel.translate("hello", :en, :fr) #=> "Bonjour"
|
13
|
+
|
14
|
+
You can also translate a text into multiple languages by supplying multiple destination languages:
|
15
|
+
|
16
|
+
Babel.translate("hello", :en, :fr, :it) #=> ["Bonjour", "Ciao"]
|
17
|
+
|
18
|
+
Babel extends String with a #translate method for convenience:
|
19
|
+
|
20
|
+
"hello".translate(:en, :fr) #=> "Bonjour"
|
21
|
+
"hello".translate(:en, :fr, :it) #=> ["Bonjour", "Ciao"]
|
22
|
+
|
23
|
+
=== Command line utility
|
24
|
+
|
25
|
+
Babel can also be used as a command line utility that translates stdin to stdout. Example usage:
|
26
|
+
|
27
|
+
cat english.txt | rbabel en:fr > french.txt
|
28
|
+
|
29
|
+
=== Contact
|
30
|
+
|
31
|
+
If you have any comments or suggestions please send an email to ciconia at gmail.com and I'll get back to you.
|
data/Rakefile
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
require "rake"
|
2
|
+
require "rake/clean"
|
3
|
+
require "rake/gempackagetask"
|
4
|
+
require "rake/rdoctask"
|
5
|
+
require "fileutils"
|
6
|
+
include FileUtils
|
7
|
+
|
8
|
+
##############################################################################
|
9
|
+
# Configuration
|
10
|
+
##############################################################################
|
11
|
+
NAME = "rbabel"
|
12
|
+
VERS = "0.2"
|
13
|
+
CLEAN.include ["**/.*.sw?", "pkg/*", ".config", "doc/*", "coverage/*"]
|
14
|
+
RDOC_OPTS = [
|
15
|
+
"--quiet",
|
16
|
+
"--title", "Babel: automatic translation",
|
17
|
+
"--opname", "index.html",
|
18
|
+
"--line-numbers",
|
19
|
+
"--main", "README",
|
20
|
+
"--inline-source"
|
21
|
+
]
|
22
|
+
|
23
|
+
##############################################################################
|
24
|
+
# RDoc
|
25
|
+
##############################################################################
|
26
|
+
task :doc => [:rdoc]
|
27
|
+
|
28
|
+
Rake::RDocTask.new do |rdoc|
|
29
|
+
rdoc.rdoc_dir = "doc/rdoc"
|
30
|
+
rdoc.options += RDOC_OPTS
|
31
|
+
rdoc.main = "README"
|
32
|
+
rdoc.title = "Babel: automatic translation"
|
33
|
+
rdoc.rdoc_files.add ["README", "COPYING", "lib/rbabel.rb", "lib/**/*.rb"]
|
34
|
+
end
|
35
|
+
|
36
|
+
task :doc_rforge => [:doc]
|
37
|
+
|
38
|
+
desc "Update docs and upload to rubyforge.org"
|
39
|
+
task :doc_rforge do
|
40
|
+
sh %{scp -r doc/rdoc/* ciconia@rubyforge.org:/var/www/gforge-projects/babel}
|
41
|
+
end
|
42
|
+
|
43
|
+
##############################################################################
|
44
|
+
# Gem packaging
|
45
|
+
##############################################################################
|
46
|
+
desc "Packages up Babel."
|
47
|
+
task :default => [:package]
|
48
|
+
task :package => [:clean]
|
49
|
+
|
50
|
+
spec = Gem::Specification.new do |s|
|
51
|
+
s.name = NAME
|
52
|
+
s.rubyforge_project = 'babel'
|
53
|
+
s.version = VERS
|
54
|
+
s.platform = Gem::Platform::RUBY
|
55
|
+
s.has_rdoc = true
|
56
|
+
s.extra_rdoc_files = ["README", "CHANGELOG", "COPYING"]
|
57
|
+
s.rdoc_options += RDOC_OPTS +
|
58
|
+
["--exclude", "^(examples|extras)\/", "--exclude", "lib/rbabel.rb"]
|
59
|
+
s.summary = "Automatic language translation"
|
60
|
+
s.description = s.summary
|
61
|
+
s.author = "Sharon Rosner"
|
62
|
+
s.email = "ciconia@gmail.com"
|
63
|
+
s.homepage = "http://babel.rubyforge.org"
|
64
|
+
s.executables = ["rbabel"]
|
65
|
+
s.required_ruby_version = ">= 1.8.4"
|
66
|
+
|
67
|
+
s.add_dependency("hpricot")
|
68
|
+
|
69
|
+
s.files = %w(COPYING README Rakefile) + Dir.glob("{bin,doc,spec,lib}/**/*")
|
70
|
+
|
71
|
+
s.require_path = "lib"
|
72
|
+
s.bindir = "bin"
|
73
|
+
end
|
74
|
+
|
75
|
+
Rake::GemPackageTask.new(spec) do |p|
|
76
|
+
p.need_tar = true
|
77
|
+
p.gem_spec = spec
|
78
|
+
end
|
79
|
+
|
80
|
+
##############################################################################
|
81
|
+
# installation & removal
|
82
|
+
##############################################################################
|
83
|
+
task :install do
|
84
|
+
sh %{rake package}
|
85
|
+
sh %{sudo gem install pkg/#{NAME}-#{VERS}}
|
86
|
+
end
|
87
|
+
|
88
|
+
task :install_no_docs do
|
89
|
+
sh %{rake package}
|
90
|
+
sh %{sudo gem install pkg/#{NAME}-#{VERS} --no-rdoc --no-ri}
|
91
|
+
end
|
92
|
+
|
93
|
+
task :uninstall => [:clean] do
|
94
|
+
sh %{sudo gem uninstall #{NAME}}
|
95
|
+
end
|
96
|
+
|
97
|
+
task :tag do
|
98
|
+
cwd = FileUtils.pwd
|
99
|
+
sh %{cd .. && svn copy #{cwd} tags/babel-#{VERS} && svn commit -m "babel-#{VERS} tag." tags}
|
100
|
+
end
|
101
|
+
|
102
|
+
##############################################################################
|
103
|
+
# gem and rdoc release
|
104
|
+
##############################################################################
|
105
|
+
task :release => [:package] do
|
106
|
+
sh %{rubyforge login}
|
107
|
+
sh %{rubyforge add_release babel #{NAME} #{VERS} pkg/#{NAME}-#{VERS}.tgz}
|
108
|
+
sh %{rubyforge add_file babel #{NAME} #{VERS} pkg/#{NAME}-#{VERS}.gem}
|
109
|
+
end
|
110
|
+
|
111
|
+
##############################################################################
|
112
|
+
# specs
|
113
|
+
##############################################################################
|
114
|
+
require "spec/rake/spectask"
|
115
|
+
|
116
|
+
desc "Run specs with coverage"
|
117
|
+
Spec::Rake::SpecTask.new("spec") do |t|
|
118
|
+
t.spec_files = FileList["spec/*_spec.rb"]
|
119
|
+
t.spec_opts = File.read("spec/spec.opts").split("\n")
|
120
|
+
t.rcov_opts = File.read("spec/rcov.opts").split("\n")
|
121
|
+
t.rcov = true
|
122
|
+
end
|
123
|
+
|
124
|
+
desc "Run specs without coverage"
|
125
|
+
Spec::Rake::SpecTask.new("spec_no_cov") do |t|
|
126
|
+
t.spec_files = FileList["spec/*_spec.rb"]
|
127
|
+
t.spec_opts = File.read("spec/spec.opts").split("\n")
|
128
|
+
end
|
129
|
+
|
data/bin/rbabel
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rbabel'
|
5
|
+
|
6
|
+
usage = <<END
|
7
|
+
Usage: rbabel <lang>:<lang>
|
8
|
+
Babel: automatic language translation
|
9
|
+
|
10
|
+
Examples:
|
11
|
+
cat english.txt | rbabel en:fr > french.txt
|
12
|
+
|
13
|
+
For more information see http://babel.rubyforge.org
|
14
|
+
END
|
15
|
+
|
16
|
+
langpair = ARGV.shift
|
17
|
+
|
18
|
+
if langpair.nil? || langpair.empty? || langpair !~ /(.+):(.+)/
|
19
|
+
puts usage
|
20
|
+
exit
|
21
|
+
end
|
22
|
+
|
23
|
+
STDOUT << STDIN.read.translate($1, $2)
|
data/lib/rbabel.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# String extensions.
|
2
|
+
class String
|
3
|
+
# Translates self from the source language into one or more destination
|
4
|
+
# languages.
|
5
|
+
#
|
6
|
+
# "hello".translate(:en, :fr) #=> "Bonjour"
|
7
|
+
#
|
8
|
+
# "hello".translate(:en, :nl, :it) #=> ["Hallo", "Ciao"]
|
9
|
+
def translate(source_lang, *dest_lang)
|
10
|
+
Babel.translate(self, source_lang, *dest_lang)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'hpricot'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
module Babel
|
5
|
+
# Encodes a string into a URI-escaped string.
|
6
|
+
def self.uri_escape(text)
|
7
|
+
text.gsub(/([^ a-zA-Z0-9_.-]+)/n) {'%'+$1.unpack('H2'*$1.size).
|
8
|
+
join('%').upcase}.tr(' ', '+')
|
9
|
+
end
|
10
|
+
|
11
|
+
# Fetches the translation page from Google.
|
12
|
+
def self.fetch_translation(text, source_lang, dest_lang)
|
13
|
+
res = Net::HTTP.start('google.com', 80) do |h|
|
14
|
+
h.get("/translate_t?langpair=%s|%s&text=%s" % [
|
15
|
+
source_lang,
|
16
|
+
dest_lang,
|
17
|
+
uri_escape(text)
|
18
|
+
])
|
19
|
+
end
|
20
|
+
res.body
|
21
|
+
end
|
22
|
+
|
23
|
+
# Translates the given text from the given source language into one or
|
24
|
+
# more destination languages.
|
25
|
+
def self.translate(text, source_lang, *dest_lang)
|
26
|
+
translations = dest_lang.map do |l|
|
27
|
+
doc = Hpricot(fetch_translation(text, source_lang, l))
|
28
|
+
(doc/"#result_box").text
|
29
|
+
end
|
30
|
+
(dest_lang.size == 1) ? translations.first : translations
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe "String#translate" do
|
4
|
+
it "should translate between languages" do
|
5
|
+
"five days".translate(:en, :fr).should == "Cinq jours"
|
6
|
+
"cinq jours".translate(:fr, :en).should == "Five days"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should translate the text to multiple languages if given" do
|
10
|
+
"hello".translate(:en, :fr, :nl, :it).should == ["Bonjour", "Hallo", "Ciao"]
|
11
|
+
end
|
12
|
+
end
|
data/spec/rcov.opts
ADDED
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "../lib/rbabel")
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rbabel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.2"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sharon Rosner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-01-14 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hpricot
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
24
|
+
description: Automatic language translation
|
25
|
+
email: ciconia@gmail.com
|
26
|
+
executables:
|
27
|
+
- rbabel
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
- CHANGELOG
|
33
|
+
- COPYING
|
34
|
+
files:
|
35
|
+
- COPYING
|
36
|
+
- README
|
37
|
+
- Rakefile
|
38
|
+
- bin/rbabel
|
39
|
+
- spec/core_ext_spec.rb
|
40
|
+
- spec/rcov.opts
|
41
|
+
- spec/spec.opts
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
- lib/rbabel
|
44
|
+
- lib/rbabel/core_ext.rb
|
45
|
+
- lib/rbabel/translation.rb
|
46
|
+
- lib/rbabel.rb
|
47
|
+
- CHANGELOG
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://babel.rubyforge.org
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --quiet
|
53
|
+
- --title
|
54
|
+
- "Babel: automatic translation"
|
55
|
+
- --opname
|
56
|
+
- index.html
|
57
|
+
- --line-numbers
|
58
|
+
- --main
|
59
|
+
- README
|
60
|
+
- --inline-source
|
61
|
+
- --exclude
|
62
|
+
- ^(examples|extras)/
|
63
|
+
- --exclude
|
64
|
+
- lib/rbabel.rb
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 1.8.4
|
72
|
+
version:
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
version:
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project: babel
|
82
|
+
rubygems_version: 1.0.1
|
83
|
+
signing_key:
|
84
|
+
specification_version: 2
|
85
|
+
summary: Automatic language translation
|
86
|
+
test_files: []
|
87
|
+
|