![]() |
|
Snippets |
|
You sometimes need to access the __() helper for text translation outside of a template. There are two ways to do it.
You can include the I18NHelper.php manually in your action, or use the symfony command to include a helper (sfLoaded::LoadHElpers()):
sfLoader::LoadHelpers(array('I18N'));
The problem is that this will add the helper functions to the global namespace. Alternately, use the following syntax:
sfContext::getInstance()->getI18n()->__($text, $args, 'messages');
Note: prior to 1.0, you had to do this (now deprecated)
sfConfig::get('sf_i18n_instance')->__($text, $args, 'messages');
(updated 29/11/06)
Comments on this snippet
we can also do the same for any other core helpers like URL helpers:
format_number_choice, format_country and all the other functions of the Helper can not be called with the 2nd technique.
This is because the sfI18N class only has the __ method (i.e see the code of I18NHelper to understand).
[php]sfContext::getInstance()->getI18n()->__($text, $args, 'messages');[/php] It should be noted that this will only work if you enable i18n in your app/settings yml (off by default), and give error otherwise. In other words, you must decide if you're gonna use i18n or not when you begin writing code.