google-translator-cis 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +34 -0
- data/google-translator-cis.gemspec +13 -0
- data/lib/google_translate_cis.rb +44 -0
- data/spec/google_translate_cis_spec.rb +26 -0
- data/spec/spec_helper.rb +4 -0
- metadata +66 -0
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
google-translate-cis
|
2
|
+
====================
|
3
|
+
|
4
|
+
With the help of this gem you can easily convert one language text into another and it is a simple language converter which you can use in your application. :-)
|
5
|
+
|
6
|
+
INSTALLTION
|
7
|
+
|
8
|
+
First thing you need to do is the installation , you can follow the below mentioned steps to install the gem inside your rails application.
|
9
|
+
You need to add sudo if you are not using rvm(ruby version manager)
|
10
|
+
|
11
|
+
<pre>
|
12
|
+
gem install google-translate-cis
|
13
|
+
</pre>
|
14
|
+
|
15
|
+
|
16
|
+
Add this following line in your Gemfile and then run bundle install.
|
17
|
+
<pre>
|
18
|
+
gem 'google-translate-cis'
|
19
|
+
</pre>
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
USAGE
|
24
|
+
|
25
|
+
Once you have installed this gem you can do the following :-
|
26
|
+
|
27
|
+
|
28
|
+
<pre>
|
29
|
+
Translator.translate("Text goes here", "Language code into which you want to convert", "Language code from which language you are converting")
|
30
|
+
Translator.translate("Hello","ga","en")
|
31
|
+
Output: Dia duit
|
32
|
+
</pre>
|
33
|
+
|
34
|
+
We have used Google library to develop this Gem. So all credit goes to Google. :-)
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'google-translator-cis'
|
3
|
+
s.version = '0.0.2'
|
4
|
+
s.authors = ["Anuradha, CISROR Team"]
|
5
|
+
s.date = '2013-10-03'
|
6
|
+
s.summary = "Language Translator"
|
7
|
+
s.description = "This is a gem which provides the facility to translate languages from one to another"
|
8
|
+
s.add_dependency("rspec-rails")
|
9
|
+
s.files = Dir.glob("{lib,spec}/**/*")
|
10
|
+
s.files += %w(README.md)
|
11
|
+
s.files += %w(google-translator-cis.gemspec)
|
12
|
+
s.require_path = "lib"
|
13
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "net/http"
|
3
|
+
require "uri"
|
4
|
+
|
5
|
+
|
6
|
+
class UnSupportedLanguage < RuntimeError
|
7
|
+
def initialize(message='')
|
8
|
+
@msg = "Language not supported."
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Translator
|
13
|
+
LANG_CODES = ['sr','lt','lv','iw','cy','ga','id','de','zh-TW','es','sl','ko','it','eu','az','af','sk','pt-PT','no','gl','bg','ar','tr','fa','mk','el','da','yi','ur','uk','ro','ja','zh','sw','mt','ms','is','ka','en','hr','ca','th','ru','hy','vi','tl','sv','hu','hi','fi','sq','pl','fr','et','nl','cs','zh-CN','be']
|
14
|
+
|
15
|
+
def self.translate(text, to="en", from='en' )
|
16
|
+
begin
|
17
|
+
raise UnSupportedLanguage unless LANG_CODES.include?(to)
|
18
|
+
raise UnSupportedLanguage unless LANG_CODES.include?(from)
|
19
|
+
|
20
|
+
uri = URI.parse(URI.encode("http://translate.google.com/translate_a/t?client=t&text=#{text}&hl=en&sl=#{from.to_s.downcase}&tl=#{to.to_s.downcase}&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=3&tsel=3&sc=1".strip))
|
21
|
+
response = Net::HTTP.get_response(uri)
|
22
|
+
response_body = response.body.gsub(",,,",",0,0,").gsub(",,",",0,")
|
23
|
+
|
24
|
+
if response.is_a?(Net::HTTPSuccess)
|
25
|
+
eval(response_body)[0].map{ |translate| translate[0] }.join(" ")
|
26
|
+
else
|
27
|
+
puts "Error in Translation"
|
28
|
+
end
|
29
|
+
|
30
|
+
rescue UnSupportedLanguage
|
31
|
+
raise UnSupportedLanguage.new
|
32
|
+
rescue => err_msg
|
33
|
+
puts "#{err_msg}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe Translator do
|
4
|
+
describe ".translate" do
|
5
|
+
it "should convert English into German" do
|
6
|
+
text = Translator.translate("hello are you there","de","en")
|
7
|
+
text.should eq("hallo bist du da")
|
8
|
+
end
|
9
|
+
it "should convert Engish into Spanish" do
|
10
|
+
text = Translator.translate("Hello","ga","en")
|
11
|
+
text.should eq("Dia duit")
|
12
|
+
end
|
13
|
+
it "should convert English into French" do
|
14
|
+
text = Translator.translate("Hello","fr","en")
|
15
|
+
text.should eq("Bonjour")
|
16
|
+
end
|
17
|
+
it "should convert English into Dutch" do
|
18
|
+
text = Translator.translate("Hello Dutch","nl","en")
|
19
|
+
text.should eq("hello Nederlands")
|
20
|
+
end
|
21
|
+
it "should convert English into Turkish" do
|
22
|
+
text = Translator.translate("Hello","tr","en")
|
23
|
+
text.should eq("Merhaba")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google-translator-cis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Anuradha, CISROR Team
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec-rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: This is a gem which provides the facility to translate languages from
|
31
|
+
one to another
|
32
|
+
email:
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- lib/google_translate_cis.rb
|
38
|
+
- spec/google_translate_cis_spec.rb
|
39
|
+
- spec/spec_helper.rb
|
40
|
+
- README.md
|
41
|
+
- google-translator-cis.gemspec
|
42
|
+
homepage:
|
43
|
+
licenses: []
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.8.24
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Language Translator
|
66
|
+
test_files: []
|