emipair-merb_global 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +9 -0
- data/CONTRIBUTORS +6 -0
- data/HISTORY +34 -0
- data/LICENSE +21 -0
- data/README +4 -0
- data/README.markdown +179 -0
- data/Rakefile +65 -0
- data/TODO +4 -0
- data/VERSION +1 -0
- data/activerecord_generators/translations_migration/USAGE +4 -0
- data/activerecord_generators/translations_migration/templates/translations_migration.erb +30 -0
- data/activerecord_generators/translations_migration/translations_migration_generator.rb +31 -0
- data/benchmarks/database1.rb +174 -0
- data/examples/active_record_example/README.txt +9 -0
- data/examples/active_record_example/application.rb +5 -0
- data/examples/active_record_example/config/database.yml +9 -0
- data/examples/active_record_example/config/framework.rb +5 -0
- data/examples/active_record_example/config/init.rb +24 -0
- data/examples/active_record_example/config/plugins.yml +3 -0
- data/examples/data_mapper_example/README.txt +9 -0
- data/examples/data_mapper_example/application.rb +5 -0
- data/examples/data_mapper_example/config/database.yml +9 -0
- data/examples/data_mapper_example/config/framework.rb +5 -0
- data/examples/data_mapper_example/config/init.rb +24 -0
- data/examples/data_mapper_example/config/plugins.yml +3 -0
- data/examples/database.sql +28 -0
- data/examples/gettext_example/README.txt +9 -0
- data/examples/gettext_example/application.rb +8 -0
- data/examples/gettext_example/config/framework.rb +5 -0
- data/examples/gettext_example/config/init.rb +24 -0
- data/examples/gettext_example/config/plugins.yml +4 -0
- data/examples/gettext_example/locale/merbapp.pot +21 -0
- data/examples/gettext_example/locale/pl/LC_MESSAGES/merbapp.mo +0 -0
- data/examples/gettext_example/locale/pl.po +23 -0
- data/examples/mock_example/README.txt +9 -0
- data/examples/mock_example/application.rb +5 -0
- data/examples/mock_example/config/framework.rb +5 -0
- data/examples/mock_example/config/init.rb +23 -0
- data/examples/mock_example/config/plugins.yml +3 -0
- data/examples/sequel_example/README.txt +9 -0
- data/examples/sequel_example/application.rb +5 -0
- data/examples/sequel_example/config/database.yml +9 -0
- data/examples/sequel_example/config/framework.rb +5 -0
- data/examples/sequel_example/config/init.rb +24 -0
- data/examples/sequel_example/config/plugins.yml +3 -0
- data/examples/yaml_example/README.txt +9 -0
- data/examples/yaml_example/application.rb +5 -0
- data/examples/yaml_example/config/framework.rb +5 -0
- data/examples/yaml_example/config/init.rb +24 -0
- data/examples/yaml_example/config/plugins.yml +4 -0
- data/examples/yaml_example/locale/en.yaml +2 -0
- data/examples/yaml_example/locale/pl.yaml +2 -0
- data/lib/merb_global/base.rb +157 -0
- data/lib/merb_global/config.rb +36 -0
- data/lib/merb_global/controller.rb +40 -0
- data/lib/merb_global/date_providers/fork.rb +37 -0
- data/lib/merb_global/date_providers.rb +47 -0
- data/lib/merb_global/locale.rb +164 -0
- data/lib/merb_global/merbrake.rb +37 -0
- data/lib/merb_global/message_providers/active_record.rb +113 -0
- data/lib/merb_global/message_providers/data_mapper.rb +113 -0
- data/lib/merb_global/message_providers/gettext.rb +125 -0
- data/lib/merb_global/message_providers/gettext.treetop +60 -0
- data/lib/merb_global/message_providers/mock.rb +17 -0
- data/lib/merb_global/message_providers/sequel.rb +99 -0
- data/lib/merb_global/message_providers/yaml.rb +105 -0
- data/lib/merb_global/message_providers.rb +146 -0
- data/lib/merb_global/numeric_providers/fork.rb +37 -0
- data/lib/merb_global/numeric_providers/java.rb +15 -0
- data/lib/merb_global/numeric_providers.rb +48 -0
- data/lib/merb_global/plural.rb +20 -0
- data/lib/merb_global/plural.treetop +267 -0
- data/lib/merb_global/providers.rb +40 -0
- data/lib/merb_global.rb +8 -0
- data/sequel_generators/translations_migration/USAGE +4 -0
- data/sequel_generators/translations_migration/templates/translations_migration.erb +28 -0
- data/sequel_generators/translations_migration/translations_migration_generator.rb +32 -0
- data/spec/base_spec.rb +124 -0
- data/spec/controller_spec.rb +95 -0
- data/spec/date_providers_spec.rb +82 -0
- data/spec/locale/merbapp.pot +26 -0
- data/spec/locale/pl/LC_MESSAGES/merbapp.mo +0 -0
- data/spec/locale/pl.po +27 -0
- data/spec/locale/pl.yaml +8 -0
- data/spec/message_providers/active_record_spec.rb +192 -0
- data/spec/message_providers/data_mapper_spec.rb +124 -0
- data/spec/message_providers/gettext_spec.rb +77 -0
- data/spec/message_providers/mock_spec.rb +27 -0
- data/spec/message_providers/sequel_spec.rb +185 -0
- data/spec/message_providers/yaml_spec.rb +64 -0
- data/spec/message_providers_spec.rb +186 -0
- data/spec/numeric_providers_spec.rb +82 -0
- data/spec/plural_spec.rb +23 -0
- data/spec/providers_spec.rb +60 -0
- data/spec/spec_helper.rb +36 -0
- metadata +183 -0
@@ -0,0 +1,26 @@
|
|
1
|
+
# Test package.
|
2
|
+
# Copyright (C) 2008
|
3
|
+
# This file is distributed under the same license as the merb_global package.
|
4
|
+
# Maciej Piechotka <uzytkownik2@gmail.com>, 2008.
|
5
|
+
#
|
6
|
+
#, fuzzy
|
7
|
+
msgid ""
|
8
|
+
msgstr ""
|
9
|
+
"Project-Id-Version: 0.0.1\n"
|
10
|
+
"POT-Creation-Date: 2008-05-16 20:47+0200\n"
|
11
|
+
"PO-Revision-Date: 2008-05-16 20:47+0200\n"
|
12
|
+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
13
|
+
"Language-Team: LANGUAGE <LL@li.org>\n"
|
14
|
+
"MIME-Version: 1.0\n"
|
15
|
+
"Content-Type: text/plain; charset=UTF-8\n"
|
16
|
+
"Content-Transfer-Encoding: 8bit\n"
|
17
|
+
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
|
18
|
+
|
19
|
+
#: test.c:4
|
20
|
+
msgid "Test"
|
21
|
+
msgid_plural "Tests"
|
22
|
+
msgstr[0] ""
|
23
|
+
msgstr[1] ""
|
24
|
+
|
25
|
+
msgid "Hello"
|
26
|
+
msgstr ""
|
Binary file
|
data/spec/locale/pl.po
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Test package.
|
2
|
+
# Copyright (C) 2008
|
3
|
+
# This file is distributed under the same license as the merb_global package.
|
4
|
+
# Maciej Piechotka <uzytkownik2@gmail.com>, 2008.
|
5
|
+
#
|
6
|
+
msgid ""
|
7
|
+
msgstr ""
|
8
|
+
"Project-Id-Version: 0.0.1\n"
|
9
|
+
"POT-Creation-Date: 2008-05-16 20:47+0200\n"
|
10
|
+
"PO-Revision-Date: 2008-05-24 13:17+0100\n"
|
11
|
+
"Last-Translator: Maciej Piechotka <uzytkownik2@gmail.com>\n"
|
12
|
+
"Language-Team: Polish\n"
|
13
|
+
"MIME-Version: 1.0\n"
|
14
|
+
"Content-Type: text/plain; charset=UTF-8\n"
|
15
|
+
"Content-Transfer-Encoding: 8bit\n"
|
16
|
+
"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
|
17
|
+
|
18
|
+
#: test.c:4
|
19
|
+
msgid "Test"
|
20
|
+
msgid_plural "Tests"
|
21
|
+
msgstr[0] "Test"
|
22
|
+
msgstr[1] "Testy"
|
23
|
+
msgstr[2] "Testów"
|
24
|
+
|
25
|
+
msgid "Hello"
|
26
|
+
msgstr "Cześć"
|
27
|
+
|
data/spec/locale/pl.yaml
ADDED
@@ -0,0 +1,192 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if HAS_AR
|
4
|
+
|
5
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',
|
6
|
+
:database => ':memory:')
|
7
|
+
ActiveRecord::Migration.verbose = false
|
8
|
+
|
9
|
+
require 'merb_global/message_providers/active_record'
|
10
|
+
|
11
|
+
load Pathname(__FILE__).dirname.parent.parent.expand_path +
|
12
|
+
'activerecord_generators/translations_migration/templates/' +
|
13
|
+
'translations_migration.erb'
|
14
|
+
|
15
|
+
describe Merb::Global::MessageProviders::ActiveRecord::AddTranslationsMigration do
|
16
|
+
before do
|
17
|
+
@migration =
|
18
|
+
Merb::Global::MessageProviders::ActiveRecord::AddTranslationsMigration
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '.up' do
|
22
|
+
after do
|
23
|
+
@migration.down
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should run the migration' do
|
27
|
+
@migration.up
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '.down' do
|
32
|
+
before do
|
33
|
+
@migration.up
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should revert the migration' do
|
37
|
+
@migration.down
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe Merb::Global::MessageProviders::ActiveRecord do
|
43
|
+
before do
|
44
|
+
@provider = Merb::Global::MessageProviders::ActiveRecord.new
|
45
|
+
@migration =
|
46
|
+
Merb::Global::MessageProviders::ActiveRecord::AddTranslationsMigration
|
47
|
+
@migration.up
|
48
|
+
end
|
49
|
+
|
50
|
+
after do
|
51
|
+
@migration.down
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '.create!' do
|
55
|
+
it 'should check if migration exists and print message if yes' do
|
56
|
+
file = mock do |file|
|
57
|
+
file.expects(:=~).with(/translations\.rb/).returns(true)
|
58
|
+
end
|
59
|
+
dir = File.join Merb.root, 'schema', 'migrations', '*.rb'
|
60
|
+
dir_mock = mock do |dir_mock|
|
61
|
+
dir_mock.expects(:detect).yields(file).returns(true)
|
62
|
+
end
|
63
|
+
Merb::Global::MessageProviders::ActiveRecord::Dir = mock do |dir_class|
|
64
|
+
dir_class.expects(:[]).with(dir).returns(dir_mock)
|
65
|
+
end
|
66
|
+
@provider.expects(:puts)
|
67
|
+
@provider.create!
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should run the script if migration exists' do
|
71
|
+
file = mock do |file|
|
72
|
+
file.expects(:=~).with(/translations\.rb/).returns(true)
|
73
|
+
end
|
74
|
+
dir = File.join Merb.root, 'schema', 'migrations', '*.rb'
|
75
|
+
dir_mock = mock do |dir_mock|
|
76
|
+
dir_mock.expects(:detect).yields(file).returns(false)
|
77
|
+
end
|
78
|
+
Merb::Global::MessageProviders::ActiveRecord::Dir = mock do |dir_class|
|
79
|
+
dir_class.stubs(:[]).with(dir).returns(dir_mock)
|
80
|
+
end
|
81
|
+
@provider.expects(:sh).with(%{merb-gen translations_migration})
|
82
|
+
@provider.create!
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe '.localize' do
|
87
|
+
before do
|
88
|
+
lang = Merb::Global::MessageProviders::ActiveRecord::Language
|
89
|
+
trans = Merb::Global::MessageProviders::ActiveRecord::Translation
|
90
|
+
en = lang.create! :name => 'en', :nplural => 2, :plural => 'n==1?0:1'
|
91
|
+
trans.create! :language_id => en.id,
|
92
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
93
|
+
:msgstr => 'One test', :msgstr_index => 0
|
94
|
+
trans.create! :language_id => en.id,
|
95
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
96
|
+
:msgstr => 'Many tests', :msgstr_index => 1
|
97
|
+
trans.create! :language_id => en.id,
|
98
|
+
:msgid => 'Hello', :msgid_plural => nil,
|
99
|
+
:msgstr => 'Hello world!', :msgstr_index => nil
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'should find it in database and return proper translation' do
|
103
|
+
en = Merb::Global::Locale.new('en')
|
104
|
+
trans = @provider.localize 'Test', 'Tests', 1, en
|
105
|
+
trans.should == 'One test'
|
106
|
+
trans = @provider.localize 'Test', 'Tests', 2, en
|
107
|
+
trans.should == 'Many tests'
|
108
|
+
trans = @provider.localize 'Hello', nil, 1, en
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'should fallback if not' do
|
112
|
+
fr = Merb::Global::Locale.new('fr')
|
113
|
+
en = Merb::Global::Locale.new('en')
|
114
|
+
trans = @provider.localize 'Test', 'Tests', 1, fr
|
115
|
+
trans.should == 'Test'
|
116
|
+
trans = @provider.localize 'Car', 'Cars', 2, en
|
117
|
+
trans.should == 'Cars'
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe '.import' do
|
122
|
+
before do
|
123
|
+
lang = Merb::Global::MessageProviders::ActiveRecord::Language
|
124
|
+
trans = Merb::Global::MessageProviders::ActiveRecord::Translation
|
125
|
+
en = lang.create! :name => 'en', :nplural => 2, :plural => 'n==1?0:1'
|
126
|
+
trans.create! :language_id => en.id,
|
127
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
128
|
+
:msgstr => 'One test', :msgstr_index => 0
|
129
|
+
trans.create! :language_id => en.id,
|
130
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
131
|
+
:msgstr => 'Many tests', :msgstr_index => 1
|
132
|
+
trans.create! :language_id => en.id,
|
133
|
+
:msgid => 'Hello', :msgid_plural => nil,
|
134
|
+
:msgstr => 'Hello world!', :msgstr_index => nil
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'should put data in the hash' do
|
138
|
+
@provider.import.should == {
|
139
|
+
"en" => {
|
140
|
+
:nplural => 2, :plural => 'n==1?0:1',
|
141
|
+
'Hello' => {
|
142
|
+
:plural => nil,
|
143
|
+
nil => 'Hello world!'
|
144
|
+
},
|
145
|
+
'Test' => {
|
146
|
+
:plural => 'Tests',
|
147
|
+
0 => 'One test',
|
148
|
+
1 => 'Many tests'
|
149
|
+
}
|
150
|
+
}
|
151
|
+
}
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe '.export' do
|
156
|
+
it 'should transform data from hash into the database' do
|
157
|
+
lang = Merb::Global::MessageProviders::ActiveRecord::Language
|
158
|
+
trans = Merb::Global::MessageProviders::ActiveRecord::Translation
|
159
|
+
en = mock do |en|
|
160
|
+
en.stubs(:id).returns(1)
|
161
|
+
end
|
162
|
+
lang.expects(:create!).
|
163
|
+
with(:name => 'en', :nplural => 2, :plural => 'n==1?0:1').
|
164
|
+
returns(en)
|
165
|
+
trans.expects(:create!).
|
166
|
+
with(:language_id => en.id,
|
167
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
168
|
+
:msgstr => 'One test', :msgstr_index => 0)
|
169
|
+
trans.expects(:create!).
|
170
|
+
with(:language_id => en.id,
|
171
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
172
|
+
:msgstr => 'Many tests', :msgstr_index => 1)
|
173
|
+
trans.expects(:create!).
|
174
|
+
with(:language_id => en.id,
|
175
|
+
:msgid => 'Hello', :msgid_plural => nil,
|
176
|
+
:msgstr => 'Hello world!', :msgstr_index => nil)
|
177
|
+
@provider.export("en" => {
|
178
|
+
:nplural => 2, :plural => 'n==1?0:1',
|
179
|
+
'Hello' => {
|
180
|
+
:plural => nil,
|
181
|
+
nil => 'Hello world!'
|
182
|
+
},
|
183
|
+
'Test' => {
|
184
|
+
:plural => 'Tests',
|
185
|
+
0 => 'One test',
|
186
|
+
1 => 'Many tests'
|
187
|
+
}
|
188
|
+
})
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if HAS_DM
|
4
|
+
DataMapper.setup :default, 'sqlite3::memory:'
|
5
|
+
|
6
|
+
require 'merb_global/message_providers/data_mapper'
|
7
|
+
|
8
|
+
describe Merb::Global::MessageProviders::DataMapper do
|
9
|
+
before do
|
10
|
+
@provider = Merb::Global::MessageProviders::DataMapper.new
|
11
|
+
# Quick'n'dirty hack - to change in future
|
12
|
+
@provider.create!
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '.create!' do
|
16
|
+
it 'should call automigrate' do
|
17
|
+
lambda {@provider.create!}.should_not raise_error
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '.localize' do
|
22
|
+
before do
|
23
|
+
lang = Merb::Global::MessageProviders::DataMapper::Language
|
24
|
+
en = lang.create! :name => 'en', :plural => 'n==1?0:1', :nplural => 2
|
25
|
+
trans = Merb::Global::MessageProviders::DataMapper::Translation
|
26
|
+
trans.create! :language_id => en.id,
|
27
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
28
|
+
:msgstr => 'One test', :msgstr_index => 0
|
29
|
+
trans.create! :language_id => en.id,
|
30
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
31
|
+
:msgstr => 'Many tests', :msgstr_index => 1
|
32
|
+
trans.create! :language_id => en.id,
|
33
|
+
:msgid => 'Hello', :msgid_plural => nil,
|
34
|
+
:msgstr => 'Hello world!', :msgstr_index => nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should fetch the correct translation from database if avaible' do
|
38
|
+
trans = @provider.localize('Test', 'Tests', 1, 'en')
|
39
|
+
trans.should == 'One test'
|
40
|
+
trans = @provider.localize('Hello', nil, 1, 'en')
|
41
|
+
trans.should == 'Hello world!'
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should fallback to default if needed' do
|
45
|
+
trans = @provider.localize('Test', 'Tests', 2, 'fr')
|
46
|
+
trans.should == 'Tests'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '.import' do
|
51
|
+
before do
|
52
|
+
lang = Merb::Global::MessageProviders::DataMapper::Language
|
53
|
+
en = lang.create! :name => 'en', :plural => 'n==1?0:1', :nplural => 2
|
54
|
+
trans = Merb::Global::MessageProviders::DataMapper::Translation
|
55
|
+
trans.create! :language_id => en.id,
|
56
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
57
|
+
:msgstr => 'One test', :msgstr_index => 0
|
58
|
+
trans.create! :language_id => en.id,
|
59
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
60
|
+
:msgstr => 'Many tests', :msgstr_index => 1
|
61
|
+
trans.create! :language_id => en.id,
|
62
|
+
:msgid => 'Hello', :msgid_plural => nil,
|
63
|
+
:msgstr => 'Hello world!', :msgstr_index => nil
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should put data in the hash' do
|
67
|
+
@provider.import.should == {
|
68
|
+
"en" => {
|
69
|
+
:nplural => 2, :plural => 'n==1?0:1',
|
70
|
+
'Hello' => {
|
71
|
+
:plural => nil,
|
72
|
+
nil => 'Hello world!'
|
73
|
+
},
|
74
|
+
'Test' => {
|
75
|
+
:plural => 'Tests',
|
76
|
+
0 => 'One test',
|
77
|
+
1 => 'Many tests'
|
78
|
+
}
|
79
|
+
}
|
80
|
+
}
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '.export' do
|
85
|
+
it 'should transform data from hash into the database' do
|
86
|
+
lang = Merb::Global::MessageProviders::DataMapper::Language
|
87
|
+
trans = Merb::Global::MessageProviders::DataMapper::Translation
|
88
|
+
en = mock do |en|
|
89
|
+
en.stubs(:id).returns(1)
|
90
|
+
end
|
91
|
+
lang.expects(:create!).
|
92
|
+
with(:name => 'en', :nplural => 2, :plural => 'n==1?0:1').
|
93
|
+
returns(en)
|
94
|
+
trans.expects(:create!).
|
95
|
+
with(:language_id => en.id,
|
96
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
97
|
+
:msgstr => 'One test', :msgstr_index => 0).
|
98
|
+
returns(mock)
|
99
|
+
trans.expects(:create!).
|
100
|
+
with(:language_id => en.id,
|
101
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
102
|
+
:msgstr => 'Many tests', :msgstr_index => 1).
|
103
|
+
returns(mock)
|
104
|
+
trans.expects(:create!).
|
105
|
+
with(:language_id => en.id,
|
106
|
+
:msgid => 'Hello', :msgid_plural => nil,
|
107
|
+
:msgstr => 'Hello world!', :msgstr_index => nil).
|
108
|
+
returns(mock)
|
109
|
+
@provider.export("en" => {
|
110
|
+
:nplural => 2, :plural => 'n==1?0:1',
|
111
|
+
'Hello' => {
|
112
|
+
:plural => nil,
|
113
|
+
nil => 'Hello world!'
|
114
|
+
},
|
115
|
+
'Test' => {
|
116
|
+
:plural => 'Tests',
|
117
|
+
0 => 'One test',
|
118
|
+
1 => 'Many tests'
|
119
|
+
}
|
120
|
+
})
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
if HAS_GETTEXT
|
6
|
+
|
7
|
+
require 'merb_global/message_providers/gettext'
|
8
|
+
|
9
|
+
describe Merb::Global::MessageProviders::Gettext do
|
10
|
+
before do
|
11
|
+
@provider = Merb::Global::MessageProviders::Gettext.new
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '.create!' do
|
15
|
+
it 'should create directory' do
|
16
|
+
File.expects(:mkdirs).with(Merb::Global::MessageProviders.localedir)
|
17
|
+
@provider.create!
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '.localize' do
|
22
|
+
it 'should translate the string' do
|
23
|
+
pl = Merb::Global::Locale.new('pl')
|
24
|
+
trans = @provider.localize 'Test', 'Tests', 1, pl
|
25
|
+
trans.should == 'Test'
|
26
|
+
trans = @provider.localize 'Test', 'Tests', 2, pl
|
27
|
+
trans.should == 'Testy'
|
28
|
+
trans = @provider.localize 'Test', 'Tests', 5, pl
|
29
|
+
trans.should == 'Testów'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should fallback if not present' do
|
33
|
+
pl = Merb::Global::Locale.new('pl')
|
34
|
+
trans = @provider.localize 'Car', 'Cars', 1, pl
|
35
|
+
trans.should == 'Car'
|
36
|
+
trans = @provider.localize 'Car', 'Cars', 2, pl
|
37
|
+
trans.should == 'Cars'
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should fallback if language is not supported' do
|
41
|
+
fr = Merb::Global::Locale.new('fr')
|
42
|
+
trans = @provider.localize 'Test', 'Tests', 1, fr
|
43
|
+
trans.should == 'Test'
|
44
|
+
trans = @provider.localize 'Test', 'Tests', 2, fr
|
45
|
+
trans.should == 'Tests'
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should translate for singular only also' do
|
49
|
+
pl = Merb::Global::Locale.new('pl')
|
50
|
+
trans = @provider.localize('Hello', nil, 1, pl)
|
51
|
+
trans.should == 'Cześć'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '.import' do
|
56
|
+
it 'should put data in the hash' do
|
57
|
+
@provider.import.should == {
|
58
|
+
"pl" => {
|
59
|
+
:plural => "(n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2)",
|
60
|
+
:nplural => 3,
|
61
|
+
"Hello" => {:plural => nil, nil => "Cześć"},
|
62
|
+
"Test" => {
|
63
|
+
:plural => "Tests",
|
64
|
+
0 => "Test",
|
65
|
+
1 => "Testy",
|
66
|
+
2 => "Testów"
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe '.export' do
|
74
|
+
it 'should transform data from hash into po files'
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'merb_global/message_providers/mock'
|
3
|
+
|
4
|
+
describe Merb::Global::MessageProviders::Mock do
|
5
|
+
before do
|
6
|
+
@provider = Merb::Global::MessageProviders::Mock.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '.localize' do
|
10
|
+
it 'should return plural for n > 1' do
|
11
|
+
pl = Merb::Global::Locale.new(pl)
|
12
|
+
@provider.localize('test', 'tests', 2, pl).should == 'tests'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should return singular for n <= 1' do
|
16
|
+
pl = Merb::Global::Locale.new(pl)
|
17
|
+
@provider.localize('test', 'tests', 0, pl).should == 'test'
|
18
|
+
@provider.localize('test', 'tests', 1, pl).should == 'test'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '.create!' do
|
23
|
+
it 'should do nothing' do
|
24
|
+
@provider.create!.should be_nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if HAS_SEQUEL
|
4
|
+
|
5
|
+
DB = Sequel.open 'sqlite:///'
|
6
|
+
|
7
|
+
require 'merb_global/message_providers/sequel'
|
8
|
+
load Pathname(__FILE__).dirname.parent.parent.expand_path +
|
9
|
+
'sequel_generators/translations_migration/templates/' +
|
10
|
+
'translations_migration.erb'
|
11
|
+
|
12
|
+
describe Merb::Global::MessageProviders::Sequel::AddTranslationsMigration do
|
13
|
+
before do
|
14
|
+
migration =
|
15
|
+
Merb::Global::MessageProviders::Sequel::AddTranslationsMigration
|
16
|
+
@migration = migration.new DB
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '.up' do
|
20
|
+
it 'should migrate the database' do
|
21
|
+
@migration.up
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '.down' do
|
26
|
+
it 'should remove the effects' do
|
27
|
+
@migration.down
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe Merb::Global::MessageProviders::Sequel do
|
33
|
+
before do
|
34
|
+
@provider = Merb::Global::MessageProviders::Sequel.new
|
35
|
+
DB.disconnect
|
36
|
+
DB.connect ":memory"
|
37
|
+
migration =
|
38
|
+
Merb::Global::MessageProviders::Sequel::AddTranslationsMigration
|
39
|
+
migration.new(DB).up
|
40
|
+
end
|
41
|
+
|
42
|
+
after do
|
43
|
+
migration =
|
44
|
+
Merb::Global::MessageProviders::Sequel::AddTranslationsMigration
|
45
|
+
migration.new(DB).down
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '.create!' do
|
49
|
+
it 'should check if migration exists and print message if yes' do
|
50
|
+
file = mock do |file|
|
51
|
+
file.expects(:=~).with(/translations\.rb/).returns(true)
|
52
|
+
end
|
53
|
+
dir = File.join Merb.root, 'schema', 'migrations', '*.rb'
|
54
|
+
dir_mock = mock do |dir_mock|
|
55
|
+
dir_mock.expects(:detect).yields(file).returns(true)
|
56
|
+
end
|
57
|
+
Dir.stubs(:[]).returns([])
|
58
|
+
Dir.expects(:[]).with(dir).returns(dir_mock)
|
59
|
+
@provider.expects(:puts)
|
60
|
+
@provider.stubs(:sh)
|
61
|
+
@provider.create!
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should run the script if migration exists' do
|
65
|
+
file = mock do |file|
|
66
|
+
file.expects(:=~).with(/translations\.rb/).returns(true)
|
67
|
+
end
|
68
|
+
dir = File.join Merb.root, 'schema', 'migrations', '*.rb'
|
69
|
+
dir_mock = mock do |dir_mock|
|
70
|
+
dir_mock.expects(:detect).yields(file).returns(false)
|
71
|
+
end
|
72
|
+
Dir.expects(:[]).with(dir).returns(dir_mock)
|
73
|
+
@provider.expects(:sh).with(%{merb-gen translations_migration})
|
74
|
+
@provider.create!
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '.localize' do
|
79
|
+
before do
|
80
|
+
lang = Merb::Global::MessageProviders::Sequel::Language
|
81
|
+
trans = Merb::Global::MessageProviders::Sequel::Translation
|
82
|
+
en = lang.create :name => 'en', :plural => 'n==1?0:1'
|
83
|
+
trans.create :language_id => en.id,
|
84
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
85
|
+
:msgstr => 'One test', :msgstr_index => 0
|
86
|
+
trans.create :language_id => en.id,
|
87
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
88
|
+
:msgstr => 'Many tests', :msgstr_index => 1
|
89
|
+
trans.create :language_id => en.id,
|
90
|
+
:msgid => 'Hello', :msgid_plural => nil,
|
91
|
+
:msgstr => 'Hello world!', :msgstr_index => nil
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should find it in database and return proper translation' do
|
95
|
+
en = Merb::Global::Locale.new('en')
|
96
|
+
trans = @provider.localize 'Test', 'Tests', 1, en
|
97
|
+
trans.should == 'One test'
|
98
|
+
trans = @provider.localize 'Test', 'Tests', 2, en
|
99
|
+
trans.should == 'Many tests'
|
100
|
+
trans = @provider.localize 'Hello', nil, 1, en
|
101
|
+
trans.should == 'Hello world!'
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should fallback if not' do
|
105
|
+
fr = Merb::Global::Locale.new('fr')
|
106
|
+
en = Merb::Global::Locale.new('pl')
|
107
|
+
trans = @provider.localize 'Test', 'Tests', 1, fr
|
108
|
+
trans.should == 'Test'
|
109
|
+
trans = @provider.localize 'Car', 'Cars', 2, en
|
110
|
+
trans.should == 'Cars'
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe '.import' do
|
115
|
+
before do
|
116
|
+
lang = Merb::Global::MessageProviders::Sequel::Language
|
117
|
+
trans = Merb::Global::MessageProviders::Sequel::Translation
|
118
|
+
en = lang.create :name => 'en', :nplural => 2, :plural => 'n==1?0:1'
|
119
|
+
trans.create :language_id => en.id,
|
120
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
121
|
+
:msgstr => 'One test', :msgstr_index => 0
|
122
|
+
trans.create :language_id => en.id,
|
123
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
124
|
+
:msgstr => 'Many tests', :msgstr_index => 1
|
125
|
+
trans.create :language_id => en.id,
|
126
|
+
:msgid => 'Hello', :msgid_plural => nil,
|
127
|
+
:msgstr => 'Hello world!', :msgstr_index => nil
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'should put data in the hash' do
|
131
|
+
@provider.import.should == {
|
132
|
+
"en" => {
|
133
|
+
:nplural => 2, :plural => 'n==1?0:1',
|
134
|
+
'Hello' => {
|
135
|
+
:plural => nil,
|
136
|
+
nil => 'Hello world!'
|
137
|
+
},
|
138
|
+
'Test' => {
|
139
|
+
:plural => 'Tests',
|
140
|
+
0 => 'One test',
|
141
|
+
1 => 'Many tests'
|
142
|
+
}
|
143
|
+
}
|
144
|
+
}
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe '.export' do
|
149
|
+
it 'should transform data from hash into the database' do
|
150
|
+
lang = Merb::Global::MessageProviders::Sequel::Language
|
151
|
+
trans = Merb::Global::MessageProviders::Sequel::Translation
|
152
|
+
en = mock do |en|
|
153
|
+
en.stubs(:[]).with(:id).returns(1)
|
154
|
+
end
|
155
|
+
lang.expects(:create).
|
156
|
+
with(:name => 'en', :nplural => 2, :plural => 'n==1?0:1').
|
157
|
+
returns(en)
|
158
|
+
trans.expects(:create).
|
159
|
+
with(:language_id => en[:id],
|
160
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
161
|
+
:msgstr => 'One test', :msgstr_index => 0).returns(1)
|
162
|
+
trans.expects(:create).
|
163
|
+
with(:language_id => en[:id],
|
164
|
+
:msgid => 'Test', :msgid_plural => 'Tests',
|
165
|
+
:msgstr => 'Many tests', :msgstr_index => 1).returns(2)
|
166
|
+
trans.expects(:create).
|
167
|
+
with(:language_id => en[:id],
|
168
|
+
:msgid => 'Hello', :msgid_plural => nil,
|
169
|
+
:msgstr => 'Hello world!', :msgstr_index => nil).returns(3)
|
170
|
+
@provider.export("en" => {
|
171
|
+
:nplural => 2, :plural => 'n==1?0:1',
|
172
|
+
'Hello' => {
|
173
|
+
:plural => nil,
|
174
|
+
nil => 'Hello world!'
|
175
|
+
},
|
176
|
+
'Test' => {
|
177
|
+
:plural => 'Tests',
|
178
|
+
0 => 'One test',
|
179
|
+
1 => 'Many tests'
|
180
|
+
}
|
181
|
+
})
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|