globalize3_translator 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +10 -0
- data/Gemfile.lock +42 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +80 -0
- data/Rakefile +34 -0
- data/lib/globalize/patches/active_record/adapter.rb +12 -0
- data/lib/globalize/patches/active_record/class_methods.rb +11 -0
- data/lib/globalize/patches/active_record/instance_methods.rb +9 -0
- data/lib/globalize/patches/active_record/migration.rb +17 -0
- data/lib/globalize/patches/active_record/translation.rb +48 -0
- data/lib/globalize/patches/core_ext/string.rb +7 -0
- data/lib/globalize/translator.rb +38 -0
- data/lib/globalize/translator/adapter.rb +58 -0
- data/lib/globalize/translator/backends/abstract.rb +16 -0
- data/lib/globalize/translator/backends/rtranslate.rb +17 -0
- data/lib/globalize/translator/configuration.rb +17 -0
- data/lib/globalize/translator/version.rb +5 -0
- metadata +158 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.0)
|
5
|
+
activesupport (= 3.0.0)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.4.1)
|
8
|
+
activerecord (3.0.0)
|
9
|
+
activemodel (= 3.0.0)
|
10
|
+
activesupport (= 3.0.0)
|
11
|
+
arel (~> 1.0.0)
|
12
|
+
tzinfo (~> 0.3.23)
|
13
|
+
activesupport (3.0.0)
|
14
|
+
arel (1.0.1)
|
15
|
+
activesupport (~> 3.0.0)
|
16
|
+
builder (2.1.2)
|
17
|
+
columnize (0.3.1)
|
18
|
+
globalize3 (0.0.7)
|
19
|
+
activerecord (>= 3.0.0.rc)
|
20
|
+
i18n (0.4.1)
|
21
|
+
json (1.4.6)
|
22
|
+
linecache (0.43)
|
23
|
+
ruby-debug (0.10.3)
|
24
|
+
columnize (>= 0.1)
|
25
|
+
ruby-debug-base (~> 0.10.3.0)
|
26
|
+
ruby-debug-base (0.10.3)
|
27
|
+
linecache (>= 0.3)
|
28
|
+
sishen-rtranslate (1.4)
|
29
|
+
activesupport (>= 2.2.0)
|
30
|
+
json (>= 1.1.3)
|
31
|
+
sqlite3-ruby (1.3.1)
|
32
|
+
tzinfo (0.3.23)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
ruby
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
activerecord (>= 3.0.0.rc)
|
39
|
+
globalize3 (>= 0.0.7)
|
40
|
+
ruby-debug
|
41
|
+
sishen-rtranslate (>= 1.3)
|
42
|
+
sqlite3-ruby
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Laurynas Butkus
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
= Globalize3 Translator
|
2
|
+
|
3
|
+
* Automatically adds missing model translations
|
4
|
+
* Uses Google Translate by default (you can use your own backend)
|
5
|
+
* Marks auto translated data
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
To install Globalize3 Translator use:
|
10
|
+
|
11
|
+
gem install globalize3_translator
|
12
|
+
|
13
|
+
== Configuration
|
14
|
+
|
15
|
+
If you wish to override default configuration create
|
16
|
+
config/initializers/globalize3.rb
|
17
|
+
|
18
|
+
Globalize::Translator.configure do |config|
|
19
|
+
# automatically translate only to specified locales
|
20
|
+
config.locales = [ :lt, :de ]
|
21
|
+
|
22
|
+
# use cust translator backend
|
23
|
+
config.backend = MyTranslatorBackend
|
24
|
+
end
|
25
|
+
|
26
|
+
== Identifying auto translated data with auto_translated?
|
27
|
+
|
28
|
+
Example:
|
29
|
+
|
30
|
+
post = Post.new :title => "Dog"
|
31
|
+
post.save!
|
32
|
+
post.reload
|
33
|
+
|
34
|
+
# should be "false" for current locale
|
35
|
+
post.title.auto_translated?
|
36
|
+
|
37
|
+
I18n.locale = :lt
|
38
|
+
|
39
|
+
# should be "true" for other locale
|
40
|
+
post.title.auto_translated?
|
41
|
+
|
42
|
+
|
43
|
+
== Writing custom backend
|
44
|
+
|
45
|
+
Example:
|
46
|
+
|
47
|
+
class MyTranslatorBackend < Globalize::Translator::Backends::Abstract
|
48
|
+
|
49
|
+
def translate(value, from_locale, to_locale)
|
50
|
+
# should return translated value
|
51
|
+
"test #{value}"
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
== Migrations
|
58
|
+
|
59
|
+
Globalize3 Translator uses extra boolean fields in translations table
|
60
|
+
to track automatic translations (with suffix '_auto_translated').
|
61
|
+
|
62
|
+
Globalize3 standard method "create_translation_table!"
|
63
|
+
automatically adds <name>_auto_translated fields so you don't have to
|
64
|
+
worry about that for new tables.
|
65
|
+
|
66
|
+
If you want to add auto translated fields to existing translations
|
67
|
+
table, you should do it manually in migration.
|
68
|
+
|
69
|
+
Example:
|
70
|
+
|
71
|
+
add_column :post_translations, :title_auto_translated, :boolean
|
72
|
+
|
73
|
+
|
74
|
+
== Patches, fixes
|
75
|
+
|
76
|
+
Please send all patches or fixes as github pull requests.
|
77
|
+
Preferably with tests.
|
78
|
+
|
79
|
+
|
80
|
+
Copyright (c) 2010 Laurynas Butkus, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'lib/globalize/translator/version'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
desc 'Test the globalize3_translator plugin.'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.libs << 'test'
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Generate documentation for the globalize3_translator plugin.'
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'Globalize3Translator'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'Build gem'
|
27
|
+
task :build do
|
28
|
+
system "gem build globalize3_translator.gemspec"
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'Release gem'
|
32
|
+
task :release => :build do
|
33
|
+
system "gem push globalize3_translator-#{Globalize::Translator::VERSION}.gem"
|
34
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'globalize/active_record/adapter'
|
2
|
+
|
3
|
+
Globalize::ActiveRecord::Adapter.class_eval do
|
4
|
+
|
5
|
+
def save_translations_with_auto!
|
6
|
+
record.globalize_translator.translate
|
7
|
+
save_translations_without_auto!
|
8
|
+
end
|
9
|
+
|
10
|
+
alias_method_chain :save_translations!, :auto
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'globalize/active_record/class_methods'
|
2
|
+
|
3
|
+
Globalize::ActiveRecord::ClassMethods.class_eval do
|
4
|
+
def translation_class_with_auto
|
5
|
+
klass = translation_class_without_auto
|
6
|
+
klass.handle_auto_translated_attributes(translated_attribute_names)
|
7
|
+
klass
|
8
|
+
end
|
9
|
+
|
10
|
+
alias_method_chain :translation_class, :auto
|
11
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'globalize/active_record/migration'
|
2
|
+
|
3
|
+
Globalize::ActiveRecord::Migration::Migrator.class_eval do
|
4
|
+
|
5
|
+
def create_translation_table_with_auto
|
6
|
+
create_translation_table_without_auto
|
7
|
+
|
8
|
+
# add auto translation indicators
|
9
|
+
fields.each do |name, type|
|
10
|
+
field_name = "#{name}_auto_translated"
|
11
|
+
connection.add_column translations_table_name, field_name, :boolean
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method_chain :create_translation_table, :auto
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'globalize/active_record/translation'
|
2
|
+
|
3
|
+
Globalize::ActiveRecord::Translation.class_eval do
|
4
|
+
|
5
|
+
class << self
|
6
|
+
cattr_accessor :auto_attributes
|
7
|
+
|
8
|
+
def handle_auto_translated_attributes(attribute_names)
|
9
|
+
self.auto_attributes = attribute_names
|
10
|
+
self.auto_attributes.each { |attr_name| self.auto_translated_attr_accessor(attr_name) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def auto_translated_attr_accessor(name)
|
14
|
+
define_method(name) do |*args|
|
15
|
+
read_auto_translated_attribute(name)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def read_auto_translated_attribute(name)
|
21
|
+
val = read_attribute(name)
|
22
|
+
|
23
|
+
if val && val.auto_translated.nil?
|
24
|
+
val.auto_translated = read_attribute(auto_marker_name(name))
|
25
|
+
end
|
26
|
+
|
27
|
+
val
|
28
|
+
end
|
29
|
+
|
30
|
+
def write_attribute(attr_name, value)
|
31
|
+
if self.class.auto_attributes.include?(attr_name.to_sym)
|
32
|
+
marker_name = auto_marker_name(attr_name)
|
33
|
+
|
34
|
+
if value
|
35
|
+
self[marker_name] = value.auto_translated?
|
36
|
+
else
|
37
|
+
self[marker_name] = nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
super
|
42
|
+
end
|
43
|
+
|
44
|
+
def auto_marker_name(field_name)
|
45
|
+
"#{field_name}_auto_translated"
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'globalize/patches/core_ext/string'
|
2
|
+
require 'globalize/patches/active_record/migration'
|
3
|
+
require 'globalize/patches/active_record/class_methods'
|
4
|
+
require 'globalize/patches/active_record/instance_methods'
|
5
|
+
require 'globalize/patches/active_record/adapter'
|
6
|
+
require 'globalize/patches/active_record/translation'
|
7
|
+
require 'globalize/translator/version'
|
8
|
+
require 'globalize/translator/configuration'
|
9
|
+
require 'globalize/translator/backends/abstract'
|
10
|
+
require 'globalize/translator/backends/rtranslate'
|
11
|
+
|
12
|
+
module Globalize
|
13
|
+
module Translator
|
14
|
+
autoload :Adapter, 'globalize/translator/adapter'
|
15
|
+
|
16
|
+
class << self
|
17
|
+
attr_accessor :backend
|
18
|
+
|
19
|
+
def configure
|
20
|
+
yield(config)
|
21
|
+
end
|
22
|
+
|
23
|
+
def config
|
24
|
+
@config||= Configuration.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def backend
|
28
|
+
@backend||= config.backend.new
|
29
|
+
end
|
30
|
+
|
31
|
+
def translatable_locales
|
32
|
+
config.locales || I18n.available_locales
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Globalize
|
2
|
+
module Translator
|
3
|
+
class Adapter
|
4
|
+
attr_reader :record
|
5
|
+
|
6
|
+
delegate :globalize, :to => :record
|
7
|
+
delegate :stash, :to => :globalize
|
8
|
+
delegate :backend, :to => :translator
|
9
|
+
delegate :translatable_locales, :to => :translator
|
10
|
+
|
11
|
+
def initialize(record)
|
12
|
+
@record = record
|
13
|
+
end
|
14
|
+
|
15
|
+
def translate
|
16
|
+
attrs = stash.values.collect(&:keys).sum.uniq
|
17
|
+
attrs.each { |name| translate_attribute(name) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def translate_attribute(name)
|
21
|
+
from_locale = get_source_locale(name)
|
22
|
+
from_value = stash.read(from_locale, name)
|
23
|
+
|
24
|
+
translatable_locales.each do |locale|
|
25
|
+
next unless translate?(locale, name)
|
26
|
+
|
27
|
+
value = backend.translate(from_value, from_locale, locale)
|
28
|
+
|
29
|
+
unless value.nil?
|
30
|
+
stash.write(locale, name, value)
|
31
|
+
value.auto_translated = true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
def manual_in_stash?(locale, name)
|
39
|
+
value = stash.read(locale, name)
|
40
|
+
!value.nil? && !value.auto_translated?
|
41
|
+
end
|
42
|
+
|
43
|
+
def translate?(locale, name)
|
44
|
+
!manual_in_stash?(locale, name)
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_source_locale(name)
|
48
|
+
locales = stash.keys.select { |locale| manual_in_stash?(locale, name) }
|
49
|
+
locales.include?(I18n.default_locale) ? I18n.default_locale : locales.first
|
50
|
+
end
|
51
|
+
|
52
|
+
def translator
|
53
|
+
Translator
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rtranslate'
|
2
|
+
|
3
|
+
module Globalize
|
4
|
+
module Translator
|
5
|
+
module Backends
|
6
|
+
|
7
|
+
class RTranslate < Abstract
|
8
|
+
|
9
|
+
def translate(value, from_locale, to_locale)
|
10
|
+
Translate.t(value, from_locale.to_s, to_locale.to_s)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: globalize3_translator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Laurynas Butkus
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-10 00:00:00 +03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activerecord
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7712042
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
- rc
|
35
|
+
version: 3.0.0.rc
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id001
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: globalize3
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 17
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
- 0
|
50
|
+
- 7
|
51
|
+
version: 0.0.7
|
52
|
+
type: :runtime
|
53
|
+
version_requirements: *id002
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: sishen-rtranslate
|
56
|
+
prerelease: false
|
57
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 9
|
63
|
+
segments:
|
64
|
+
- 1
|
65
|
+
- 3
|
66
|
+
version: "1.3"
|
67
|
+
type: :runtime
|
68
|
+
version_requirements: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: ruby-debug
|
71
|
+
prerelease: false
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
type: :development
|
82
|
+
version_requirements: *id004
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3-ruby
|
85
|
+
prerelease: false
|
86
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
type: :development
|
96
|
+
version_requirements: *id005
|
97
|
+
description: Globalize3 auto-translator using Google Translate (or any other backend).
|
98
|
+
email: laurynas.butkus@gmail.com
|
99
|
+
executables: []
|
100
|
+
|
101
|
+
extensions: []
|
102
|
+
|
103
|
+
extra_rdoc_files: []
|
104
|
+
|
105
|
+
files:
|
106
|
+
- lib/globalize/patches/core_ext/string.rb
|
107
|
+
- lib/globalize/patches/active_record/adapter.rb
|
108
|
+
- lib/globalize/patches/active_record/migration.rb
|
109
|
+
- lib/globalize/patches/active_record/class_methods.rb
|
110
|
+
- lib/globalize/patches/active_record/instance_methods.rb
|
111
|
+
- lib/globalize/patches/active_record/translation.rb
|
112
|
+
- lib/globalize/translator.rb
|
113
|
+
- lib/globalize/translator/adapter.rb
|
114
|
+
- lib/globalize/translator/version.rb
|
115
|
+
- lib/globalize/translator/configuration.rb
|
116
|
+
- lib/globalize/translator/backends/rtranslate.rb
|
117
|
+
- lib/globalize/translator/backends/abstract.rb
|
118
|
+
- README.rdoc
|
119
|
+
- Rakefile
|
120
|
+
- MIT-LICENSE
|
121
|
+
- Gemfile.lock
|
122
|
+
- Gemfile
|
123
|
+
has_rdoc: true
|
124
|
+
homepage: http://github.com/laurynas/globalize3_translator
|
125
|
+
licenses: []
|
126
|
+
|
127
|
+
post_install_message:
|
128
|
+
rdoc_options: []
|
129
|
+
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
none: false
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
hash: 3
|
138
|
+
segments:
|
139
|
+
- 0
|
140
|
+
version: "0"
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
none: false
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
hash: 3
|
147
|
+
segments:
|
148
|
+
- 0
|
149
|
+
version: "0"
|
150
|
+
requirements: []
|
151
|
+
|
152
|
+
rubyforge_project: "[none]"
|
153
|
+
rubygems_version: 1.3.7
|
154
|
+
signing_key:
|
155
|
+
specification_version: 3
|
156
|
+
summary: Globalize3 auto-translator using Google Translate (or any other backend)
|
157
|
+
test_files: []
|
158
|
+
|