0
votes

I am using Zend Translate to translate and localize views. Therefore I set the translator applicationwide using array translationfiles like en.php and fr.php. Now I have for example in index.phtml:

  • Title in English
  • Subject1 in English
  • Subject2 in English

Say if I want part of a view translated in another language, is this possible? The desired result from index.phtml would be:

  • Title in English
  • Sujet1 en francais
  • Subject2 in English

Within the same view. I tried adding in my viewfile just before Subject1

<?php $translate =  new Zend_Translate('array', APPLICATION_PATH .'/../language/fr.php', 'fr'); ?>

<?php echo $this->translate('Subject1 in English');?>

but the application-wide translation overrules. Is it possible to change from one translation to another within one view and how should I go about this?

1

1 Answers

3
votes

It's simpler than it looks. Just translate using your new translator:

<?php $translate =  new Zend_Translate('array', APPLICATION_PATH .'/../language/fr.php', 'fr'); ?>

<?php echo $this->translate('SUBJECT1');?>//this will be in English
<?php echo $translate->translate('SUBJECT1');?>//this will be in French

<?php echo $translate->_('SUBJECT1');?>//same as above, just shorter

You can also try some more permanent solution which also not require you to change standard way of translating:

<?= $this->translate('SUBJECT');?> //this will be in standard language

<?php
$old = $this->translate()->getTranslator(); //save current translator
$this->translate()->setTranslator($newTranslator); //set new translator
?>

<?= $this->translate('SUBJECT');?> //this will be in new language

$this->translate()->setTranslator($old); //restore original translator

<?= $this->translate('SUBJECT');?> //this again will be in original language