acts_as_translatable 0.1.4 → 0.1.5
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.
- data/lib/acts_as_translatable/active_record.rb +41 -17
- metadata +3 -3
@@ -7,9 +7,12 @@ class ActiveRecord::Base
|
|
7
7
|
def translations
|
8
8
|
unless @translations
|
9
9
|
@translations = {}
|
10
|
+
I18n.available_locales.each do |locale|
|
11
|
+
@translations[locale] ||= {}
|
12
|
+
end
|
10
13
|
record_translations.each do |translation|
|
11
14
|
@translations[translation.locale.to_sym] ||= {}
|
12
|
-
@translations[translation.locale.to_sym][translation.translatable_field] = translation.content
|
15
|
+
@translations[translation.locale.to_sym][translation.translatable_field.to_sym] = translation.content
|
13
16
|
end
|
14
17
|
end
|
15
18
|
@translations
|
@@ -36,36 +39,57 @@ class ActiveRecord::Base
|
|
36
39
|
# delete all translations of this record
|
37
40
|
record_translations.destroy_all
|
38
41
|
end
|
39
|
-
end"
|
42
|
+
end"
|
43
|
+
|
44
|
+
localized_fields = ""
|
45
|
+
I18n.available_locales.each do |locale|
|
46
|
+
fields.each do |field|
|
47
|
+
localized_fields << "def #{field}_#{locale}
|
48
|
+
get_field_content(\"#{locale}\".to_sym, \"#{field}\".to_sym)
|
49
|
+
end
|
50
|
+
def #{field}_#{locale}=(content)
|
51
|
+
set_field_content(\"#{locale}\".to_sym, \"#{field}\".to_sym, content)
|
52
|
+
end
|
53
|
+
"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
40
57
|
fields.each do |field|
|
41
58
|
eval "class ::#{name}
|
42
59
|
def #{field}
|
60
|
+
get_field_content(I18n.locale, \"#{field}\")
|
61
|
+
end
|
62
|
+
|
63
|
+
def #{field}?
|
64
|
+
!#{field}.blank?
|
65
|
+
end
|
66
|
+
|
67
|
+
def #{field}=(content)
|
68
|
+
set_field_content(I18n.locale, \"#{field}\", content)
|
69
|
+
end
|
70
|
+
|
71
|
+
#{localized_fields}
|
72
|
+
|
73
|
+
def get_field_content(locale, field)
|
43
74
|
# get I18n fallbacks
|
44
75
|
if I18n.respond_to?(:fallbacks)
|
45
|
-
locales = I18n.fallbacks[
|
76
|
+
locales = I18n.fallbacks[locale.to_sym]
|
46
77
|
else
|
47
|
-
locales = [
|
78
|
+
locales = [locale.to_sym]
|
48
79
|
end
|
49
80
|
|
50
81
|
# fallbacks
|
51
|
-
locales.each do |
|
52
|
-
|
53
|
-
|
54
|
-
return content if content
|
55
|
-
end
|
82
|
+
locales.each do |l|
|
83
|
+
content = translations[l][field.to_sym]
|
84
|
+
return content if content
|
56
85
|
end
|
57
86
|
|
58
87
|
# none found
|
59
88
|
return nil
|
60
89
|
end
|
61
|
-
|
62
|
-
def
|
63
|
-
|
64
|
-
end
|
65
|
-
|
66
|
-
def #{field}=(value)
|
67
|
-
translations[I18n.locale] ||= {}
|
68
|
-
translations[I18n.locale][\"#{field}\"] = value
|
90
|
+
|
91
|
+
def set_field_content(locale, field, content)
|
92
|
+
translations[locale.to_sym][field.to_sym] = content
|
69
93
|
end
|
70
94
|
end"
|
71
95
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_translatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lasse Bunk
|