Kiren S
Kiren S

Reputation: 3097

Zend Translation and View Scripts

Form labels and error messages are translated automatically. But strings in the view scripts are not. I have to use $this->translate("text to transfer"); in each and every phtml files.I don't want to use this $this->translate("text to transfer"); method.How can I automatically translate view scripts as in the case of forms.My code is

protected function _initTranslation() { 
    $langNamespace = new Zend_Session_Namespace('language_sess');
    $lang           = $langNamespace->lang;
    $registry = Zend_Registry::getInstance();
    $tr = new Zend_Translate(
        array(
            'adapter' => 'array',
            'content' => APPLICATION_PATH . "/languages/$lang/$lang.php",
            'locale'  => "ar",
            'scan' => Zend_Translate::LOCALE_DIRECTORY
        )
    );
    $registry->set('Zend_Translate', $tr);
    return $registry;
}

Upvotes: 0

Views: 179

Answers (1)

Serge Kuharev
Serge Kuharev

Reputation: 1052

OK, to make it clear, we need to understand few things:

  • Zend_Translate is supposed to translate only part of your site, not related to content. That is menu items, section names, titles, etc. You might have exceptions from that rule, especially if you use Zend_Translate_Adapter_Gettext.
  • For content, you should use something else to provide your translation. Usually, for example if it is a static article, you get that data from DB, not the language file.

In your case, if you are ok with a little bit "machine" translation, you should use google translate engine for sites

https://translate.google.com/manager/website/

As for dropping $this->translate("text to transfer"); part, you can try to translate all data in controller, but that is rather bad advice, I would advice you to do something else. When you pass your data to view, you can loop through it and make translation for each and everystring inside:

foreach($data as $key => $string) {
    $data[$key] = $tr->translate($string);
}

or something like that. This is bad approach because of few reasons. But most important is that you are not dividing your presentation from your logic, and that is why the best place for this is View.

So my summary:

  • Try to keep using $this->translate()
  • If that really does not work for you, try to use google translate engine
  • If that is also not good for you, try Zend_Translate_Adapter_Gettext.
  • Please, do not use language files for your content.

Upvotes: 2

Related Questions