goobalize3 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,6 +5,8 @@ Goobalize3 (Google + Globalize3) is useful to auto translate the attributes of y
5
5
  If you have a model with some attributes translated with {Globalize3}[https://github.com/svenfuchs/globalize3] you
6
6
  can in easy way auto translate them via {Google Translate}[http://translate.google.com].
7
7
 
8
+ With version 0.2.0 Goobalize use Bing to translate because Google Translate service is no more free.
9
+
8
10
  == Requirements
9
11
 
10
12
  * Globalize3
@@ -14,11 +16,12 @@ can in easy way auto translate them via {Google Translate}[http://translate.goog
14
16
  * As gem: put <tt>gem 'goobalize3'</tt> in your Gemfile
15
17
  * As plugin: run <tt>rails plugin install git@github.com:pioz/goobalize3.git</tt>
16
18
 
17
- Now create <tt>google_translate.yml</tt> file in your rails config folder and put the follow line:
19
+ Now create <tt>goobalize.yml</tt> file in your rails config folder and put the follow line:
18
20
 
19
- api : YOUR_GOOGLE_TRANSLATE_API_KEY
21
+ bing_app_id: YOUR_BING_APP_ID
22
+ google_api : YOUR_GOOGLE_TRANSLATE_API_KEY
20
23
 
21
- You can get api key at {Google apis console page}[https://code.google.com/apis/console/].
24
+ You can get Google api key at {Google apis console page}[https://code.google.com/apis/console/].
22
25
 
23
26
  == Usage
24
27
 
@@ -32,6 +35,11 @@ You can set also the target locales
32
35
 
33
36
  @post.translate(:it, :en, :de)
34
37
 
38
+ The default service is *Bing*. To use Google Translate set:
39
+
40
+ GOOBALIZE_SERVICE = :google
41
+
42
+
35
43
  == Questions or problems?
36
44
 
37
45
  If you have any issues with Goobalize3 please add an {issue on GitHub}[https://github.com/pioz/goobalize3/issues]
data/Rakefile CHANGED
@@ -1,9 +1,2 @@
1
1
  require 'bundler'
2
- Bundler::GemHelper.install_tasks
3
-
4
- require 'rake/rdoctask'
5
- Rake::RDocTask.new do |rdoc|
6
- rdoc.rdoc_dir = 'rdoc'
7
- rdoc.rdoc_files.include('README*')
8
- rdoc.main = 'README.rdoc'
9
- end
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,46 @@
1
+ module BingTranslate
2
+ require 'net/http'
3
+ require 'nokogiri'
4
+
5
+ URI = URI.parse('http://api.microsofttranslator.com/V2/Http.svc/Translate')
6
+ QUERY_LIMIT = 2000000
7
+ LOCALES_MAP = {
8
+ :cn => :'zh-CNT'
9
+ }
10
+
11
+ def self.get_api
12
+ config_file = "#{Rails.root}/config/goobalize.yml"
13
+ #config_file = File.expand_path('../bing_translate.yml', __FILE__)
14
+ if File.exists?(config_file)
15
+ @@bing_translate_api = YAML.load_file(config_file)['bing_app_id']
16
+ raise GoobalizeTranslateError.new("No APP ID found in '#{config_file}'") if @@bing_translate_api.blank?
17
+ return @@bing_translate_api
18
+ else
19
+ raise GoobalizeTranslateException.new("No config file found '#{config_file}'")
20
+ end
21
+ return nil
22
+ end
23
+
24
+ def self.map(locale)
25
+ mapped = LOCALES_MAP[locale]
26
+ mapped.nil? ? locale : mapped
27
+ end
28
+
29
+ def self.perform(params)
30
+ @@bing_translate_api ||= get_api
31
+ query = {}
32
+ query[:text] = CGI::escape(params[:q].to_s[0..QUERY_LIMIT])
33
+ query[:from] = map params[:source]
34
+ query[:to] = map params[:target]
35
+ query.merge!(:appId => @@bing_translate_api, :contentType => 'text/html', :category => 'general')
36
+ data = []
37
+ query.each_pair { |k,v| data << "#{k}=#{v}" }
38
+ query_string = data.join('&')
39
+ result = Net::HTTP.new(URI.host, URI.port).get("#{URI.path}?#{query_string}")
40
+ begin
41
+ Nokogiri.parse(result.body).xpath('//xmlns:string').first.content
42
+ rescue
43
+ raise GoobalizeTranslateError.new('Translation failed')
44
+ end
45
+ end
46
+ end
@@ -1,5 +1,7 @@
1
1
  module Goobalize3
2
+ require File.expand_path('../goobalize_translate_error', __FILE__)
2
3
  require File.expand_path('../google_translate', __FILE__)
4
+ require File.expand_path( '../bing_translate', __FILE__)
3
5
 
4
6
  def translate(*args)
5
7
  if self.class.translates?
@@ -11,8 +13,12 @@ module Goobalize3
11
13
  autotranslated_attributes = {}
12
14
  self.class.translated_attribute_names.each do |attr_name|
13
15
  begin
14
- autotranslated_attributes[attr_name] = GoogleTranslate.perform(:source => cur_locale, :target => locale, :q => self.send(attr_name, cur_locale))
15
- rescue GoogleTranslateException => e
16
+ if defined?(GOOBALIZE_SERVICE) && GOOBALIZE_SERVICE.to_sym == :google
17
+ autotranslated_attributes[attr_name] = GoogleTranslate.perform(:source => cur_locale, :target => locale, :q => self.send(attr_name, cur_locale))
18
+ else
19
+ autotranslated_attributes[attr_name] = BingTranslate.perform(:source => cur_locale, :target => locale, :q => self.send(attr_name, cur_locale))
20
+ end
21
+ rescue GoobalizeTranslateError => e
16
22
  Rails.logger.error(e.to_s)
17
23
  end
18
24
  end
@@ -1,3 +1,3 @@
1
1
  module Goobalize3
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1 @@
1
+ class GoobalizeTranslateError < StandardError; end
@@ -10,14 +10,14 @@ module GoogleTranslate
10
10
  }
11
11
 
12
12
  def self.get_api
13
- config_file = "#{Rails.root}/config/google_translate.yml"
13
+ config_file = "#{Rails.root}/config/goobalize.yml"
14
14
  #config_file = File.expand_path('../google_translate.yml', __FILE__)
15
15
  if File.exists?(config_file)
16
- @@goole_translate_api = YAML.load_file(config_file)['api']
17
- raise GoogleTranslateException.new("No API key found in '#{config_file}'") if @@goole_translate_api.blank?
16
+ @@goole_translate_api = YAML.load_file(config_file)['google_api']
17
+ raise GoobalizeTranslateError.new("No API key found in '#{config_file}'") if @@goole_translate_api.blank?
18
18
  return @@goole_translate_api
19
19
  else
20
- raise GoogleTranslateException.new("No config file found '#{config_file}'")
20
+ raise GoobalizeTranslateError.new("No config file found '#{config_file}'")
21
21
  end
22
22
  return nil
23
23
  end
@@ -40,15 +40,26 @@ module GoogleTranslate
40
40
  http.use_ssl = true
41
41
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
42
42
  response, data = http.post(SERVICE, query_string, 'X-HTTP-Method-Override' => 'GET')
43
- json = JSON.parse(data)
44
- if json['data'] && json['data']['translations'] && json['data']['translations'].first['translatedText']
45
- return CGI::unescapeHTML(json['data']['translations'].first['translatedText'])
46
- elsif json['error'] && json['error']['errors'] && json['error']['errors'].first['message']
47
- raise GoogleTranslateException.new(json['error']['errors'].first['message'])
43
+ if response.code == 200
44
+ json = JSON.parse(data)
45
+ if json['data'] && json['data']['translations'] && json['data']['translations'].first['translatedText']
46
+ return CGI::unescapeHTML(json['data']['translations'].first['translatedText'])
47
+ else
48
+ raise GoobalizeTranslateError.new(error(json))
49
+ end
48
50
  else
49
- raise GoogleTranslateException.new('Unknown error')
51
+ json = JSON.parse(response.body)
52
+ raise GoobalizeTranslateError.new(error(json))
50
53
  end
51
54
  end
52
- end
53
55
 
54
- class GoogleTranslateException < StandardError; end
56
+ private
57
+
58
+ def error(json)
59
+ if json['error'] && json['error']['errors'] && json['error']['errors'].first['message']
60
+ return json['error']['errors'].first['message']
61
+ else
62
+ return 'Unknown error'
63
+ end
64
+ end
65
+ end
metadata CHANGED
@@ -1,89 +1,67 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: goobalize3
3
- version: !ruby/object:Gem::Version
4
- hash: 25
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Enrico Pilotto
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-03-02 00:00:00 +01:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-01-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: globalize3
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70138247307220 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
33
22
  type: :runtime
34
- version_requirements: *id001
23
+ prerelease: false
24
+ version_requirements: *70138247307220
35
25
  description: Auto translate with Google Translate the Globalize3 attributes
36
- email:
26
+ email:
37
27
  - enrico@megiston.it
38
28
  executables: []
39
-
40
29
  extensions: []
41
-
42
30
  extra_rdoc_files: []
43
-
44
- files:
31
+ files:
45
32
  - .gitignore
46
33
  - Gemfile
47
34
  - README.rdoc
48
35
  - Rakefile
49
36
  - goobalize3.gemspec
50
37
  - init.rb
38
+ - lib/bing_translate.rb
51
39
  - lib/goobalize3.rb
52
40
  - lib/goobalize3/version.rb
41
+ - lib/goobalize_translate_error.rb
53
42
  - lib/google_translate.rb
54
- has_rdoc: true
55
- homepage: ""
43
+ homepage: ''
56
44
  licenses: []
57
-
58
45
  post_install_message:
59
46
  rdoc_options: []
60
-
61
- require_paths:
47
+ require_paths:
62
48
  - lib
63
- required_ruby_version: !ruby/object:Gem::Requirement
49
+ required_ruby_version: !ruby/object:Gem::Requirement
64
50
  none: false
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- hash: 3
69
- segments:
70
- - 0
71
- version: "0"
72
- required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
56
  none: false
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- hash: 3
78
- segments:
79
- - 0
80
- version: "0"
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
81
61
  requirements: []
82
-
83
62
  rubyforge_project: goobalize3
84
- rubygems_version: 1.4.2
63
+ rubygems_version: 1.8.10
85
64
  signing_key:
86
65
  specification_version: 3
87
66
  summary: Auto translate with Google Translate the Globalize3 attributes
88
67
  test_files: []
89
-