embedded_localization 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
data/README.textile
CHANGED
@@ -50,15 +50,28 @@ g.name # => "공상 과학 소설"
|
|
50
50
|
|
51
51
|
No extra tables needed for this!
|
52
52
|
|
53
|
-
h3. Rails 3.0
|
53
|
+
h3. Rails 3.0
|
54
54
|
|
55
55
|
<pre><code>
|
56
56
|
class CreateGenres < ActiveRecord::Migration
|
57
57
|
def self.up
|
58
58
|
create_table :genres do |t|
|
59
|
-
t.text :i18n #
|
59
|
+
t.text :i18n # stores the translated attributes; persisted as a HashWithIndifferentAccess
|
60
|
+
|
61
|
+
# optional:
|
62
|
+
# t.string :name # you CAN define :name as a real column in your DB (but you don't have to)
|
63
|
+
# # If you define it, it will store the I18n.default_locale translation for SQL lookups
|
64
|
+
# # You can do this for any of the translated attributes.
|
60
65
|
t.timestamps
|
61
66
|
end
|
67
|
+
# -- example for a data migration: ---
|
68
|
+
# Genre.record_timestamps = false
|
69
|
+
# Genre.all.each do |g|
|
70
|
+
# g.name = g.name # the right-hand-side fetches the translation from the i18n attribute hash
|
71
|
+
# g.save # saves the :name attribute without updating the updated_at timestamp
|
72
|
+
# end
|
73
|
+
# Genre.record_timestamps = true
|
74
|
+
# ------------------------------------
|
62
75
|
end
|
63
76
|
def self.down
|
64
77
|
drop_table :posts
|
@@ -66,7 +79,16 @@ class CreateGenres < ActiveRecord::Migration
|
|
66
79
|
end
|
67
80
|
</code></pre>
|
68
81
|
|
69
|
-
|
82
|
+
h4. NOTE:
|
83
|
+
|
84
|
+
EmbeddedLocalization implementations < 0.2.0 have the drawback that you can not do SQL queries on translated attributes.
|
85
|
+
|
86
|
+
To eliminate this limitation, you can now define any translated attribute as a first-class database column in your migration. If you define a translated attribute as a column, EmbeddedLocalization will store the attribute value for I18n.default_locale in that column, so you can search for it. After defining the column, and running the migration, you need to populate the column initially. It will auto-update every time you write while you are using I18n.default_locale .
|
87
|
+
e.g.:
|
88
|
+
|
89
|
+
<pre><code>
|
90
|
+
g = Genre.where(:name => "science fiction") # this only works if you define :name as a DB column, and populate it
|
91
|
+
</pre></code>
|
70
92
|
|
71
93
|
Note that the ActiveRecord model @Genre@ must already exist and have a @translates@ directive listing the translated fields.
|
72
94
|
|
@@ -68,6 +68,9 @@ module EmbeddedLocalization
|
|
68
68
|
#
|
69
69
|
define_method(attr_name.to_s+ '=') do |new_translation|
|
70
70
|
self.i18n_will_change! # for ActiveModel Dirty tracking
|
71
|
+
if self.attributes.has_key?(attr_name.to_s) # if user has defined DB field with that name
|
72
|
+
write_attribute(attr_name , new_translation) if I18n.locale == I18n.default_locale
|
73
|
+
end
|
71
74
|
self.i18n[I18n.locale] ||= HashWithIndifferentAccess.new
|
72
75
|
self.i18n[I18n.locale][attr_name] = new_translation
|
73
76
|
end
|
@@ -18,6 +18,9 @@ module EmbeddedLocalization
|
|
18
18
|
|
19
19
|
def set_localized_attribute(attr_name, locale, new_translation)
|
20
20
|
self.i18n_will_change! # for ActiveModel Dirty tracking
|
21
|
+
if self.attributes.has_key?(attr_name.to_s) # if user has defined DB field with that name
|
22
|
+
write_attribute(attr_name , new_translation) if locale == I18n.default_locale
|
23
|
+
end
|
21
24
|
self.i18n[locale] ||= HashWithIndifferentAccess.new
|
22
25
|
self.i18n[locale][attr_name] = new_translation
|
23
26
|
end
|