easy_translatable 0.1.3 → 0.1.4
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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a2db1f63c194bb5c4ff3014938c96f635ad2f17
|
4
|
+
data.tar.gz: 296172fd483778a4e593c659bb2b4a30c665ad53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e63d023cdde40a254c4874f81f892ecf28c28143d6857008776f3cf1efe49edf959034c8e6d45320fe1562ee44eeace6efced3161775a72e96a089c0a47e568b
|
7
|
+
data.tar.gz: 8908a1addd1f71c87c15ac9033d5c0ad27a6ae8fde08616cc398e159a721267a946fd7b528450427528b9d5c992967af6592246eef967a16a4e581e05b536ee2
|
@@ -21,8 +21,8 @@ module Translatable
|
|
21
21
|
stash.contains?(locale, name)
|
22
22
|
end
|
23
23
|
|
24
|
-
def fetch(locale, name)
|
25
|
-
value = stash_contains?(locale, name) ? fetch_stash(locale, name) : fetch_attribute(locale, name)
|
24
|
+
def fetch(locale, name, value = nil)
|
25
|
+
value = stash_contains?(locale, name) ? fetch_stash(locale, name) : fetch_attribute(locale, name, value)
|
26
26
|
|
27
27
|
return value
|
28
28
|
end
|
@@ -33,9 +33,14 @@ module Translatable
|
|
33
33
|
|
34
34
|
protected
|
35
35
|
|
36
|
-
def fetch_attribute(locale, name)
|
37
|
-
|
38
|
-
|
36
|
+
def fetch_attribute(locale, name, value = nil)
|
37
|
+
if value.nil?
|
38
|
+
translation = record.translation_for(locale, name)
|
39
|
+
return translation && translation.send(:value)
|
40
|
+
else
|
41
|
+
translation = record.translation_for_serialized(locale, name, value)
|
42
|
+
return translation
|
43
|
+
end
|
39
44
|
end
|
40
45
|
|
41
46
|
end
|
@@ -6,7 +6,8 @@ module Translatable
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def translated?(name)
|
9
|
-
translated_attribute_names.include?(name.to_sym)
|
9
|
+
included = translated_attribute_names.detect { |attr| attr.is_a?(Hash) ? attr.keys.include?(name.to_sym) : attr == name.to_sym }
|
10
|
+
!included.nil?
|
10
11
|
end
|
11
12
|
|
12
13
|
protected
|
@@ -53,7 +53,8 @@ module Translatable
|
|
53
53
|
def read_attribute(name, options = {})
|
54
54
|
options = {:translated => true, :locale => nil}.merge(options)
|
55
55
|
if translated?(name) and options[:translated]
|
56
|
-
|
56
|
+
serialized_value = super(name) if self.serialized_attributes.has_key?(name)
|
57
|
+
if translate? && (value = translatable.fetch(options[:locale] || Translatable.locale, name, serialized_value))
|
57
58
|
value
|
58
59
|
else
|
59
60
|
super(name)
|
@@ -77,6 +78,45 @@ module Translatable
|
|
77
78
|
translation_caches["#{locale}_#{name}"]
|
78
79
|
end
|
79
80
|
|
81
|
+
# Fetch translations per keys in the form
|
82
|
+
# model_attr_serialized_attribute
|
83
|
+
# E.G.1: ```Model``` has attribute ```config``` which has a key ```auto_save```
|
84
|
+
# will have a translation key ```config_auto_save```
|
85
|
+
# E.G.2: ```Model``` has attribute ```config``` which is an array of ```user_id => ([0-9]+)```
|
86
|
+
# will have translations key containing the index: ```config_user_id_0```, ```config_user_id_1```...
|
87
|
+
def translation_for_serialized(locale, name, value, build_if_missing = false)
|
88
|
+
|
89
|
+
unless translation_caches["#{locale}_#{name}"]
|
90
|
+
engine = self.serialized_attributes.has_key?(name) ? nil : JSON
|
91
|
+
deserialized_value = engine.nil? ? value : engine.load(value)
|
92
|
+
|
93
|
+
only = self.translated_serialized_attributes[name.to_sym].map(&:to_s)
|
94
|
+
if only.empty?
|
95
|
+
regex_keys = /\A#{name.to_s}_([a-z_]+[a-z])(?:_([0-9]*))?\Z/
|
96
|
+
else
|
97
|
+
regex_keys = /\A#{name.to_s}_(#{only.join('|')})(?:_([0-9]*))?\Z/
|
98
|
+
end
|
99
|
+
# Fetch translations from database as those in the translation collection may be incomplete
|
100
|
+
_translations = translations.select{|t| t.locale.to_s == locale.to_s && t.key.to_s =~ regex_keys }
|
101
|
+
_translations.each do |t|
|
102
|
+
matched = t.key.match regex_keys
|
103
|
+
unless matched.nil?
|
104
|
+
if matched.size == 3
|
105
|
+
sub_attr = matched[1]
|
106
|
+
index = matched[2].to_i
|
107
|
+
value[index][sub_attr] = t.value if value.size > index && value[index].has_key?(sub_attr)
|
108
|
+
elsif matched.size == 3
|
109
|
+
sub_attr = matched[1]
|
110
|
+
value[sub_attr] = t.value if value.has_key?(sub_attr)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
translation_caches["#{locale}_#{name}"] = value
|
116
|
+
end
|
117
|
+
translation_caches["#{locale}_#{name}"]
|
118
|
+
end
|
119
|
+
|
80
120
|
def translation_caches
|
81
121
|
@translation_caches ||= {}
|
82
122
|
end
|
@@ -19,7 +19,9 @@ module Translatable
|
|
19
19
|
|
20
20
|
# Add attribute to the list.
|
21
21
|
self.translated_attribute_names << attr_name
|
22
|
-
self.
|
22
|
+
if self.serialized_attributes.has_key?(attr_name) || options[:only]
|
23
|
+
self.translated_serialized_attributes[attr_name] = options[:only] ? options[:only] : []
|
24
|
+
end
|
23
25
|
end
|
24
26
|
|
25
27
|
Translatable.add_translatable self
|
@@ -48,7 +50,7 @@ module Translatable
|
|
48
50
|
self.translated_attribute_names = []
|
49
51
|
self.translation_options = options
|
50
52
|
self.fallbacks_for_empty_translations = options[:fallbacks_for_empty_translations]
|
51
|
-
self.translated_serialized_attributes = Hash.new
|
53
|
+
self.translated_serialized_attributes = Hash.new
|
52
54
|
|
53
55
|
include InstanceMethods
|
54
56
|
extend ClassMethods
|
data/lib/translatable.rb
CHANGED
@@ -44,7 +44,10 @@ module Translatable
|
|
44
44
|
end
|
45
45
|
|
46
46
|
unless klass.translated_serialized_attributes.nil?
|
47
|
-
@@translatable[klass.name].map!
|
47
|
+
@@translatable[klass.name].map! do |attr|
|
48
|
+
serialized = klass.translated_serialized_attributes.reject{|k,v| k != attr}
|
49
|
+
serialized.empty? ? attr : serialized
|
50
|
+
end
|
48
51
|
end
|
49
52
|
end
|
50
53
|
|