language-translator 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Anil Yanduri
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ MIT-LICENSE
2
+ Manifest
3
+ README.rdoc
4
+ Rakefile
5
+ language-translator.gemspec
6
+ lib/translator.rb
7
+ test/test_helper.rb
8
+ test/translator_test.rb
@@ -0,0 +1,23 @@
1
+ = Translator
2
+
3
+ == Description
4
+ A gem to translate from one language to another using google api(Unofficial).
5
+
6
+ == Installation
7
+
8
+ gem install language-translator
9
+
10
+ == Usage
11
+
12
+ require 'rubygems'
13
+ require 'language-translator'
14
+
15
+ #To translate 'Hello World' to Hindi
16
+ tr = Translator.new()
17
+ tr.translate( 'Hello, World', 'hi')
18
+ =>"नमस्ते, दुनिया"
19
+
20
+ #To translate "नमस्ते, दुनिया" to English
21
+ tr.translate('नमस्ते, दुनिया', 'en','hi')
22
+ =>"Hello World"
23
+
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('language-translator', '0.1.1') do |p|
6
+ p.description = "A gem to translate from one language to another using google api(Unofficial)."
7
+ p.url = "http://github.com/anilyanduri/language-translator"
8
+ p.author = "Anil Yanduri"
9
+ p.email = "anilkumaryln@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{language-translator}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Anil Yanduri"]
9
+ s.date = %q{2010-06-03}
10
+ s.description = %q{A gem to translate from one language to another using google api(Unofficial).}
11
+ s.email = %q{anilkumaryln@gmail.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/translator.rb"]
13
+ s.files = ["MIT-LICENSE", "Manifest", "README.rdoc", "Rakefile", "language-translator.gemspec", "lib/translator.rb", "test/test_helper.rb", "test/translator_test.rb"]
14
+ s.homepage = %q{http://github.com/anilyanduri/language-translator}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Language-translator", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{language-translator}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{A gem to translate from one language to another using google api(Unofficial).}
20
+ s.test_files = ["test/translator_test.rb", "test/test_helper.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
@@ -0,0 +1,46 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ class UnSupportedLanguage < RuntimeError
5
+ attr :msg
6
+ def initialize(message='')
7
+ @msg = "Language pair not supported yet."
8
+ end
9
+ end
10
+
11
+ class Translator
12
+
13
+ Site_Url = 'ajax.googleapis.com'
14
+ Request_Uri = "/ajax/services/language/translate"
15
+ SUPPORTED_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']
16
+
17
+ def translate( text, to, from='en' )
18
+
19
+ begin
20
+ raise UnSupportedLanguage if !SUPPORTED_LANG_CODES.include?(to) || !SUPPORTED_LANG_CODES.include?(from)
21
+
22
+ http = Net::HTTP.new(Site_Url, 80)
23
+
24
+ request = Net::HTTP::Post.new(Request_Uri)
25
+
26
+ request.set_form_data({'v'=>1.0, 'langpair'=>"#{from}|#{to}", 'q'=>"#{text}"})
27
+
28
+ response = http.request(request)
29
+
30
+ json_response_body = JSON.parse( response.body )
31
+
32
+ if json_response_body['responseStatus'] == 200
33
+ json_response_body['responseData']['translatedText']
34
+ else
35
+ puts json_response_body['responseDetails']
36
+ raise StandardError, response['responseDetails']
37
+ end
38
+ rescue UnSupportedLanguage
39
+ raise UnSupportedLanguage.new
40
+ rescue => err_msg
41
+ puts #{err_msg}
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,6 @@
1
+ require 'test/unit'
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+ require 'rubygems'
4
+ require 'translator'
5
+ require 'mocha'
6
+
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ class TranslatorTest < Test::Unit::TestCase
3
+
4
+ def test_translate_from_english_to_other
5
+ tr = Translator.new()
6
+ assert_equal("नमस्ते दुनिया", tr.translate("Hello world", "hi", "en"))
7
+ assert_equal("Bonjour tout le monde", tr.translate("Hello world", "fr", "en"))
8
+ assert_equal("Hallo Welt", tr.translate("Hello world", "de", "en"))
9
+ assert_equal("Ciao a tutti", tr.translate("Hello world", "it", "en"))
10
+ assert_equal("こんにちは、世界", tr.translate("Hello world", "ja", "en"))
11
+ assert_equal("Dia duit domhan", tr.translate("Hello world", "ga", "en"))
12
+ assert_equal("Olá mundo", tr.translate("Hello world", "pt-PT", "en"))
13
+ assert_equal("Привет мир", tr.translate("Hello world", "ru", "en"))
14
+ assert_equal("¡Hola, mundo", tr.translate("Hello world", "es", "en"))
15
+ assert_equal("مرحبا العالم", tr.translate("Hello world", "ar", "en"))
16
+ end
17
+
18
+ def test_translate_from_other_to_english
19
+ tr = Translator.new()
20
+ assert_equal("Hello World", tr.translate("नमस्ते दुनिया", "en","hi"))
21
+ assert_equal("Hello world", tr.translate("世界您好", "en", "zh-CN"))
22
+ assert_equal("Hello world", tr.translate("Dia duit domhan", "en", "ga"))
23
+ assert_equal("Hello world", tr.translate("こんにちは、世界", "en", "ja"))
24
+ assert_equal("Hello world", tr.translate("안녕하세요 세상", "en", "ko"))
25
+ assert_equal("Hello world", tr.translate("Olá mundo", "en", "pt-PT"))
26
+ assert_equal("Hello world", tr.translate("Привет мир", "en", "ru"))
27
+ assert_equal("Hello world", tr.translate("¡Hola, mundo", "en", "es"))
28
+ assert_equal("Hello world", tr.translate("مرحبا العالم", "en", "ar"))
29
+ end
30
+
31
+ def test_unsupported_translate
32
+ assert_raise UnSupportedLanguage do
33
+ tr = Translator.new()
34
+ tr.translate("你好世界", 'zh', 'hz')
35
+ end
36
+ end
37
+
38
+ end
39
+
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: language-translator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Anil Yanduri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-06-03 00:00:00 +05:30
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A gem to translate from one language to another using google api(Unofficial).
17
+ email: anilkumaryln@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - lib/translator.rb
25
+ files:
26
+ - MIT-LICENSE
27
+ - Manifest
28
+ - README.rdoc
29
+ - Rakefile
30
+ - language-translator.gemspec
31
+ - lib/translator.rb
32
+ - test/test_helper.rb
33
+ - test/translator_test.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/anilyanduri/language-translator
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --line-numbers
41
+ - --inline-source
42
+ - --title
43
+ - Language-translator
44
+ - --main
45
+ - README.rdoc
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "1.2"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project: language-translator
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: A gem to translate from one language to another using google api(Unofficial).
67
+ test_files:
68
+ - test/translator_test.rb
69
+ - test/test_helper.rb