globalize3 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/README.textile +80 -0
- data/Rakefile +22 -0
- data/VERSION +1 -0
- data/lib/globalize.rb +17 -0
- data/lib/globalize/active_record.rb +55 -0
- data/lib/globalize/active_record/act_macro.rb +51 -0
- data/lib/globalize/active_record/adapter.rb +82 -0
- data/lib/globalize/active_record/attributes.rb +25 -0
- data/lib/globalize/active_record/class_methods.rb +68 -0
- data/lib/globalize/active_record/instance_methods.rb +63 -0
- data/lib/globalize/active_record/migration.rb +44 -0
- data/lib/globalize/version.rb +3 -0
- data/lib/i18n/missing_translations_log_handler.rb +41 -0
- data/lib/i18n/missing_translations_raise_handler.rb +25 -0
- data/lib/patches/active_record/xml_attribute_serializer.rb +12 -0
- metadata +101 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2008, 2009 Joshua Harvey
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
h1. Globalize3
|
2
|
+
|
3
|
+
Globalize3 is the successor of Globalize for Rails. Globalize is targeted at ActiveRecord 3.
|
4
|
+
|
5
|
+
It is compatible with and builds on the new "I18n api in Ruby on Rails":http://guides.rubyonrails.org/i18n.html. and adds model translations to ActiveRecord.
|
6
|
+
|
7
|
+
Globalize3 is much more lightweight and compatible than its predecessor was. Model translations in Globalize3 use default ActiveRecord features and do not limit any ActiveRecord functionality any more.
|
8
|
+
|
9
|
+
h2. Requirements
|
10
|
+
|
11
|
+
ActiveRecord > 3.0.0.rc
|
12
|
+
I18n
|
13
|
+
|
14
|
+
h2. Installation
|
15
|
+
|
16
|
+
To install Globalize3 with its default setup just use:
|
17
|
+
|
18
|
+
<pre><code>
|
19
|
+
$ gem install globalize3
|
20
|
+
</code></pre>
|
21
|
+
|
22
|
+
h2. Model translations
|
23
|
+
|
24
|
+
Model translations allow you to translate your models' attribute values. E.g.
|
25
|
+
|
26
|
+
<pre><code>
|
27
|
+
class Post < ActiveRecord::Base
|
28
|
+
translates :title, :text
|
29
|
+
end
|
30
|
+
</code></pre>
|
31
|
+
|
32
|
+
Allows you to values for the attributes :title and :text per locale:
|
33
|
+
|
34
|
+
<pre><code>
|
35
|
+
I18n.locale = :en
|
36
|
+
post.title # => Globalize3 rocks!
|
37
|
+
|
38
|
+
I18n.locale = :he
|
39
|
+
post.title # => גלובאלייז2 שולט!
|
40
|
+
</code></pre>
|
41
|
+
|
42
|
+
In order to make this work, you'll need to add the appropriate translation tables. Globalize3 comes with a handy helper method to help you do this. It's called @create_translation_table!@. Here's an example:
|
43
|
+
|
44
|
+
<pre><code>
|
45
|
+
class CreatePosts < ActiveRecord::Migration
|
46
|
+
def self.up
|
47
|
+
create_table :posts do |t|
|
48
|
+
t.timestamps
|
49
|
+
end
|
50
|
+
Post.create_translation_table! :title => :string, :text => :text
|
51
|
+
end
|
52
|
+
def self.down
|
53
|
+
drop_table :posts
|
54
|
+
Post.drop_translation_table!
|
55
|
+
end
|
56
|
+
end
|
57
|
+
</code></pre>
|
58
|
+
|
59
|
+
Note that the ActiveRecord model @Post@ must already exist and have a @translates@ directive listing the translated fields.
|
60
|
+
|
61
|
+
h2. Migration from Globalize for Rails (version 1)
|
62
|
+
|
63
|
+
See this script by Tomasz Stachewicz: http://gist.github.com/120867
|
64
|
+
|
65
|
+
h2. Alternative Solutions
|
66
|
+
|
67
|
+
* "Veger's fork":http://github.com/veger/globalize2 - uses default AR schema for the default locale, delegates to the translations table for other locales only
|
68
|
+
* "TranslatableColumns":http://github.com/iain/translatable_columns - have multiple languages of the same attribute in a model (Iain Hecker)
|
69
|
+
* "localized_record":http://github.com/glennpow/localized_record - allows records to have localized attributes without any modifications to the database (Glenn Powell)
|
70
|
+
* "model_translations":http://github.com/janne/model_translations - Minimal implementation of Globalize2 style model translations (Jan Andersson)
|
71
|
+
|
72
|
+
h2. Related solutions
|
73
|
+
|
74
|
+
* "globalize2_versioning":http://github.com/joshmh/globalize2_versioning - acts_as_versioned style versioning for globalize2 (Joshua Harvey)
|
75
|
+
* "i18n_multi_locales_validations":http://github.com/ZenCocoon/i18n_multi_locales_validations - multi-locales attributes validations to validates attributes from globalize2 translations models (Sébastien Grosjean)
|
76
|
+
* "globalize2 Demo App":http://github.com/svenfuchs/globalize2-demo - demo application for globalize2 (Sven Fuchs)</li>
|
77
|
+
* "migrate_from_globalize1":http://gist.github.com/120867 - migrate model translations from Globalize1 to globalize2 (Tomasz Stachewicz)</li>
|
78
|
+
* "easy_globalize2_accessors":http://github.com/astropanic/easy_globalize2_accessors - easily access (read and write) globalize2-translated fields (astropanic, Tomasz Stachewicz)</li>
|
79
|
+
* "globalize2-easy-translate":http://github.com/bsamman/globalize2-easy-translate - adds methods to easily access or set translated attributes to your model (bsamman)</li>
|
80
|
+
* "batch_translations":http://github.com/alvarezrilla/batch_translations - allow saving multiple globalize2 translations in the same request (Jose Alvarez Rilla)</li>
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Run all tests.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Globalize3'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.1
|
data/lib/globalize.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'patches/active_record/xml_attribute_serializer'
|
2
|
+
|
3
|
+
module Globalize
|
4
|
+
autoload :ActiveRecord, 'globalize/active_record'
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def fallbacks?
|
8
|
+
I18n.respond_to?(:fallbacks)
|
9
|
+
end
|
10
|
+
|
11
|
+
def fallbacks(locale)
|
12
|
+
fallbacks? ? I18n.fallbacks[locale] : [locale.to_sym]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
ActiveRecord::Base.extend(Globalize::ActiveRecord::ActMacro)
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Globalize
|
2
|
+
class MigrationError < StandardError; end
|
3
|
+
class MigrationMissingTranslatedField < MigrationError; end
|
4
|
+
class BadMigrationFieldType < MigrationError; end
|
5
|
+
|
6
|
+
module ActiveRecord
|
7
|
+
autoload :ActMacro, 'globalize/active_record/act_macro'
|
8
|
+
autoload :Adapter, 'globalize/active_record/adapter'
|
9
|
+
autoload :Attributes, 'globalize/active_record/attributes'
|
10
|
+
autoload :ClassMethods, 'globalize/active_record/class_methods'
|
11
|
+
autoload :InstanceMethods, 'globalize/active_record/instance_methods'
|
12
|
+
autoload :Migration, 'globalize/active_record/migration'
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def build_translation_class(target, options)
|
16
|
+
options[:table_name] ||= "#{target.table_name.singularize}_translations"
|
17
|
+
|
18
|
+
klass = target.const_defined?(:Translation) ?
|
19
|
+
target.const_get(:Translation) :
|
20
|
+
target.const_set(:Translation, Class.new(::ActiveRecord::Base))
|
21
|
+
|
22
|
+
klass.class_eval do
|
23
|
+
set_table_name(options[:table_name])
|
24
|
+
|
25
|
+
belongs_to target.name.underscore.gsub('/', '_')
|
26
|
+
|
27
|
+
class << self
|
28
|
+
def by_locale(locale)
|
29
|
+
where(:locale => locale.to_s)
|
30
|
+
end
|
31
|
+
|
32
|
+
def by_locales(locales)
|
33
|
+
where(:locale => locales.map(&:to_s))
|
34
|
+
end
|
35
|
+
|
36
|
+
# TODO build is not defined here even when called through record.translations.find_or_initialize_by_locale(...)
|
37
|
+
# def find_or_initialize_by_locale(locale)
|
38
|
+
# by_locale(locale.to_s).first || build(:locale => locale.to_s)
|
39
|
+
# end
|
40
|
+
end
|
41
|
+
|
42
|
+
def locale
|
43
|
+
read_attribute(:locale).to_sym
|
44
|
+
end
|
45
|
+
|
46
|
+
def locale=(locale)
|
47
|
+
write_attribute(:locale, locale.to_s)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
klass
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Globalize
|
2
|
+
module ActiveRecord
|
3
|
+
module ActMacro
|
4
|
+
def locale
|
5
|
+
(defined?(@@locale) && @@locale)
|
6
|
+
end
|
7
|
+
|
8
|
+
def locale=(locale)
|
9
|
+
@@locale = locale
|
10
|
+
end
|
11
|
+
|
12
|
+
def translates(*attr_names)
|
13
|
+
return if translates?
|
14
|
+
options = attr_names.extract_options!
|
15
|
+
|
16
|
+
class_inheritable_accessor :translation_class, :translated_attribute_names
|
17
|
+
class_inheritable_writer :required_attributes
|
18
|
+
|
19
|
+
self.translation_class = ActiveRecord.build_translation_class(self, options)
|
20
|
+
self.translated_attribute_names = attr_names.map(&:to_sym)
|
21
|
+
|
22
|
+
include InstanceMethods
|
23
|
+
extend ClassMethods, Migration
|
24
|
+
|
25
|
+
after_save :save_translations!
|
26
|
+
has_many :translations, :class_name => translation_class.name,
|
27
|
+
:foreign_key => class_name.foreign_key,
|
28
|
+
:dependent => :delete_all
|
29
|
+
|
30
|
+
scope :with_translations, lambda { |locale|
|
31
|
+
conditions = required_attributes.map do |attribute|
|
32
|
+
"#{quoted_translation_table_name}.#{attribute} IS NOT NULL"
|
33
|
+
end
|
34
|
+
conditions << "#{quoted_translation_table_name}.locale = ?"
|
35
|
+
{ :include => :translations, :conditions => [conditions.join(' AND '), locale] }
|
36
|
+
}
|
37
|
+
|
38
|
+
attr_names.each { |attr_name| translated_attr_accessor(attr_name) }
|
39
|
+
end
|
40
|
+
|
41
|
+
def class_name
|
42
|
+
class_name = table_name[table_name_prefix.length..-(table_name_suffix.length + 1)].camelize
|
43
|
+
pluralize_table_names ? class_name.singularize : class_name
|
44
|
+
end
|
45
|
+
|
46
|
+
def translates?
|
47
|
+
included_modules.include?(InstanceMethods)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Globalize
|
2
|
+
module ActiveRecord
|
3
|
+
class Adapter
|
4
|
+
# The cache caches attributes that already were looked up for read access.
|
5
|
+
# The stash keeps track of new or changed values that need to be saved.
|
6
|
+
attr_reader :record, :cache, :stash
|
7
|
+
|
8
|
+
def initialize(record)
|
9
|
+
@record = record
|
10
|
+
@cache = Attributes.new
|
11
|
+
@stash = Attributes.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def fetch(locale, attr_name)
|
15
|
+
cache.contains?(locale, attr_name) ?
|
16
|
+
cache.read(locale, attr_name) :
|
17
|
+
cache.write(locale, attr_name, fetch_attribute(locale, attr_name))
|
18
|
+
end
|
19
|
+
|
20
|
+
def write(locale, attr_name, value)
|
21
|
+
stash.write(locale, attr_name, value)
|
22
|
+
cache.write(locale, attr_name, value)
|
23
|
+
end
|
24
|
+
|
25
|
+
def save_translations!
|
26
|
+
stash.each do |locale, attrs|
|
27
|
+
# TODO use find_or_initialize_by_locale
|
28
|
+
translation = record.translations.by_locale(locale.to_s).first
|
29
|
+
translation ||= record.translations.build(:locale => locale.to_s)
|
30
|
+
attrs.each { |attr_name, value| translation[attr_name] = value }
|
31
|
+
translation.save!
|
32
|
+
end
|
33
|
+
stash.clear
|
34
|
+
end
|
35
|
+
|
36
|
+
def reset
|
37
|
+
cache.clear
|
38
|
+
# stash.clear
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
|
43
|
+
def fetch_translation(locale)
|
44
|
+
locale = locale.to_sym
|
45
|
+
record.translations.loaded? ? record.translations.detect { |t| t.locale == locale } :
|
46
|
+
record.translations.by_locale(locale)
|
47
|
+
end
|
48
|
+
|
49
|
+
def fetch_translations(locale)
|
50
|
+
# only query if not already included with :include => translations
|
51
|
+
record.translations.loaded? ? record.translations :
|
52
|
+
record.translations.by_locales(Globalize.fallbacks(locale))
|
53
|
+
end
|
54
|
+
|
55
|
+
def fetch_attribute(locale, attr_name)
|
56
|
+
translations = fetch_translations(locale)
|
57
|
+
value, requested_locale = nil, locale
|
58
|
+
|
59
|
+
Globalize.fallbacks(locale).each do |fallback|
|
60
|
+
translation = translations.detect { |t| t.locale == fallback }
|
61
|
+
value = translation && translation.send(attr_name)
|
62
|
+
locale = fallback && break if value
|
63
|
+
end
|
64
|
+
|
65
|
+
set_metadata(value, :locale => locale, :requested_locale => requested_locale)
|
66
|
+
value
|
67
|
+
end
|
68
|
+
|
69
|
+
def set_metadata(object, metadata)
|
70
|
+
if object.respond_to?(:translation_metadata)
|
71
|
+
object.translation_metadata.merge!(meta_data)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def translation_metadata_accessor(object)
|
76
|
+
return if obj.respond_to?(:translation_metadata)
|
77
|
+
class << object; attr_accessor :translation_metadata end
|
78
|
+
object.translation_metadata ||= {}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Helper class for storing values per locale. Used by Globalize::Adapter
|
2
|
+
# to stash and cache attribute values.
|
3
|
+
module Globalize
|
4
|
+
module ActiveRecord
|
5
|
+
class Attributes < Hash
|
6
|
+
def [](locale)
|
7
|
+
locale = locale.to_sym
|
8
|
+
self[locale] = {} unless has_key?(locale)
|
9
|
+
self.fetch(locale)
|
10
|
+
end
|
11
|
+
|
12
|
+
def contains?(locale, attr_name)
|
13
|
+
self[locale].has_key?(attr_name)
|
14
|
+
end
|
15
|
+
|
16
|
+
def read(locale, attr_name)
|
17
|
+
self[locale][attr_name]
|
18
|
+
end
|
19
|
+
|
20
|
+
def write(locale, attr_name, value)
|
21
|
+
self[locale][attr_name] = value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Globalize
|
2
|
+
module ActiveRecord
|
3
|
+
module ClassMethods
|
4
|
+
delegate :set_translation_table_name, :to => :translation_class
|
5
|
+
|
6
|
+
def with_locale(locale)
|
7
|
+
previous_locale, self.locale = self.locale, locale
|
8
|
+
result = yield
|
9
|
+
self.locale = previous_locale
|
10
|
+
result
|
11
|
+
end
|
12
|
+
|
13
|
+
def translation_table_name
|
14
|
+
translation_class.table_name
|
15
|
+
end
|
16
|
+
|
17
|
+
def quoted_translation_table_name
|
18
|
+
translation_class.quoted_table_name
|
19
|
+
end
|
20
|
+
|
21
|
+
def required_attributes
|
22
|
+
@required_attributes ||= reflect_on_all_validations.select do |validation|
|
23
|
+
validation.macro == :validates_presence_of && translated_attribute_names.include?(validation.name)
|
24
|
+
end.map(&:name)
|
25
|
+
end
|
26
|
+
|
27
|
+
def respond_to?(method, *args, &block)
|
28
|
+
method.to_s =~ /^find_by_(\w+)$/ && translated_attribute_names.include?($1.to_sym) || super
|
29
|
+
end
|
30
|
+
|
31
|
+
def method_missing(method, *args)
|
32
|
+
if method.to_s =~ /^find_by_(\w+)$/ && translated_attribute_names.include?($1.to_sym)
|
33
|
+
find_first_by_translated_attr_and_locales($1, args.first)
|
34
|
+
else
|
35
|
+
super
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
protected
|
40
|
+
|
41
|
+
def find_first_by_translated_attr_and_locales(name, value)
|
42
|
+
query = "#{translated_attr_name(name)} = ? AND #{translated_attr_name('locale')} IN (?)"
|
43
|
+
locales = Globalize.fallbacks(locale || I18n.locale).map(&:to_s)
|
44
|
+
find(
|
45
|
+
:first,
|
46
|
+
:joins => :translations,
|
47
|
+
:conditions => [query, value, locales],
|
48
|
+
:readonly => false
|
49
|
+
)
|
50
|
+
end
|
51
|
+
|
52
|
+
def translated_attr_accessor(name)
|
53
|
+
define_method "#{name}=", lambda { |value|
|
54
|
+
globalize.write(self.class.locale || I18n.locale, name, value)
|
55
|
+
self[name] = value
|
56
|
+
}
|
57
|
+
define_method name, lambda { |*args|
|
58
|
+
globalize.fetch(args.first || self.class.locale || I18n.locale, name)
|
59
|
+
}
|
60
|
+
alias_method "#{name}_before_type_cast", name
|
61
|
+
end
|
62
|
+
|
63
|
+
def translated_attr_name(name)
|
64
|
+
"#{translation_class.table_name}.#{name}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Globalize
|
2
|
+
module ActiveRecord
|
3
|
+
module InstanceMethods
|
4
|
+
def globalize
|
5
|
+
@globalize ||= Adapter.new self
|
6
|
+
end
|
7
|
+
|
8
|
+
def attributes
|
9
|
+
self.attribute_names.inject({}) do |attrs, name|
|
10
|
+
attrs[name] = read_attribute(name) ||
|
11
|
+
(globalize.fetch(I18n.locale, name) rescue nil)
|
12
|
+
attrs
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def attributes=(attributes, *args)
|
17
|
+
if attributes.respond_to?(:delete) && locale = attributes.delete(:locale)
|
18
|
+
self.class.with_locale(locale) { super }
|
19
|
+
else
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def attribute_names
|
25
|
+
translated_attribute_names.map(&:to_s) + super
|
26
|
+
end
|
27
|
+
|
28
|
+
def available_locales
|
29
|
+
translations.select('DISTINCT locale').map(&:locale)
|
30
|
+
end
|
31
|
+
|
32
|
+
def translated_locales
|
33
|
+
translations.map(&:locale)
|
34
|
+
end
|
35
|
+
|
36
|
+
def translated_attributes
|
37
|
+
translated_attribute_names.inject({}) do |attributes, name|
|
38
|
+
attributes.merge(name => send(name))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def set_translations(options)
|
43
|
+
options.keys.each do |locale|
|
44
|
+
translation = translations.find_by_locale(locale.to_s) ||
|
45
|
+
translations.build(:locale => locale.to_s)
|
46
|
+
translation.update_attributes!(options[locale])
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def reload(options = nil)
|
51
|
+
translated_attribute_names.each { |name| @attributes.delete(name.to_s) }
|
52
|
+
globalize.reset
|
53
|
+
super(options)
|
54
|
+
end
|
55
|
+
|
56
|
+
protected
|
57
|
+
|
58
|
+
def save_translations!
|
59
|
+
globalize.save_translations!
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Globalize
|
2
|
+
module ActiveRecord
|
3
|
+
module Migration
|
4
|
+
def create_translation_table!(fields)
|
5
|
+
translated_attribute_names.each do |f|
|
6
|
+
raise MigrationMissingTranslatedField, "Missing translated field #{f}" unless fields[f]
|
7
|
+
end
|
8
|
+
|
9
|
+
fields.each do |name, type|
|
10
|
+
if translated_attribute_names.include?(name) && ![:string, :text].include?(type)
|
11
|
+
raise BadMigrationFieldType, "Bad field type for #{name}, should be :string or :text"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
self.connection.create_table(translation_table_name) do |t|
|
16
|
+
t.references table_name.sub(/^#{table_name_prefix}/, "").singularize
|
17
|
+
t.string :locale
|
18
|
+
fields.each do |name, type|
|
19
|
+
t.column name, type
|
20
|
+
end
|
21
|
+
t.timestamps
|
22
|
+
end
|
23
|
+
|
24
|
+
self.connection.add_index(
|
25
|
+
translation_table_name,
|
26
|
+
"#{table_name.sub(/^#{table_name_prefix}/, "").singularize}_id",
|
27
|
+
:name => translation_index_name
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
def translation_index_name
|
32
|
+
require 'digest/sha1'
|
33
|
+
# FIXME what's the max size of an index name?
|
34
|
+
index_name = "index_#{translation_table_name}_on_#{self.table_name.singularize}_id"
|
35
|
+
index_name.size < 50 ? index_name : "index_#{Digest::SHA1.hexdigest(index_name)}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def drop_translation_table!
|
39
|
+
self.connection.remove_index(translation_table_name, :name => translation_index_name) rescue nil
|
40
|
+
self.connection.drop_table(translation_table_name)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# A simple exception handler that behaves like the default exception handler
|
2
|
+
# but additionally logs missing translations to a given log.
|
3
|
+
#
|
4
|
+
# Useful for identifying missing translations during testing.
|
5
|
+
#
|
6
|
+
# E.g.
|
7
|
+
#
|
8
|
+
# require 'globalize/i18n/missing_translations_log_handler'
|
9
|
+
# I18n.missing_translations_logger = RAILS_DEFAULT_LOGGER
|
10
|
+
# I18n.exception_handler = :missing_translations_log_handler
|
11
|
+
#
|
12
|
+
# To set up a different log file:
|
13
|
+
#
|
14
|
+
# logger = Logger.new("#{RAILS_ROOT}/log/missing_translations.log")
|
15
|
+
# I18n.missing_translations_logger = logger
|
16
|
+
|
17
|
+
module I18n
|
18
|
+
@@missing_translations_logger = nil
|
19
|
+
|
20
|
+
class << self
|
21
|
+
def missing_translations_logger
|
22
|
+
@@missing_translations_logger ||= begin
|
23
|
+
require 'logger' unless defined?(Logger)
|
24
|
+
Logger.new(STDOUT)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def missing_translations_logger=(logger)
|
29
|
+
@@missing_translations_logger = logger
|
30
|
+
end
|
31
|
+
|
32
|
+
def missing_translations_log_handler(exception, locale, key, options)
|
33
|
+
if MissingTranslationData === exception
|
34
|
+
missing_translations_logger.warn(exception.message)
|
35
|
+
return exception.message
|
36
|
+
else
|
37
|
+
raise exception
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# A simple exception handler that behaves like the default exception handler
|
2
|
+
# but also raises on missing translations.
|
3
|
+
#
|
4
|
+
# Useful for identifying missing translations during testing.
|
5
|
+
#
|
6
|
+
# E.g.
|
7
|
+
#
|
8
|
+
# require 'globalize/i18n/missing_translations_raise_handler'
|
9
|
+
# I18n.exception_handler = :missing_translations_raise_handler
|
10
|
+
module I18n
|
11
|
+
class << self
|
12
|
+
def missing_translations_raise_handler(exception, locale, key, options)
|
13
|
+
raise exception
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
I18n.exception_handler = :missing_translations_raise_handler
|
19
|
+
|
20
|
+
ActionView::Helpers::TranslationHelper.module_eval do
|
21
|
+
def translate(key, options = {})
|
22
|
+
I18n.translate(key, options)
|
23
|
+
end
|
24
|
+
alias :t :translate
|
25
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'active_record/serializers/xml_serializer'
|
2
|
+
|
3
|
+
ActiveRecord::XmlSerializer::Attribute.class_eval do
|
4
|
+
def compute_type_with_translations
|
5
|
+
if @serializable.class.translated_attribute_names.include?(name.to_sym)
|
6
|
+
:string
|
7
|
+
else
|
8
|
+
compute_type_without_translations
|
9
|
+
end
|
10
|
+
end
|
11
|
+
alias_method_chain :compute_type, :translations
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: globalize3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sven Fuchs
|
14
|
+
- Joshua Harvey
|
15
|
+
- Clemens Kofler
|
16
|
+
- John-Paul Bader
|
17
|
+
autorequire:
|
18
|
+
bindir: bin
|
19
|
+
cert_chain: []
|
20
|
+
|
21
|
+
date: 2010-07-30 00:00:00 +02:00
|
22
|
+
default_executable:
|
23
|
+
dependencies:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: activerecord
|
26
|
+
prerelease: false
|
27
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
hash: 7712042
|
33
|
+
segments:
|
34
|
+
- 3
|
35
|
+
- 0
|
36
|
+
- 0
|
37
|
+
- rc
|
38
|
+
version: 3.0.0.rc
|
39
|
+
type: :runtime
|
40
|
+
version_requirements: *id001
|
41
|
+
description: "Rails I18n: de-facto standard library for ActiveRecord 3 model/data translation."
|
42
|
+
email: nobody@globalize-rails.org
|
43
|
+
executables: []
|
44
|
+
|
45
|
+
extensions: []
|
46
|
+
|
47
|
+
extra_rdoc_files: []
|
48
|
+
|
49
|
+
files:
|
50
|
+
- lib/globalize/active_record/act_macro.rb
|
51
|
+
- lib/globalize/active_record/adapter.rb
|
52
|
+
- lib/globalize/active_record/attributes.rb
|
53
|
+
- lib/globalize/active_record/class_methods.rb
|
54
|
+
- lib/globalize/active_record/instance_methods.rb
|
55
|
+
- lib/globalize/active_record/migration.rb
|
56
|
+
- lib/globalize/active_record.rb
|
57
|
+
- lib/globalize/version.rb
|
58
|
+
- lib/globalize.rb
|
59
|
+
- lib/i18n/missing_translations_log_handler.rb
|
60
|
+
- lib/i18n/missing_translations_raise_handler.rb
|
61
|
+
- lib/patches/active_record/xml_attribute_serializer.rb
|
62
|
+
- LICENSE
|
63
|
+
- Rakefile
|
64
|
+
- README.textile
|
65
|
+
- VERSION
|
66
|
+
has_rdoc: true
|
67
|
+
homepage: http://github.com/svenfuchs/globlize3
|
68
|
+
licenses: []
|
69
|
+
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project: "[none]"
|
96
|
+
rubygems_version: 1.3.7
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: "Rails I18n: de-facto standard library for ActiveRecord 3 model/data translation"
|
100
|
+
test_files: []
|
101
|
+
|