easy_translate 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/easy_translate.rb +45 -0
- data/lib/easy_translate/translator.rb +119 -0
- data/lib/easy_translate/version.rb +5 -0
- metadata +75 -0
@@ -0,0 +1,45 @@
|
|
1
|
+
module EasyTranslate
|
2
|
+
|
3
|
+
require 'easy_translate/translator'
|
4
|
+
|
5
|
+
require 'uri'
|
6
|
+
require 'net/http'
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'json'
|
10
|
+
|
11
|
+
API_URL = 'ajax.googleapis.com'
|
12
|
+
API_TRANSLATE_PATH = '/ajax/services/language/translate'
|
13
|
+
API_DETECT_PATH = '/ajax/services/language/detect'
|
14
|
+
API_VERSION = '1.0'
|
15
|
+
|
16
|
+
LANGUAGES = { 'tr' => 'turkish', 'sv' => 'swedish', 'km' => 'khmer',
|
17
|
+
'mk' => 'macedonian', 'chr' => 'cherokee', 'si' => 'sinhalese',
|
18
|
+
'zh-cn' => 'chinese_simplified', 'fi' => 'finnish', 'lo' => 'laothian',
|
19
|
+
'da' => 'danish', 'th' => 'thai', 'sk' => 'slovak', 'sq' => 'albanian',
|
20
|
+
'ms' => 'malay', 'no' => 'norwegian', '' => 'unknown', 'cy' => 'welsh',
|
21
|
+
'be' => 'belarusian', 'am' => 'amharic', 'ca' => 'catalan',
|
22
|
+
'zh' => 'chinese', 'id' => 'indonesian', 'ta' => 'tamil',
|
23
|
+
'fa' => 'persian', 'zh-tw' => 'chinese_traditional', 'uz' => 'uzbek',
|
24
|
+
'sw' => 'swahili', 'ja' => 'japanese', 'kk' => 'kazakh',
|
25
|
+
'gl' => 'galician', 'ps' => 'pashto', 'lv' => 'latvian', 'te' => 'telugu',
|
26
|
+
'sa' => 'sanskrit', 'kn' => 'kannada', 'af' => 'afrikaans',
|
27
|
+
'ka' => 'georgian', 'it' => 'italian', 'mr' => 'marathi', 'ug' => 'uighur',
|
28
|
+
'ro' => 'romanian', 'nl' => 'dutch', 'gu' => 'gujarati',
|
29
|
+
'eo' => 'esperanto', 'uk' => 'ukrainian', 'ru' => 'russian',
|
30
|
+
'ky' => 'kyrgyz', 'ga' => 'irish', 'tl' => 'tagalog', 'sr' => 'serbian',
|
31
|
+
'pa' => 'punjabi', 'mt' => 'maltese', 'ne' => 'nepali', 'or' => 'oriya',
|
32
|
+
'eu' => 'basque', 'dv' => 'dhivehi', 'sl' => 'slovenian',
|
33
|
+
'pl' => 'polish', 'el' => 'greek', 'ku' => 'kurdish', 'de' => 'german',
|
34
|
+
'iw' => 'hebrew', 'sd' => 'sindhi', 'et' => 'estonian', 'gn' => 'guarani',
|
35
|
+
'is' => 'icelandic', 'bn' => 'bengali', 'tl' => 'filipino',
|
36
|
+
'bo' => 'tibetan', 'es' => 'spanish', 'fr' => 'french',
|
37
|
+
'hy' => 'armenian', 'bg' => 'bulgarian', 'lt' => 'lithuanian',
|
38
|
+
'mn' => 'mongolian', 'az' => 'azerbaijani', 'yi' => 'yiddish',
|
39
|
+
'ur' => 'urdu', 'en' => 'english', 'pt-pt' => 'portuguese',
|
40
|
+
'vi' => 'vietnamese', 'ml' => 'malayalam', 'ar' => 'arabic',
|
41
|
+
'bh' => 'bihari', 'iu' => 'inuktitut', 'my' => 'burmese',
|
42
|
+
'hu' => 'hungarian', 'ko' => 'korean', 'tg' => 'tajik', 'cs' => 'czech',
|
43
|
+
'hi' => 'hindi', 'hr' => 'croatian' }
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
module EasyTranslate
|
2
|
+
|
3
|
+
attr_writer :api_key
|
4
|
+
|
5
|
+
# Detect the language of a given string of text.
|
6
|
+
# Optional parameters:
|
7
|
+
# :key - API key for google language (defaults to nil)
|
8
|
+
# :host_language - language (defaults to 'en')
|
9
|
+
# :user_ip - the ip of the end user - will help not be mistaken for abuse
|
10
|
+
# Required Parameters:
|
11
|
+
# the text to detect the language for
|
12
|
+
# Returns:
|
13
|
+
# the string language code (ie: 'en') of the language
|
14
|
+
# Note:
|
15
|
+
# This API (since it focuses more on tranlations/ease, does not return
|
16
|
+
# confidence ratings)
|
17
|
+
def self.detect(text, options = {})
|
18
|
+
json = api_call "#{API_DETECT_PATH}?#{base_params(text, options)}&q=#{URI.escape text}"
|
19
|
+
json['responseData']['language']
|
20
|
+
end
|
21
|
+
|
22
|
+
# Translate a string using Google Translate
|
23
|
+
# Optional parameters:
|
24
|
+
# :key - API key for google language (defaults to nil)
|
25
|
+
# :host_language - language (defaults to 'en') (symbol/string)
|
26
|
+
# :user_ip - the ip of the end user - will help not be mistaken for abuse
|
27
|
+
# :from - the language to translate from (symbol/string)
|
28
|
+
# :html - boolean indicating whether the text you're translating is HTML or plaintext
|
29
|
+
# Required Parameters:
|
30
|
+
# the text to translate
|
31
|
+
# :to - the language to translate to (symbol/string)
|
32
|
+
# Returns:
|
33
|
+
# the translated string
|
34
|
+
def self.translate(text, options)
|
35
|
+
# translate params if necessary
|
36
|
+
to_lang = get_language(options[:to])
|
37
|
+
from_lang = get_language(options[:from])
|
38
|
+
# make the call
|
39
|
+
path = "#{API_TRANSLATE_PATH}?#{base_params(text, options)}"
|
40
|
+
path << '&format=' << (options[:html] ? 'html' : 'text')
|
41
|
+
path << '&q=' << URI.escape(text)
|
42
|
+
path << '&langpair=' << URI.escape("#{from_lang}|#{to_lang}")
|
43
|
+
# get the proper response and return
|
44
|
+
json = api_call path
|
45
|
+
json['responseData']['translatedText']
|
46
|
+
end
|
47
|
+
|
48
|
+
# Translate batches of string using Google Translate
|
49
|
+
# Optional parameters:
|
50
|
+
# :key - API key for google language (defaults to nil)
|
51
|
+
# :host_language - language (defaults to 'en') (symbol/string)
|
52
|
+
# :user_ip - the ip of the end user - will help not be mistaken for abuse
|
53
|
+
# :from - the language to translate from (symbol/string)
|
54
|
+
# :html - boolean indicating whether the text you're translating is HTML or plaintext
|
55
|
+
# Required Parameters:
|
56
|
+
# the text(s) to translate (string/array)
|
57
|
+
# :to - the language(s) to translate to (symbol/string/array)
|
58
|
+
# Returns:
|
59
|
+
# an array of the translated strings
|
60
|
+
def self.translate_batch(text, options)
|
61
|
+
# translate params if necessary
|
62
|
+
to_lang = (options[:to].class == Array) ? options[:to].map { |to| get_language(to) } : get_language(options[:to])
|
63
|
+
from_lang = get_language(options[:from])
|
64
|
+
# make the call
|
65
|
+
path = "#{API_TRANSLATE_PATH}?#{base_params(text, options)}"
|
66
|
+
path << '&format=' << (options[:html] ? 'html' : 'text')
|
67
|
+
# for each to language, put all of the q's
|
68
|
+
to_lang.each do |tol|
|
69
|
+
escaped_lang_pair = URI.escape "#{from_lang}|#{tol}"
|
70
|
+
text.each do |t|
|
71
|
+
path << "&q=#{URI.escape t}"
|
72
|
+
path << "&langpair=#{escaped_lang_pair}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
# get the proper response and return
|
76
|
+
json = api_call path
|
77
|
+
if json['responseData'].class == Array
|
78
|
+
translations = json['responseData'].map { |j| j['responseData']['translatedText'] }
|
79
|
+
else
|
80
|
+
[json['responseData']['translatedText']]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# an exception indicating something went wrong in EasyTranslate
|
85
|
+
class EasyTranslateException < Exception
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
# take in the base parameters for the google translate api
|
91
|
+
def self.base_params(text, options)
|
92
|
+
raise ArgumentError.new('multiple :from not allowed') if options[:from] && options[:from].class == Array
|
93
|
+
raise ArgumentError.new('no string given') if text.empty?
|
94
|
+
key = options[:key] || @api_key || nil
|
95
|
+
params = "v=#{API_VERSION}"
|
96
|
+
params << '&userip=' << options[:user_ip] if options.has_key?(:user_ip)
|
97
|
+
params << '&hl=' << get_language(options[:host_language]) if options.has_key?(:host_language)
|
98
|
+
params << '&key=' << key if key
|
99
|
+
# key is standard - but left to individual methods
|
100
|
+
params
|
101
|
+
end
|
102
|
+
|
103
|
+
# make a call to the api and throw an error on non-200 response
|
104
|
+
# TODO - expand error handling
|
105
|
+
def self.api_call(path)
|
106
|
+
response = Net::HTTP.get(API_URL, path)
|
107
|
+
json = JSON.parse(response)
|
108
|
+
raise EasyTranslateException.new(json['responseDetails']) unless json['responseStatus'] == 200
|
109
|
+
json
|
110
|
+
end
|
111
|
+
|
112
|
+
# a function used to get the lang code of any input.
|
113
|
+
# can take -- :english, 'english', :en, 'en'
|
114
|
+
def self.get_language(lang)
|
115
|
+
lang = lang.to_s
|
116
|
+
LANGUAGES.include?(lang) ? lang : LANGUAGES.index(lang)
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_translate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- John Crepezzi
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-28 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
description: easy_translate is a wrapper for the google translate API that makes sense programatically, and implements API keys
|
33
|
+
email: john@crepezzi.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files: []
|
39
|
+
|
40
|
+
files:
|
41
|
+
- lib/easy_translate/translator.rb
|
42
|
+
- lib/easy_translate/version.rb
|
43
|
+
- lib/easy_translate.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/seejohnrun/easy_translate/
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project: easy-translate
|
70
|
+
rubygems_version: 1.3.6
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Google Translate API Wrapper for Ruby
|
74
|
+
test_files: []
|
75
|
+
|