google_translator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,34 @@
1
+ google_translator
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_translator
13
+ </pre>
14
+
15
+
16
+ Add this following line in your Gemfile and then run bundle install.
17
+ <pre>
18
+ gem 'google_translator'
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,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'google_translator'
3
+ s.version = '0.0.1'
4
+
5
+ s.authors = ["Anuradha Dindorkar"]
6
+ s.email = 'anuradha.d@cisinlabs.com'
7
+ s.date = '2013-09-27'
8
+ s.summary = "Language Translator"
9
+ s.description = "This is a gem which provides the facility to translate languages from one to another"
10
+ s.homepage = 'https://github.com/anuradha-d/google_translator'
11
+
12
+ s.add_dependency("rspec-rails")
13
+ s.files = Dir.glob("{lib,spec}/**/*")
14
+ s.files += %w(README.md)
15
+ s.files += %w(google_translator.gemspec)
16
+ s.require_path = "lib"
17
+ 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
@@ -0,0 +1,4 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+
3
+ require 'rails'
4
+ require 'google_translator'
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_translator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anuradha Dindorkar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-27 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: anuradha.d@cisinlabs.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/google_translator.rb
38
+ - spec/google_translator_spec.rb
39
+ - spec/spec_helper.rb
40
+ - README.md
41
+ - google_translator.gemspec
42
+ homepage: https://github.com/anuradha-d/google_translator
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: []