开发者

Zend Framework locale and translation problems in form with Zend_Locale, Zend_Translate and Zend_Form

I've been hav开发者_JAVA百科ing a few problems with Zend_Locale and Zend_Translate, specifically with htmlentities. But there a couple of other oddities.

Firstly, in the Zend_Locale documentation it states:

Zend Framework allows the usage of an application wide locale. You simply set an instance of Zend_Locale to the registry with the key 'Zend_Locale'. Then this instance will be used within all locale aware classes of Zend Framework

This does not seem to work with Zend_Translate, I'm assuming Zend_Translate is not a 'locale aware' class, weird that it isn't but no big problem I just had to set it explicitly. Below is the initLocale() function from my Bootstrap:

$session = new Zend_Session_Namespace();

// only if locale is explicitly set by user:
if(isset($_GET['locale']) && !empty($_GET['locale'])) {
    $session->locale = $_GET['locale'];
}

$locale = isset($session->locale) ? $session->locale : 'auto';

try {
    $zendLocale = new Zend_Locale($locale);
} catch (Zend_Locale_Exception $e) {
    $zendLocale = new Zend_Locale('en_CA');
}   

Zend_Registry::set('Zend_Locale', $zendLocale);

require_once(APPLICATION_PATH . '/languages/translation.php');

$translate = new Zend_Translate(array(
    'adapter' => 'array',
    'content' => $english,
    'locale'  => 'en'
));

$translate->addTranslation(array(
    'content' => $french,
    'locale'  => 'fr'
));

$translate->setLocale($zendLocale->toString());     
Zend_Registry::set('translate', $translate);

As you can see here, I'm using the array adapter for Zend_Translate. My arrays are defined in translation.php.

Similarly to Zend_Locale in the documentation for Zend_Form (http://framework.zend.com/manual/en/zend.form.i18n.html) it says the easiest way to add translations to Zend_Form is to set a Zend_Translate object in the registry with a key of 'Zend_Translate' - easy, set this up and any of the form labels, error messages, etc. that have a matching translation in the arrays will be translated automagically. Problem is that my french translations have a lot of htmlentities. I could be wrong but I believe that Zend_Form's setLabel method escapes text, so my entities are getting converted twice. I tried removing all entities from the translations but this completely fails and gives me a blank output.

Does anyone have any ideas? htmlentities for translations seems like a major necessity right?

Currently I'm not doing this automatically but I've added an intermediate function to my form which does the following (where $this->_translate is a Zend_Translate object):

private function getTranslation($str) {
    return html_entity_decode($this->_translate->_($str), ENT_COMPAT, 'UTF-8');
}

But this is far from ideal as I have to wrap every setLabel and errorMessage in this first, kind of missing the point of Zend's locale and translation classes.


I've also used Zend_Translate together with the array adapter, to set the labels of a form. I didn't need to encode any character as HTML entities. As far as I know, it's enough to encode them as UTF-8. So, the wrapper you mention shouldn't be necessary.

I'm just adding Zend_Translate to the registry (with the 'Zend_Translate' key, otherwise it won't work), passing the translation keys to setLabel() directly and it works.

Hope that helps...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜