globalize2 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -1
- data/README.textile +27 -143
- data/VERSION +1 -1
- data/generators/templates/db_backend_migration.rb +3 -3
- data/globalize2.gemspec +30 -49
- data/init.rb +1 -8
- data/lib/globalize.rb +15 -0
- data/lib/globalize/active_record.rb +195 -0
- data/lib/globalize/active_record/adapter.rb +80 -0
- data/lib/globalize/active_record/attributes.rb +25 -0
- data/lib/globalize/active_record/migration.rb +40 -0
- data/lib/{globalize/i18n → i18n}/missing_translations_log_handler.rb +8 -8
- data/lib/{globalize/i18n → i18n}/missing_translations_raise_handler.rb +3 -5
- data/test/active_record/fallbacks_test.rb +102 -0
- data/test/{model/active_record → active_record}/migration_test.rb +21 -26
- data/test/{model/active_record → active_record}/sti_translated_test.rb +4 -30
- data/test/active_record/translates_test.rb +87 -0
- data/test/active_record/translation_class_test.rb +30 -0
- data/test/active_record/validation_tests.rb +75 -0
- data/test/active_record_test.rb +451 -0
- data/test/data/models.rb +16 -0
- data/test/data/schema.rb +23 -7
- data/test/i18n/missing_translations_test.rb +6 -6
- data/test/test_helper.rb +55 -15
- metadata +33 -46
- data/lib/globalize/backend/chain.rb +0 -102
- data/lib/globalize/backend/pluralizing.rb +0 -37
- data/lib/globalize/backend/static.rb +0 -61
- data/lib/globalize/load_path.rb +0 -63
- data/lib/globalize/locale/fallbacks.rb +0 -63
- data/lib/globalize/locale/language_tag.rb +0 -81
- data/lib/globalize/model/active_record.rb +0 -56
- data/lib/globalize/model/active_record/adapter.rb +0 -100
- data/lib/globalize/model/active_record/translated.rb +0 -174
- data/lib/globalize/translation.rb +0 -32
- data/lib/locale/root.yml +0 -3
- data/lib/rails_edge_load_path_patch.rb +0 -40
- data/notes.textile +0 -51
- data/test/backends/chained_test.rb +0 -175
- data/test/backends/pluralizing_test.rb +0 -63
- data/test/backends/static_test.rb +0 -147
- data/test/data/locale/all.yml +0 -2
- data/test/data/locale/de-DE.yml +0 -2
- data/test/data/locale/en-US.yml +0 -2
- data/test/data/locale/en-US/module.yml +0 -2
- data/test/data/locale/fi-FI/module.yml +0 -2
- data/test/data/locale/root.yml +0 -0
- data/test/load_path_test.rb +0 -49
- data/test/locale/fallbacks_test.rb +0 -154
- data/test/locale/language_tag_test.rb +0 -130
- data/test/model/active_record/translated_test.rb +0 -487
- data/test/translation_test.rb +0 -54
data/test/data/models.rb
CHANGED
@@ -1,9 +1,16 @@
|
|
1
|
+
#require 'ruby2ruby'
|
2
|
+
#require 'parse_tree'
|
3
|
+
#require 'parse_tree_extensions'
|
4
|
+
#require 'pp'
|
5
|
+
|
1
6
|
class PostTranslation < ActiveRecord::Base
|
2
7
|
def existing_method ; end
|
3
8
|
end
|
9
|
+
|
4
10
|
class Post < ActiveRecord::Base
|
5
11
|
translates :subject, :content
|
6
12
|
validates_presence_of :subject
|
13
|
+
named_scope :foobar, :conditions => { :title => "foobar" }
|
7
14
|
end
|
8
15
|
|
9
16
|
class Blog < ActiveRecord::Base
|
@@ -38,3 +45,12 @@ class Reloader < Parent
|
|
38
45
|
reload
|
39
46
|
end
|
40
47
|
end
|
48
|
+
|
49
|
+
class Validatee < ActiveRecord::Base
|
50
|
+
translates :string
|
51
|
+
end
|
52
|
+
|
53
|
+
class User < ActiveRecord::Base
|
54
|
+
translates :name
|
55
|
+
validates_presence_of :name, :email
|
56
|
+
end
|
data/test/data/schema.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
ActiveRecord::Schema.define do
|
2
|
-
|
3
2
|
create_table :blogs, :force => true do |t|
|
4
|
-
t.string
|
3
|
+
t.string :description
|
5
4
|
end
|
6
5
|
|
7
6
|
create_table :posts, :force => true do |t|
|
@@ -14,7 +13,7 @@ ActiveRecord::Schema.define do
|
|
14
13
|
t.string :subject
|
15
14
|
t.text :content
|
16
15
|
end
|
17
|
-
|
16
|
+
|
18
17
|
create_table :parents, :force => true do |t|
|
19
18
|
end
|
20
19
|
|
@@ -24,16 +23,33 @@ ActiveRecord::Schema.define do
|
|
24
23
|
t.text :content
|
25
24
|
t.string :type
|
26
25
|
end
|
27
|
-
|
26
|
+
|
28
27
|
create_table :comments, :force => true do |t|
|
29
28
|
t.references :post
|
30
29
|
end
|
31
30
|
|
32
|
-
create_table :
|
31
|
+
create_table :comment_translations, :force => true do |t|
|
33
32
|
t.string :locale
|
34
33
|
t.references :comment
|
35
34
|
t.string :subject
|
36
35
|
t.text :content
|
37
36
|
end
|
38
|
-
|
39
|
-
|
37
|
+
|
38
|
+
create_table :validatees, :force => true do |t|
|
39
|
+
end
|
40
|
+
|
41
|
+
create_table :validatee_translations, :force => true do |t|
|
42
|
+
t.string :locale
|
43
|
+
t.references :validatee
|
44
|
+
t.string :string
|
45
|
+
end
|
46
|
+
|
47
|
+
create_table :users, :force => true do |t|
|
48
|
+
t.string :email
|
49
|
+
end
|
50
|
+
|
51
|
+
create_table :users_translations, :force => true do |t|
|
52
|
+
t.references :user
|
53
|
+
t.string :name
|
54
|
+
end
|
55
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require File.
|
2
|
-
require '
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
require 'i18n/missing_translations_log_handler'
|
3
3
|
|
4
4
|
class MissingTranslationsTest < ActiveSupport::TestCase
|
5
5
|
test "defines I18n.missing_translations_logger accessor" do
|
@@ -19,18 +19,18 @@ class LogMissingTranslationsTest < ActiveSupport::TestCase
|
|
19
19
|
def setup
|
20
20
|
@locale, @key, @options = :en, :foo, {}
|
21
21
|
@exception = I18n::MissingTranslationData.new(@locale, @key, @options)
|
22
|
-
|
22
|
+
|
23
23
|
@logger = TestLogger.new
|
24
24
|
I18n.missing_translations_logger = @logger
|
25
25
|
end
|
26
26
|
|
27
27
|
test "still returns the exception message for MissingTranslationData exceptions" do
|
28
28
|
result = I18n.send(:missing_translations_log_handler, @exception, @locale, @key, @options)
|
29
|
-
assert_equal 'translation missing: en, foo', result
|
29
|
+
assert_equal 'translation missing: en, foo', result
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
test "logs the missing translation to I18n.missing_translations_logger" do
|
33
33
|
I18n.send(:missing_translations_log_handler, @exception, @locale, @key, @options)
|
34
|
-
assert_equal 'translation missing: en, foo', @logger
|
34
|
+
assert_equal 'translation missing: en, foo', @logger
|
35
35
|
end
|
36
36
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,28 +1,41 @@
|
|
1
|
+
$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' )
|
2
|
+
|
1
3
|
require 'rubygems'
|
2
4
|
require 'test/unit'
|
5
|
+
require 'active_record'
|
3
6
|
require 'active_support'
|
4
7
|
require 'active_support/test_case'
|
5
8
|
require 'mocha'
|
9
|
+
require 'globalize'
|
10
|
+
# require 'validation_reflection'
|
6
11
|
|
7
|
-
|
12
|
+
config = { :adapter => 'sqlite3', :database => ':memory:' }
|
13
|
+
ActiveRecord::Base.establish_connection(config)
|
8
14
|
|
9
15
|
class ActiveSupport::TestCase
|
10
|
-
def reset_db!(
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
:database => ':memory:'
|
15
|
-
})
|
16
|
-
::ActiveRecord::Base.silence do
|
17
|
-
load schema_path
|
18
|
-
end
|
16
|
+
def reset_db!(schema_path = nil)
|
17
|
+
schema_path ||= File.expand_path(File.dirname(__FILE__) + '/data/schema.rb')
|
18
|
+
ActiveRecord::Migration.verbose = false
|
19
|
+
ActiveRecord::Base.silence { load(schema_path) }
|
19
20
|
end
|
20
|
-
|
21
|
-
def assert_member(item,
|
22
|
-
assert_block "Item #{item} is not in array #{
|
23
|
-
|
21
|
+
|
22
|
+
def assert_member(item, array)
|
23
|
+
assert_block "Item #{item} is not in array #{array}" do
|
24
|
+
array.member?(item)
|
24
25
|
end
|
25
26
|
end
|
27
|
+
|
28
|
+
def assert_belongs_to(model, associated)
|
29
|
+
assert model.reflect_on_all_associations(:belongs_to).detect { |association|
|
30
|
+
association.name.to_s == associated.to_s
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def assert_has_many(model, associated)
|
35
|
+
assert model.reflect_on_all_associations(:has_many).detect { |association|
|
36
|
+
association.name.to_s == associated.to_s
|
37
|
+
}
|
38
|
+
end
|
26
39
|
end
|
27
40
|
|
28
41
|
module ActiveRecord
|
@@ -33,4 +46,31 @@ module ActiveRecord
|
|
33
46
|
end
|
34
47
|
end
|
35
48
|
end
|
36
|
-
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# module ActiveRecord
|
52
|
+
# class BaseWithoutTable < Base
|
53
|
+
# self.abstract_class = true
|
54
|
+
#
|
55
|
+
# def create_or_update
|
56
|
+
# errors.empty?
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
# class << self
|
60
|
+
# def columns()
|
61
|
+
# @columns ||= []
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# def column(name, sql_type = nil, default = nil, null = true)
|
65
|
+
# columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
66
|
+
# reset_column_information
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# # Do not reset @columns
|
70
|
+
# def reset_column_information
|
71
|
+
# read_methods.each { |name| undef_method(name) }
|
72
|
+
# @column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = nil
|
73
|
+
# end
|
74
|
+
# end
|
75
|
+
# end
|
76
|
+
# end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: globalize2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Sven Fuchs, Joshua Harvey, Clemens Kofler
|
@@ -9,7 +14,7 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-04-22 00:00:00 +02:00
|
13
18
|
default_executable:
|
14
19
|
dependencies: []
|
15
20
|
|
@@ -32,43 +37,26 @@ files:
|
|
32
37
|
- generators/templates/db_backend_migration.rb
|
33
38
|
- globalize2.gemspec
|
34
39
|
- init.rb
|
35
|
-
- lib/globalize
|
36
|
-
- lib/globalize/
|
37
|
-
- lib/globalize/
|
38
|
-
- lib/globalize/
|
39
|
-
- lib/globalize/
|
40
|
-
- lib/
|
41
|
-
- lib/
|
42
|
-
-
|
43
|
-
-
|
44
|
-
-
|
45
|
-
-
|
46
|
-
-
|
47
|
-
-
|
48
|
-
-
|
49
|
-
- notes.textile
|
40
|
+
- lib/globalize.rb
|
41
|
+
- lib/globalize/active_record.rb
|
42
|
+
- lib/globalize/active_record/adapter.rb
|
43
|
+
- lib/globalize/active_record/attributes.rb
|
44
|
+
- lib/globalize/active_record/migration.rb
|
45
|
+
- lib/i18n/missing_translations_log_handler.rb
|
46
|
+
- lib/i18n/missing_translations_raise_handler.rb
|
47
|
+
- test/active_record/fallbacks_test.rb
|
48
|
+
- test/active_record/migration_test.rb
|
49
|
+
- test/active_record/sti_translated_test.rb
|
50
|
+
- test/active_record/translates_test.rb
|
51
|
+
- test/active_record/translation_class_test.rb
|
52
|
+
- test/active_record/validation_tests.rb
|
53
|
+
- test/active_record_test.rb
|
50
54
|
- test/all.rb
|
51
|
-
- test/backends/chained_test.rb
|
52
|
-
- test/backends/pluralizing_test.rb
|
53
|
-
- test/backends/static_test.rb
|
54
|
-
- test/data/locale/all.yml
|
55
|
-
- test/data/locale/de-DE.yml
|
56
|
-
- test/data/locale/en-US.yml
|
57
|
-
- test/data/locale/en-US/module.yml
|
58
|
-
- test/data/locale/fi-FI/module.yml
|
59
|
-
- test/data/locale/root.yml
|
60
55
|
- test/data/models.rb
|
61
56
|
- test/data/no_globalize_schema.rb
|
62
57
|
- test/data/schema.rb
|
63
58
|
- test/i18n/missing_translations_test.rb
|
64
|
-
- test/load_path_test.rb
|
65
|
-
- test/locale/fallbacks_test.rb
|
66
|
-
- test/locale/language_tag_test.rb
|
67
|
-
- test/model/active_record/migration_test.rb
|
68
|
-
- test/model/active_record/sti_translated_test.rb
|
69
|
-
- test/model/active_record/translated_test.rb
|
70
59
|
- test/test_helper.rb
|
71
|
-
- test/translation_test.rb
|
72
60
|
has_rdoc: true
|
73
61
|
homepage: http://github.com/joshmh/globalize2
|
74
62
|
licenses: []
|
@@ -82,35 +70,34 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
82
70
|
requirements:
|
83
71
|
- - ">="
|
84
72
|
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 0
|
85
75
|
version: "0"
|
86
|
-
version:
|
87
76
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
77
|
requirements:
|
89
78
|
- - ">="
|
90
79
|
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
91
82
|
version: "0"
|
92
|
-
version:
|
93
83
|
requirements: []
|
94
84
|
|
95
85
|
rubyforge_project:
|
96
|
-
rubygems_version: 1.3.
|
86
|
+
rubygems_version: 1.3.6
|
97
87
|
signing_key:
|
98
88
|
specification_version: 3
|
99
89
|
summary: "Rails I18n: de-facto standard library for ActiveRecord data translation"
|
100
90
|
test_files:
|
91
|
+
- test/active_record/fallbacks_test.rb
|
92
|
+
- test/active_record/migration_test.rb
|
93
|
+
- test/active_record/sti_translated_test.rb
|
94
|
+
- test/active_record/translates_test.rb
|
95
|
+
- test/active_record/translation_class_test.rb
|
96
|
+
- test/active_record/validation_tests.rb
|
97
|
+
- test/active_record_test.rb
|
101
98
|
- test/all.rb
|
102
|
-
- test/backends/chained_test.rb
|
103
|
-
- test/backends/pluralizing_test.rb
|
104
|
-
- test/backends/static_test.rb
|
105
99
|
- test/data/models.rb
|
106
100
|
- test/data/no_globalize_schema.rb
|
107
101
|
- test/data/schema.rb
|
108
102
|
- test/i18n/missing_translations_test.rb
|
109
|
-
- test/load_path_test.rb
|
110
|
-
- test/locale/fallbacks_test.rb
|
111
|
-
- test/locale/language_tag_test.rb
|
112
|
-
- test/model/active_record/migration_test.rb
|
113
|
-
- test/model/active_record/sti_translated_test.rb
|
114
|
-
- test/model/active_record/translated_test.rb
|
115
103
|
- test/test_helper.rb
|
116
|
-
- test/translation_test.rb
|
@@ -1,102 +0,0 @@
|
|
1
|
-
module I18n
|
2
|
-
class << self
|
3
|
-
def chain_backends(*args)
|
4
|
-
self.backend = Globalize::Backend::Chain.new(*args)
|
5
|
-
end
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
module Globalize
|
10
|
-
module Backend
|
11
|
-
class Chain
|
12
|
-
def initialize(*args)
|
13
|
-
add(*args) unless args.empty?
|
14
|
-
end
|
15
|
-
|
16
|
-
# Change this to a) accept any number of backends and b) accept classes.
|
17
|
-
# When classes are passed instantiate them and add the instances as backends.
|
18
|
-
# Return the added backends from #add.
|
19
|
-
#
|
20
|
-
# Add an initialize method that accepts the same arguments and passes them
|
21
|
-
# to #add, so we could:
|
22
|
-
# I18n.backend = Globalize::Backend::Chain.new(Globalize::Backend::Foo, Globalize::Backend::Bar)
|
23
|
-
# Globalize::Backend::Chain.new(:foo, :bar)
|
24
|
-
# Globalize.chain_backends :foo, :bar
|
25
|
-
def add(*backends)
|
26
|
-
backends.each do |backend|
|
27
|
-
backend = Globalize::Backend.const_get(backend.to_s.capitalize) if backend.is_a? Symbol
|
28
|
-
backend = backend.new if backend.is_a? Class
|
29
|
-
self.backends << backend
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def load_translations(*args)
|
34
|
-
backends.each{|backend| backend.load_translations(*args) }
|
35
|
-
end
|
36
|
-
|
37
|
-
# For defaults:
|
38
|
-
# Never pass any default option to the backends but instead implement our own default
|
39
|
-
# mechanism (e.g. symbols as defaults would need to be passed to the whole chain to
|
40
|
-
# be translated).
|
41
|
-
#
|
42
|
-
# For namespace lookup:
|
43
|
-
# Only return if the result is not a hash OR count is not present, otherwise merge them.
|
44
|
-
# So in effect the count variable would control whether we have a namespace lookup or a
|
45
|
-
# pluralization going on.
|
46
|
-
#
|
47
|
-
# Exceptions:
|
48
|
-
# Make sure that we catch MissingTranslationData exceptions and raise
|
49
|
-
# one in the end when no translation was found at all.
|
50
|
-
#
|
51
|
-
# For bulk translation:
|
52
|
-
# If the key is an array we need to call #translate for each of the
|
53
|
-
# keys and collect the results.
|
54
|
-
|
55
|
-
def translate(locale, key, options = {})
|
56
|
-
raise I18n::InvalidLocale.new(locale) if locale.nil?
|
57
|
-
return key.map{|k| translate locale, k, options } if key.is_a? Array
|
58
|
-
|
59
|
-
default = options.delete(:default)
|
60
|
-
result = backends.inject({}) do |namespace, backend|
|
61
|
-
begin
|
62
|
-
translation = backend.translate(locale.to_sym, key, options)
|
63
|
-
if namespace_lookup?(translation, options)
|
64
|
-
namespace.merge! translation
|
65
|
-
elsif translation
|
66
|
-
return translation
|
67
|
-
end
|
68
|
-
rescue I18n::MissingTranslationData
|
69
|
-
end
|
70
|
-
end
|
71
|
-
result || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options))
|
72
|
-
end
|
73
|
-
|
74
|
-
def localize(locale, object, format = :default)
|
75
|
-
backends.each do |backend|
|
76
|
-
result = backend.localize(locale, object, format) and return result
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
protected
|
81
|
-
def backends
|
82
|
-
@backends ||= []
|
83
|
-
end
|
84
|
-
|
85
|
-
def default(locale, default, options = {})
|
86
|
-
case default
|
87
|
-
when String then default
|
88
|
-
when Symbol then translate locale, default, options
|
89
|
-
when Array then default.each do |obj|
|
90
|
-
result = default(locale, obj, options.dup) and return result
|
91
|
-
end and nil
|
92
|
-
end
|
93
|
-
rescue I18n::MissingTranslationData
|
94
|
-
nil
|
95
|
-
end
|
96
|
-
|
97
|
-
def namespace_lookup?(result, options)
|
98
|
-
result.is_a?(Hash) and not options.has_key?(:count)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'i18n/backend/simple'
|
2
|
-
|
3
|
-
module Globalize
|
4
|
-
module Backend
|
5
|
-
class Pluralizing < I18n::Backend::Simple
|
6
|
-
def pluralize(locale, entry, count)
|
7
|
-
return entry unless entry.is_a?(Hash) and count
|
8
|
-
key = :zero if count == 0 && entry.has_key?(:zero)
|
9
|
-
key ||= pluralizer(locale).call(count)
|
10
|
-
raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
|
11
|
-
translation entry[key], :plural_key => key
|
12
|
-
end
|
13
|
-
|
14
|
-
def add_pluralizer(locale, pluralizer)
|
15
|
-
pluralizers[locale.to_sym] = pluralizer
|
16
|
-
end
|
17
|
-
|
18
|
-
def pluralizer(locale)
|
19
|
-
pluralizers[locale.to_sym] || default_pluralizer
|
20
|
-
end
|
21
|
-
|
22
|
-
protected
|
23
|
-
def default_pluralizer
|
24
|
-
pluralizers[:en]
|
25
|
-
end
|
26
|
-
|
27
|
-
def pluralizers
|
28
|
-
@pluralizers ||= { :en => lambda{|n| n == 1 ? :one : :other } }
|
29
|
-
end
|
30
|
-
|
31
|
-
# Overwrite this method to return something other than a String
|
32
|
-
def translation(string, attributes)
|
33
|
-
string
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|