Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

call the translation helper from an action

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)

by Francois Zaninotto on 2006-07-24, tagged helper  i18n 

Comments on this snippet

gravatar icon
#1 brikou on 2006-07-27 at 10:21

we can also do the same for any other core helpers like URL helpers:

include_once('symfony/helper/UrlHelper.php');
 
$this->getResponse()->addHttpMeta('refresh', '5; url=' . url_for('order/confirm'));
gravatar icon
#2 christophe blin on 2006-08-22 at 11:27

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).

gravatar icon
#3 rihad on 2007-06-07 at 11:25

[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.

You need to create an account or log in to post a comment or rate this snippet.