gettext_simple_rails 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dba110af5f0b0ba57c1a1897886553e0bf40120e
4
- data.tar.gz: 18d584e1d49fc3e29335637a32ec1d589aa3a012
3
+ metadata.gz: 9d00b567f8c0f4de3190531fe8432c568e5aa26b
4
+ data.tar.gz: 415e870fcd1c235bf50e63cca42cbaa386ee0683
5
5
  SHA512:
6
- metadata.gz: 6f77d5c43e56e22aed004138502a129175af697a544b74add757ede7d30af48940d0d6fd57ab26b27960096cb337e07d6138a00fc878377bb796e1a14570a01a
7
- data.tar.gz: 3f73cf94710505a17dd827e9ab26e2ffb6f7bfef50a1f234f4d9aeab2bf19af05db9392dc41f39439b30f059fbe80125023e640355e3c51df6c804ed21998105
6
+ metadata.gz: be227b770df8458a6cda081c1f755c5e63fbf994d30a61535d83e7b702b668b9acbca46483379ea36307b76b191ae068c2171611ae16d942a3aaf2a3f3922707
7
+ data.tar.gz: c268b48020bde34b23c0d6b147f53f372a61fd11f4e54a8ee918ebfb966703e7e2c57047d1a20c7541f6d4a195438c0db0597692074725f257d6ee5141057e25
@@ -4,14 +4,74 @@ class GettextSimpleRails::Translators::ActiveRecordTranslator
4
4
  end
5
5
 
6
6
  def translations
7
- return {
7
+ @translations_hash = {
8
8
  "activerecord" => {
9
9
  "errors" => {
10
10
  "messages" => {
11
11
  "record_invalid" => "Invalid record"
12
- }
12
+ },
13
+ "models" => {}
13
14
  }
14
15
  }
15
16
  }
17
+
18
+ scan_validation_errors
19
+
20
+ return @translations_hash
21
+ end
22
+
23
+ private
24
+
25
+ def scan_validation_errors
26
+ GettextSimpleRails::ModelInspector.model_classes do |inspector|
27
+ clazz_snake_name = StringCases.camel_to_snake(inspector.clazz.name)
28
+ @translations_hash["activerecord"]["errors"]["models"][clazz_snake_name] = {"attributes" => {}} unless @translations_hash["activerecord"]["errors"]["models"].key?(clazz_snake_name)
29
+ attributes_hash = @translations_hash["activerecord"]["errors"]["models"][clazz_snake_name]["attributes"]
30
+
31
+ inspector.clazz._validators.each do |attribute_name, validators|
32
+ validators.each do |validator|
33
+ attributes_hash[attribute_name] = {} unless attributes_hash.key?(attribute_name)
34
+ attribute_hash = attributes_hash[attribute_name]
35
+
36
+ if validator.is_a?(ActiveModel::Validations::LengthValidator)
37
+ translations_for_length_validator(validator, attribute_hash)
38
+ elsif validator.is_a?(ActiveModel::Validations::FormatValidator)
39
+ translations_for_format_validator(validator, attribute_hash)
40
+ elsif validator.is_a?(ActiveRecord::Validations::UniquenessValidator)
41
+ translations_for_uniqueness_validator(validator, attribute_hash)
42
+ elsif validator.is_a?(ActiveRecord::Validations::PresenceValidator)
43
+ translations_for_presence_Validator(validator, attribute_hash)
44
+ else
45
+ puts "Unknown validator: #{validator.class.name}"
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ def translations_for_length_validator(validator, attribute_hash)
53
+ if validator.options[:minimum].present?
54
+ attribute_hash["too_short"] = "is too short. The minimum is %{count}"
55
+ end
56
+
57
+ if validator.options[:maximum].present?
58
+ attribute_hash["too_long"] = "is too long. The maximum is %{count}"
59
+ end
60
+
61
+ if validator.options[:is].present?
62
+ attribute_hash["wrong_length"] = "is not the correct length: %{count}"
63
+ end
64
+ end
65
+
66
+ def translations_for_format_validator(validator, attribute_hash)
67
+ attribute_hash["invalid"] = "is invalid"
68
+ end
69
+
70
+ def translations_for_uniqueness_validator(validator, attribute_hash)
71
+ attribute_hash["taken"] = "has already been taken"
72
+ end
73
+
74
+ def translations_for_presence_Validator(validator, attribute_hash)
75
+ attribute_hash["blank"] = "cannot be blank"
16
76
  end
17
77
  end
@@ -1,3 +1,3 @@
1
1
  module GettextSimpleRails
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+
3
+ describe GettextSimpleRails::Translators::ActiveRecordTranslator do
4
+ before do
5
+ if !$translations_generated_for_date_translator
6
+ $translations_generated_for_date_translator = true
7
+
8
+ # Make it possible to call the Rake task.
9
+ ::Dummy::Application.load_tasks
10
+
11
+ # Clean up any existing translations.
12
+ FileUtils.rm_r(GettextSimpleRails.translation_dir) if File.exists?(GettextSimpleRails.translation_dir)
13
+
14
+ # Generate model translations so we can check them.
15
+ ::Rake::Task["gettext_simple_rails:generate_translator_files"].invoke
16
+ end
17
+ end
18
+
19
+ it "should generate translations for validations" do
20
+ filepath = "#{GettextSimpleRails.translation_dir}/active_record_translator_translations.rb"
21
+ cont = File.read(filepath)
22
+ cont.should include "_('activerecord.errors.models.user.attributes.name.too_short')"
23
+ cont.should include "_('activerecord.errors.models.user.attributes.name.invalid')"
24
+ cont.should include "_('activerecord.errors.models.user.attributes.name.blank')"
25
+ cont.should include "_('activerecord.errors.models.user.attributes.name.taken')"
26
+ cont.should include "_('activerecord.errors.models.user.attributes.name.invalid')"
27
+ end
28
+ end
@@ -1,3 +1,7 @@
1
1
  class User < ActiveRecord::Base
2
2
  has_many :roles
3
+
4
+ validates :name, :length => {:in => 2..255}
5
+ validates :name, :uniqueness => true, :presence => true
6
+ validates :name, :format => {:with => /\A[A-z]+\Z/}
3
7
  end
@@ -0,0 +1,16 @@
1
+ class GettextSimpleRails::MonthNames
2
+ def translations
3
+ #. Default value: Invalid record
4
+ _('activerecord.errors.messages.record_invalid')
5
+ #. Default value: is too short. The minimum is %{count}
6
+ _('activerecord.errors.models.user.attributes.name.too_short')
7
+ #. Default value: is too long. The maximum is %{count}
8
+ _('activerecord.errors.models.user.attributes.name.too_long')
9
+ #. Default value: has already been taken
10
+ _('activerecord.errors.models.user.attributes.name.taken')
11
+ #. Default value: cannot be blank
12
+ _('activerecord.errors.models.user.attributes.name.blank')
13
+ #. Default value: is invalid
14
+ _('activerecord.errors.models.user.attributes.name.invalid')
15
+ end
16
+ end
@@ -0,0 +1,60 @@
1
+ class GettextSimpleRails::MonthNames
2
+ def translations
3
+ #. Default value: %Y-%m-%d
4
+ _('date.formats.default')
5
+ #. Default value: %b %d
6
+ _('date.formats.short')
7
+ #. Default value: %B %d, %Y
8
+ _('date.formats.long')
9
+ _('date.day_names.0')
10
+ _('date.day_names.1')
11
+ _('date.day_names.2')
12
+ _('date.day_names.3')
13
+ _('date.day_names.4')
14
+ _('date.day_names.5')
15
+ _('date.day_names.6')
16
+ _('date.abbr_day_names.0')
17
+ _('date.abbr_day_names.1')
18
+ _('date.abbr_day_names.2')
19
+ _('date.abbr_day_names.3')
20
+ _('date.abbr_day_names.4')
21
+ _('date.abbr_day_names.5')
22
+ _('date.abbr_day_names.6')
23
+ _('date.month_names.0')
24
+ _('date.month_names.1')
25
+ _('date.month_names.2')
26
+ _('date.month_names.3')
27
+ _('date.month_names.4')
28
+ _('date.month_names.5')
29
+ _('date.month_names.6')
30
+ _('date.month_names.7')
31
+ _('date.month_names.8')
32
+ _('date.month_names.9')
33
+ _('date.month_names.10')
34
+ _('date.month_names.11')
35
+ _('date.month_names.12')
36
+ _('date.abbr_month_names.0')
37
+ _('date.abbr_month_names.1')
38
+ _('date.abbr_month_names.2')
39
+ _('date.abbr_month_names.3')
40
+ _('date.abbr_month_names.4')
41
+ _('date.abbr_month_names.5')
42
+ _('date.abbr_month_names.6')
43
+ _('date.abbr_month_names.7')
44
+ _('date.abbr_month_names.8')
45
+ _('date.abbr_month_names.9')
46
+ _('date.abbr_month_names.10')
47
+ _('date.abbr_month_names.11')
48
+ _('date.abbr_month_names.12')
49
+ #. Default value: %a, %d %b %Y %H:%M:%S %z
50
+ _('time.formats.default')
51
+ #. Default value: %d %b %H:%M
52
+ _('time.formats.short')
53
+ #. Default value: %B %d, %Y %H:%M
54
+ _('time.formats.long')
55
+ #. Default value: am
56
+ _('time.am')
57
+ #. Default value: pm
58
+ _('time.pm')
59
+ end
60
+ end
@@ -0,0 +1,48 @@
1
+ class GettextSimpleRails::MonthNames
2
+ def translations
3
+ #. Default value: ,
4
+ _('number.currency.format.delimiter')
5
+ #. Default value: %n %u
6
+ _('number.currency.format.format')
7
+ #. Default value: .
8
+ _('number.currency.format.separator')
9
+ #. Default value: $
10
+ _('number.currency.format.unit')
11
+ #. Default value: ,
12
+ _('number.format.delimiter')
13
+ #. Default value: .
14
+ _('number.format.separator')
15
+ #. Default value: %n %u
16
+ _('number.human.decimal_units.format')
17
+ #. Default value: Billion
18
+ _('number.human.decimal_units.units.billion')
19
+ #. Default value: Million
20
+ _('number.human.decimal_units.units.million')
21
+ #. Default value: Quadrillion
22
+ _('number.human.decimal_units.units.quadrillion')
23
+ #. Default value: Thousand
24
+ _('number.human.decimal_units.units.thousand')
25
+ #. Default value: Trillion
26
+ _('number.human.decimal_units.units.trillion')
27
+ #. Default value:
28
+ _('number.human.decimal_units.units.unit')
29
+ #. Default value:
30
+ _('number.human.format.delimiter')
31
+ #. Default value: %n %u
32
+ _('number.human.storage_units.format')
33
+ #. Default value: Byte
34
+ _('number.human.storage_units.units.byte.one')
35
+ #. Default value: Bytes
36
+ _('number.human.storage_units.units.byte.other')
37
+ #. Default value: GB
38
+ _('number.human.storage_units.units.gb')
39
+ #. Default value: KB
40
+ _('number.human.storage_units.units.kb')
41
+ #. Default value: MB
42
+ _('number.human.storage_units.units.mb')
43
+ #. Default value: TB
44
+ _('number.human.storage_units.units.tb')
45
+ #. Default value:
46
+ _('number.percentage.format.delimiter')
47
+ end
48
+ end
@@ -315,3 +315,18 @@
315
315
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
316
316
   (0.1ms) begin transaction
317
317
   (0.1ms) rollback transaction
318
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
319
+  (0.1ms) begin transaction
320
+  (0.1ms) rollback transaction
321
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
322
+  (0.1ms) begin transaction
323
+  (0.1ms) rollback transaction
324
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
325
+  (0.1ms) begin transaction
326
+  (0.1ms) rollback transaction
327
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
328
+  (0.1ms) begin transaction
329
+  (0.1ms) rollback transaction
330
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
331
+  (0.1ms) begin transaction
332
+  (0.1ms) rollback transaction
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gettext_simple_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Johansen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-09 00:00:00.000000000 Z
11
+ date: 2014-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - '>='
32
32
  - !ruby/object:Gem::Version
33
- version: 0.0.8
33
+ version: 0.0.9
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
- version: 0.0.8
40
+ version: 0.0.9
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: string-cases
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -114,8 +114,9 @@ files:
114
114
  - spec/dummy/config.ru
115
115
  - spec/dummy/README.rdoc
116
116
  - spec/dummy/Rakefile
117
- - spec/dummy/lib/gettext_simple_rails/models/user_model_translations.rb
118
- - spec/dummy/lib/gettext_simple_rails/models/role_model_translations.rb
117
+ - spec/dummy/lib/gettext_simple_rails/date_translator_translations.rb
118
+ - spec/dummy/lib/gettext_simple_rails/active_record_translator_translations.rb
119
+ - spec/dummy/lib/gettext_simple_rails/number_translator_translations.rb
119
120
  - spec/dummy/test/models/user_test.rb
120
121
  - spec/dummy/test/models/role_test.rb
121
122
  - spec/dummy/test/fixtures/users.yml
@@ -159,6 +160,7 @@ files:
159
160
  - spec/gettext_simple_rails_spec.rb
160
161
  - spec/spec_helper.rb
161
162
  - spec/date_translator_spec.rb
163
+ - spec/active_record_spec.rb
162
164
  homepage: http://www.github.com/kaspernj/gettext_simple_rails
163
165
  licenses:
164
166
  - MIT
@@ -190,8 +192,9 @@ test_files:
190
192
  - spec/dummy/config.ru
191
193
  - spec/dummy/README.rdoc
192
194
  - spec/dummy/Rakefile
193
- - spec/dummy/lib/gettext_simple_rails/models/user_model_translations.rb
194
- - spec/dummy/lib/gettext_simple_rails/models/role_model_translations.rb
195
+ - spec/dummy/lib/gettext_simple_rails/date_translator_translations.rb
196
+ - spec/dummy/lib/gettext_simple_rails/active_record_translator_translations.rb
197
+ - spec/dummy/lib/gettext_simple_rails/number_translator_translations.rb
195
198
  - spec/dummy/test/models/user_test.rb
196
199
  - spec/dummy/test/models/role_test.rb
197
200
  - spec/dummy/test/fixtures/users.yml
@@ -235,3 +238,4 @@ test_files:
235
238
  - spec/gettext_simple_rails_spec.rb
236
239
  - spec/spec_helper.rb
237
240
  - spec/date_translator_spec.rb
241
+ - spec/active_record_spec.rb
@@ -1,21 +0,0 @@
1
- class GettextSimpleRails::UserModelTranslations
2
- def self.attribute_translations
3
- puts _('models.attributes.role.id')
4
- puts _('models.attributes.role.role')
5
- puts _('models.attributes.role.user_id')
6
- puts _('models.attributes.role.created_at')
7
- puts _('models.attributes.role.updated_at')
8
- end
9
-
10
- def self.relationship_translations
11
- puts _('models.attributes.role.user')
12
- end
13
-
14
- def self.paperclip_attachments
15
- end
16
-
17
- def self.model_name
18
- puts _('models.name.role.one')
19
- puts _('models.name.role.other')
20
- end
21
- end
@@ -1,22 +0,0 @@
1
- class GettextSimpleRails::UserModelTranslations
2
- def self.attribute_translations
3
- puts _('models.attributes.user.id')
4
- puts _('models.attributes.user.name')
5
- puts _('models.attributes.user.birthday_at')
6
- puts _('models.attributes.user.age')
7
- puts _('models.attributes.user.created_at')
8
- puts _('models.attributes.user.updated_at')
9
- end
10
-
11
- def self.relationship_translations
12
- puts _('models.attributes.user.roles')
13
- end
14
-
15
- def self.paperclip_attachments
16
- end
17
-
18
- def self.model_name
19
- puts _('models.name.user.one')
20
- puts _('models.name.user.other')
21
- end
22
- end