| 1 |
The goal of this extension is to let you switch the language |
|---|
| 2 |
of a model during execution. Then you can have content in more |
|---|
| 3 |
than one language in the same page without pain. |
|---|
| 4 |
|
|---|
| 5 |
Previous languages were cached in the model instance, then you |
|---|
| 6 |
can modify your model in more than one language and save all the |
|---|
| 7 |
modifications at once by a call to the standard save method! |
|---|
| 8 |
|
|---|
| 9 |
A side effect of the extension is that it is less error prone because |
|---|
| 10 |
you can set the language once at begining of the page, |
|---|
| 11 |
then switch during a single code block using |
|---|
| 12 |
|
|---|
| 13 |
Locale.switch('fr') {#my code for French}. |
|---|
| 14 |
|
|---|
| 15 |
The main method is 'ActiveRecord#switch_language(code, &block)' which : |
|---|
| 16 |
* translate the model in the language which correspond to the code |
|---|
| 17 |
* translate the loaded associations too |
|---|
| 18 |
* if a block is given : |
|---|
| 19 |
* modifiy Locale language to current one |
|---|
| 20 |
* execute the block |
|---|
| 21 |
* at the end, revert model, Locale and associations |
|---|
| 22 |
to previous language |
|---|
| 23 |
|
|---|
| 24 |
Exemple : |
|---|
| 25 |
|
|---|
| 26 |
Locale.set('fr') |
|---|
| 27 |
@document.title = "Nouvelles du jour" |
|---|
| 28 |
@document.switch_language('en') do |
|---|
| 29 |
@document.title = "Today News" |
|---|
| 30 |
@document.title #--> Today News |
|---|
| 31 |
end |
|---|
| 32 |
@document.title #--> Nouvelle du jour |
|---|
| 33 |
|
|---|
| 34 |
# And then save everything: |
|---|
| 35 |
@document.save |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
GlobalizeExtension add the following methods to Globalize plugin |
|---|
| 39 |
|
|---|
| 40 |
defined in active_record_ex.rb : |
|---|
| 41 |
=============================== |
|---|
| 42 |
ActiveRecord |
|---|
| 43 |
.globalized? --> return true if the model has translated fields. |
|---|
| 44 |
#language_code --> return the model current language code |
|---|
| 45 |
#switch_language --> load another translation for |
|---|
| 46 |
this model (cache the current values) |
|---|
| 47 |
switch_language is defined in ActiveRecord |
|---|
| 48 |
because it follow association. Then translatiosn |
|---|
| 49 |
doesen't break if a product is not translated in |
|---|
| 50 |
the chain. |
|---|
| 51 |
#operate_switch_language_on_associations --> translates loaded |
|---|
| 52 |
associations (private) |
|---|
| 53 |
|
|---|
| 54 |
defined in db_translate_ex.rb : |
|---|
| 55 |
============================== |
|---|
| 56 |
Globalize::DbTranslate::ClassMethods |
|---|
| 57 |
#translates --> add the "after_save :save_translations" |
|---|
| 58 |
callback |
|---|
| 59 |
|
|---|
| 60 |
Globalize::DbTranslate::TranslateClassMethods |
|---|
| 61 |
#after_switch_language --> callback occurs after the translation. |
|---|
| 62 |
Useful if you have non-standard association. |
|---|
| 63 |
#before_switch_language --> callback occurs before the translation. |
|---|
| 64 |
#find_every --> override to support translations (private) |
|---|
| 65 |
|
|---|
| 66 |
Globalize::DbTranslate::TranslateObjectMethods |
|---|
| 67 |
#after_switch_language --> callback |
|---|
| 68 |
#before_switch_language --> callback |
|---|
| 69 |
#set_globalized_attributes --> You can send a params dict which contain |
|---|
| 70 |
keys like { :title => {:fr => 'Mon Titre', :en => 'My Title'} } |
|---|
| 71 |
#save_translations --> save all translations for the model |
|---|
| 72 |
#translation_cache[=] --> accessor to the @@translation_cache (protected) |
|---|
| 73 |
#operate_switch_language --> load other translations (private) |
|---|
| 74 |
|
|---|
| 75 |
defined in local_ex.rb : |
|---|
| 76 |
======================= |
|---|
| 77 |
Globalize::Locale.switch --> switch the language code to 'code' during |
|---|
| 78 |
the block, then revert to previous language. |
|---|