views:

1972

answers:

2

Hi!

I have the whole MVC-Model set up and use HTML views as templates. But I have german strings in there that I would like to translate to other languages at some point.

What is the best way to do this? I know I have to use Zend_Translate, but do I have to implement a single call to a translate function for every word that I have in my view templates?

+6  A: 

First of all, I'd suggest to use complete phrases as the basis for your translation. With words you always have the problem that languages are not consistent when it comes to sentence structure.

Then you have to choose one of the available Zend_Transalate adapters: Array, Csv, Gettext, Ini, Tbx, Tmx, Qt, Xliff or XmlTm. Most of them are adapters to industry standards for storing translation information, so it probably would suffice if you chose Array, Csv or Ini for the beginning and for the ease of use. Please see 49.2.1. How to decide which translation adapter to use in the Zend Framework manual.

// setup your translation
$translate = new Zend_Translate('csv', '/my/path/source-de.csv', 'de');
$translate->addTranslation('/my/path/source-en.csv', 'en');
// add the translation adapter to the registry
Zend_Registry::set('Zend_Translate', $translate);

As there is a Zend_View_Helper_Translate that access the standardized Zend_Registry entry Zend_Translate as the default translation source, you can use the following in your views:

[...]
<title><?php echo this->translate('Title'); ?></title>
[...]
<p>You can also do <?php echo $this->translate('Hello %1$s', $this->userName); ?></p>
[...]

Please note that this only is a short introduction into Zend_Translate and by no means a complete presentation of the functionality provided by this component. For example there is a lot to be said about determining the locale the translation adapter will use.

I'd suggest you read the following in the Zend Framework manual, because localization can be a complex issue and Zend_Translate can not be described entirely in here:

Stefan Gehrig
Thanks. So I always have to call $this->translate(). Thank you!
Sebastian Hoitz
+1  A: 

No, you don't have to translate every single word. The idea of translation in this sense is more a of a message translation idea.

In my current project we do two types of translations. When it's just going to be a short text, we type that directly into the source code for purposes of readability.

I.E.

<?=$this->translate('Add');?>
<?=$this->translate('Delete');?>
<?=$this->translate('Are you sure you want to delete %1$s?', $thing);?>

But if the message is much longer, we usually follow a standard to mark it for the translation system:

<?=$this->translate('controller-action_form-information');?>

The idea being that you can then replace that with a very long text in your translation tool and it keeps the view script tidy.

smack0007