goobalize3 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ .DS_Store
5
+ rdoc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in goobalize3.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,41 @@
1
+ = Goobalize3
2
+
3
+ Goobalize3 (Google + Globalize3) is useful to auto translate the attributes of your activerecord models.
4
+
5
+ If you have a model with some attributes translated with {Globalize3}[https://github.com/svenfuchs/globalize3] you
6
+ can in easy way auto translate them via {Google Translate}[http://translate.google.com].
7
+
8
+ == Installation
9
+
10
+ As gem
11
+
12
+ gem install goobalize3
13
+
14
+ As plugin
15
+
16
+ rails plugin install git@github.com:pioz/goobalize3.git
17
+
18
+ == Requirements
19
+
20
+ * Globalize3
21
+
22
+ == Usage
23
+
24
+ You can auto translate all globalized attributes of an activerecord object with
25
+
26
+ @post.translate
27
+
28
+ this translate the attributes from current set locale (<tt>I18n.locale</tt>) to all available locales (<tt>I18n.available_locales - I18n.locale</tt>).
29
+
30
+ You can set also the target locales
31
+
32
+ @post.translate(:it, :en, :de)
33
+
34
+ == Questions or problems?
35
+
36
+ If you have any issues with Goobalize3 please add an {issue on GitHub}[https://github.com/pioz/goobalize3/issues]
37
+ or fork the project and send a pull request.
38
+
39
+ == Copyright
40
+
41
+ Copyright (c) 2010 Enrico Pilotto.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
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
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "goobalize3/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "goobalize3"
7
+ s.version = Goobalize3::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Enrico Pilotto"]
10
+ s.email = ["enrico@megiston.it"]
11
+ s.homepage = ""
12
+ s.summary = %q{Auto translate with Google Translate the Globalize3 attributes}
13
+ s.description = s.summary
14
+ s.add_dependency('globalize3')
15
+
16
+
17
+ s.rubyforge_project = "goobalize3"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'goobalize3'
data/lib/goobalize3.rb ADDED
@@ -0,0 +1,27 @@
1
+ module Goobalize3
2
+ require File.expand_path('../google_translate', __FILE__)
3
+
4
+ def translate(*args)
5
+ if self.class.translates?
6
+ cur_locale = I18n.locale
7
+ targets = I18n.available_locales - [I18n.locale]
8
+ args.map!{|l| l.to_s.downcase.to_sym}
9
+ targets = targets & args unless args.empty?
10
+ targets.each do |locale|
11
+ autotranslated_attributes = {}
12
+ self.class.translated_attribute_names.each do |attr_name|
13
+ begin
14
+ autotranslated_attributes[attr_name] = GoogleTranslate.perform(:source => cur_locale, :target => locale, :q => self.send(attr_name))
15
+ rescue GoogleTranslateException => e
16
+ end
17
+ end
18
+ I18n.locale = locale
19
+ self.update_attributes(autotranslated_attributes)
20
+ end
21
+ I18n.locale = cur_locale
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ Globalize::ActiveRecord::InstanceMethods.send(:include, Goobalize3)
@@ -0,0 +1,3 @@
1
+ module Goobalize3
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,42 @@
1
+ module GoogleTranslate
2
+ require 'net/http'
3
+ require 'json'
4
+
5
+ HOST = 'www.googleapis.com'
6
+ SERVICE = '/language/translate/v2'
7
+
8
+ def self.get_api
9
+ config_file = "#{Rails.root}/config/google_translate.yml"
10
+ #config_file = File.expand_path('../google_translate.yml', __FILE__)
11
+ if File.exists?(config_file)
12
+ @@goole_translate_api = YAML.load_file(config_file)['api']
13
+ raise GoogleTranslateException.new("No API key found in '#{config_file}'") if @@goole_translate_api.blank?
14
+ return @@goole_translate_api
15
+ else
16
+ raise GoogleTranslateException.new("No config file found '#{config_file}'")
17
+ end
18
+ return nil
19
+ end
20
+
21
+ def self.perform(params)
22
+ @@goole_translate_api ||= get_api
23
+ params[:q] = CGI::escape(params[:q])
24
+ params.merge!(:key => @@goole_translate_api)
25
+ data = []
26
+ params.each_pair { |k,v| data << "#{k}=#{v}" }
27
+ query_string = data.join('&')
28
+ http = Net::HTTP.new(HOST, 443)
29
+ http.use_ssl = true
30
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
31
+ response, data = http.get("#{SERVICE}?#{query_string}")
32
+ json = JSON.parse(data)
33
+ raise GoogleTranslateException.new(json['error']['errors'].first['message']) if json['error']
34
+ if json['data'] && json['data']['translations'] && json['data']['translations'].first['translatedText']
35
+ return CGI::unescapeHTML(json['data']['translations'].first['translatedText'])
36
+ else
37
+ raise GoogleTranslateException.new('Unknown error')
38
+ end
39
+ end
40
+ end
41
+
42
+ class GoogleTranslateException < StandardError; end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goobalize3
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
+ - Enrico Pilotto
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-21 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: globalize3
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: Auto translate with Google Translate the Globalize3 attributes
34
+ email:
35
+ - enrico@megiston.it
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - README.rdoc
46
+ - Rakefile
47
+ - goobalize3.gemspec
48
+ - init.rb
49
+ - lib/goobalize3.rb
50
+ - lib/goobalize3/version.rb
51
+ - lib/google_translate.rb
52
+ has_rdoc: true
53
+ homepage: ""
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project: goobalize3
80
+ rubygems_version: 1.3.7
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Auto translate with Google Translate the Globalize3 attributes
84
+ test_files: []
85
+