l10n 0.0.10 → 0.0.11

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.
@@ -9,6 +9,7 @@ require 'l10n/i18n_extensions'
9
9
  require 'l10n/inflections'
10
10
  require 'l10n/numeric_column_conversions'
11
11
  require 'l10n/column_translation'
12
+ require 'l10n/translation_validator'
12
13
  require 'l10n/forms'
13
14
  require 'l10n/request'
14
15
 
@@ -63,10 +63,10 @@ module L10n
63
63
  translated_attributes.map(&:to_s).include?(attr_name.to_s)
64
64
  end
65
65
 
66
- def translate_column_name(column_name_t)
66
+ def translate_column_name(column_name_t, language_code = nil)
67
67
  name = column_name_t.to_s
68
68
  if name.ends_with?('_t') and translates?(name[0..-3])
69
- "#{name[0..-3]}#{I18n.translation_suffix}".to_sym
69
+ "#{name[0..-3]}#{I18n.translation_suffix(language_code)}".to_sym
70
70
  else
71
71
  column_name_t
72
72
  end
@@ -8,8 +8,8 @@ module L10n
8
8
 
9
9
  module ClassMethods
10
10
 
11
- def default?
12
- language_code == default_language_code
11
+ def default?(language_code = I18n.language_code)
12
+ language_code.to_s == default_language_code.to_s
13
13
  end
14
14
 
15
15
  def language_code
@@ -69,8 +69,10 @@ module L10n
69
69
  available_language_codes.inject({}) { |hash, code| hash[code] = I18n.as(code) { I18n.t(key) }; hash }
70
70
  end
71
71
 
72
- def translation_suffix
73
- default? ? '' : "_#{I18n.language_code}"
72
+ def translation_suffix(language_code = nil)
73
+ language_code = available(language_code) if language_code
74
+ language_code ||= I18n.language_code
75
+ default?(language_code) ? '' : "_#{language_code}"
74
76
  end
75
77
 
76
78
  private
@@ -0,0 +1,26 @@
1
+ module L10n
2
+ class TranslationValidator < ActiveModel::EachValidator
3
+ def self.kind
4
+ :translation
5
+ end
6
+
7
+ def validate_each(record, attribute, value)
8
+ translations = record.public_send("#{attribute}_translations")
9
+ I18n.available_language_codes.each do |language_code|
10
+ if translations[language_code].blank?
11
+ record.errors.add(record.class.translate_column_name("#{attribute}_t", language_code), options[:message] || :blank)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ module ActiveModel
19
+ module Validations
20
+ module HelperMethods
21
+ def validates_translation_of(*attr_names)
22
+ validates_with L10n::TranslationValidator, _merge_attributes(attr_names)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module L10n
2
- VERSION = '0.0.10'
2
+ VERSION = '0.0.11'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: l10n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-24 00:00:00.000000000 Z
12
+ date: 2013-10-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
@@ -158,6 +158,7 @@ files:
158
158
  - lib/l10n/inflections.rb
159
159
  - lib/l10n/numeric_column_conversions.rb
160
160
  - lib/l10n/request.rb
161
+ - lib/l10n/translation_validator.rb
161
162
  - lib/l10n/version.rb
162
163
  - lib/l10n.rb
163
164
  - lib/locales/de.yml