You are here: Browse Railsplugins Globalize Extension
The goal of this extension is to let you switch the language of a model during execution. Then you can have content in more than one language in the same page without pain.
Previous languages were cached in the model instance, then you can modify your model in more than one language and save all the modifications at once by a call to the standard save method!
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, then switch during a single code block using
Locale.switch(‘fr’) {#my code for French}.
The main method is ‘ActiveRecord#switch_language(code, &block)’ which : * translate the model in the language which correspond to the code * translate the loaded associations too * if a block is given : * modifiy Locale language to current one * execute the block * at the end, revert model, Locale and associations to previous language
Exemple :
Locale.set(‘fr’) @document.title = “Nouvelles du jour” @document.switch_language(‘en’) do @document.title =ActiveRecord .globalized? —> return true if the model has translated fields. #language_code —> return the model current language code #switch_language—> load another translation for this model (cache the current values) switch_language is defined in ActiveRecord because it follow association. Then translatiosn doesen’t break if a product is not translated in the chain. #operate_switch_language_on_associations—> translates loaded associations (private)
Globalize::DbTranslate::ClassMethods #translates —> add the “after_save :save_translations” callback
Globalize::DbTranslate::TranslateClassMethods #after_switch_language—> callback occurs after the translation. Useful if you have non-standard association. #before_switch_language—> callback occurs before the translation. #find_every —> override to support translations (private)
Globalize::DbTranslate::TranslateObjectMethods #after_switch_language—> callback #before_switch_language—> callback #set_globalized_attributes—> You can send a params dict which contain keys like { :title => {:fr => ‘Mon Titre’, :en => ‘My Title’} } #save_translations —> save all translations for the model #translation_cache[=] —> accessor to the @@translation_cache (protected) #operate_switch_language—> load other translations (private)
Globalize::Locale.switch —> switch the language code to ‘code’ during the block, then revert to previous language.
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly