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