zlocalize 4.2.1 → 4.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/zlocalize/rails/active_record.rb +3 -0
- data/lib/zlocalize/rails/attached_translations.rb +14 -4
- data/lib/zlocalize/rails/default_locale_evaluator.rb +44 -0
- data/lib/zlocalize/rails/translated_attributes_serializer.rb +15 -4
- data/lib/zlocalize/rails/translated_columns.rb +13 -5
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0b7205eae0de41db4cd6fcbc5aa2cbd5807e60a
|
4
|
+
data.tar.gz: 3c0374e95c6e25cbfb8b43dd93238635b22f605a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed6638ee95dd4bb20b32fad644c6352f079d5ed6a788f500e2d4fa82c44a473717130eab600af9bb0612a3493889f46c29f1d2ea3ab41ee56c22e15be96cadb9
|
7
|
+
data.tar.gz: d96e729b473b41d377352a1a770f254a1e8e5ed611b483f22aba85c4023b1e31aee1468d8d191bd36daf744c52d49cb25f5393ec7bc6a79def9de11cd93a5981
|
@@ -11,3 +11,6 @@ ActiveRecord::Base.send(:include, ZLocalize::Translatable::LocalizedDecimalAttri
|
|
11
11
|
|
12
12
|
require 'zlocalize/rails/translated_attributes_serializer'
|
13
13
|
ActiveRecord::Base.send(:include, ZLocalize::Translatable::TranslatedAttributesSerializer)
|
14
|
+
|
15
|
+
require 'zlocalize/rails/default_locale_evaluator'
|
16
|
+
ActiveRecord::Base.send(:include, ZLocalize::Translatable::DefaultLocaleEvaluator)
|
@@ -41,14 +41,18 @@ module ZLocalize
|
|
41
41
|
end
|
42
42
|
|
43
43
|
module AttachedTranslations #:nodoc:
|
44
|
+
|
44
45
|
def self.included(base)
|
45
46
|
base.extend(ClassMethods)
|
46
47
|
end
|
47
48
|
|
48
49
|
module ClassMethods
|
49
50
|
|
50
|
-
def has_translations
|
51
|
+
def has_translations(options = {})
|
51
52
|
has_many :translations, :as => :translated, :dependent => :destroy
|
53
|
+
|
54
|
+
set_default_locale_for_translations(options[:default_locale])
|
55
|
+
|
52
56
|
include ZLocalize::Translatable::AttachedTranslations::InstanceMethods
|
53
57
|
end
|
54
58
|
|
@@ -56,12 +60,18 @@ module ZLocalize
|
|
56
60
|
|
57
61
|
module InstanceMethods
|
58
62
|
|
59
|
-
def translate(attr_name,locale = nil)
|
63
|
+
def translate(attr_name,locale = nil, fetch_default = true)
|
60
64
|
locale ||= ZLocalize.locale
|
61
65
|
if tr = find_translation(attr_name,locale)
|
62
|
-
tr.value
|
66
|
+
return tr.value
|
63
67
|
else
|
64
|
-
|
68
|
+
unless (default_locale = evaluate_default_locale_for_translations).blank?
|
69
|
+
if default_locale.to_s != locale.to_s
|
70
|
+
if tr = find_translation(attr_name,default_locale)
|
71
|
+
return tr.value
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
65
75
|
end
|
66
76
|
end
|
67
77
|
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module ZLocalize
|
3
|
+
module Translatable
|
4
|
+
module DefaultLocaleEvaluator
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
cattr_reader :default_locale_for_translations
|
13
|
+
|
14
|
+
def set_default_locale_for_translations(value)
|
15
|
+
@@default_locale_for_translations =
|
16
|
+
case value
|
17
|
+
when nil then nil
|
18
|
+
when Symbol, String then value.to_sym
|
19
|
+
else
|
20
|
+
raise(
|
21
|
+
Zigotos::Translatable::TranslationError,
|
22
|
+
"default_locale option must be either a String or a Symbol representing a method on this class (trying to set it to a #{value.class.name})"
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
include ZLocalize::Translatable::DefaultLocaleEvaluator::InstanceMethods
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module InstanceMethods
|
31
|
+
|
32
|
+
def evaluate_default_locale_for_translations
|
33
|
+
unless self.class.default_locale_for_translations.blank?
|
34
|
+
self.send(self.class.default_locale_for_translations)
|
35
|
+
else
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end # module InstanceMethods
|
41
|
+
|
42
|
+
end # module DefaultLocaleEvaluator
|
43
|
+
end # module Translatable
|
44
|
+
end # module ZLocalize
|
@@ -15,9 +15,11 @@ module ZLocalize
|
|
15
15
|
|
16
16
|
serialize :translated_attributes, HashWithIndifferentAccess
|
17
17
|
|
18
|
+
set_default_locale_for_translations(options[:default_locale])
|
19
|
+
|
18
20
|
[attribute_names].flatten.each do |attr_name|
|
19
21
|
class_eval "def #{attr_name}(options = {})
|
20
|
-
read_translated_attribute('#{attr_name}',(options[:locale] || ZLocalize.locale).to_s)
|
22
|
+
read_translated_attribute('#{attr_name}',(options[:locale] || ZLocalize.locale).to_s, options[:fetch_default] == true)
|
21
23
|
end"
|
22
24
|
end
|
23
25
|
|
@@ -31,13 +33,22 @@ module ZLocalize
|
|
31
33
|
|
32
34
|
include ZLocalize::Translatable::TranslatedAttributesSerializer::InstanceMethods
|
33
35
|
end
|
36
|
+
|
34
37
|
end
|
35
38
|
|
36
39
|
module InstanceMethods
|
37
40
|
|
38
|
-
def read_translated_attribute(attr_name,locale)
|
39
|
-
s = self.translated_attributes[locale]
|
40
|
-
s.
|
41
|
+
def read_translated_attribute(attr_name,locale,fetch_default = true)
|
42
|
+
s = self.translated_attributes[locale].try(:'[]',attr_name)
|
43
|
+
if s.blank? && fetch_default
|
44
|
+
unless (default_locale = evaluate_default_locale_for_translations).blank?
|
45
|
+
if default_locale.to_s != locale.to_s
|
46
|
+
return self.translated_attributes[default_locale].try(:'[]',attr_name)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
else
|
50
|
+
return s
|
51
|
+
end
|
41
52
|
end
|
42
53
|
|
43
54
|
alias :translate :read_translated_attribute
|
@@ -38,7 +38,9 @@ module ZLocalize
|
|
38
38
|
|
39
39
|
module ClassMethods
|
40
40
|
|
41
|
-
def translates_columns(column_names)
|
41
|
+
def translates_columns(column_names, options = {})
|
42
|
+
|
43
|
+
set_default_locale_for_translations(options[:default_locale])
|
42
44
|
|
43
45
|
[column_names].flatten.each do |col_name|
|
44
46
|
class_eval "def #{col_name}(options = {})
|
@@ -54,12 +56,18 @@ module ZLocalize
|
|
54
56
|
|
55
57
|
def read_translated_column(col_name,locale,fetch_default = true)
|
56
58
|
s = self.read_attribute("#{col_name}_#{locale}")
|
57
|
-
if
|
59
|
+
if s.blank? && fetch_default
|
60
|
+
unless (default_locale = evaluate_default_locale_for_translations).blank?
|
61
|
+
if default_locale.to_s != locale.to_s
|
62
|
+
attr_name = "#{col_name}_#{default_locale}"
|
63
|
+
if self.respond_to?(attr_name)
|
64
|
+
return self.read_attribute(attr_name)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
else
|
58
69
|
return s
|
59
|
-
elsif fetch_default == true
|
60
|
-
return self.read_attribute("#{col_name}_#{ZLocalize.default_locale}")
|
61
70
|
end
|
62
|
-
nil
|
63
71
|
end
|
64
72
|
|
65
73
|
end # module InstanceMethods
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zlocalize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.2.
|
4
|
+
version: 4.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charles Bedard
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- lib/zlocalize/rails/active_record.rb
|
82
82
|
- lib/zlocalize/rails/attached_translations.rb
|
83
83
|
- lib/zlocalize/rails/decimal_attributes.rb
|
84
|
+
- lib/zlocalize/rails/default_locale_evaluator.rb
|
84
85
|
- lib/zlocalize/rails/generators/initializer.rb
|
85
86
|
- lib/zlocalize/rails/generators/templates/initializer_template.rb
|
86
87
|
- lib/zlocalize/rails/generators/templates/translations_migration_template.rb
|