Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Use Zend_Translate to Translate Your Web App

DZone's Guide to

Use Zend_Translate to Translate Your Web App

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

There are some really good reasons to use Zend_Translate for your multilingual site instead of some other technique. Actually you can definitely simulate something like this Zend component with and array or ini file, but here are some good things to mention.

1. Write a human readable source file

In fact you can do this even without Zend_Translate. Image you’ve an ini file with some pairs like:

lblMyLabel = "My Label"

Nobody says this cannot be “more” human readable – like so:

My Label = "My Label"

Than you can support several files i.e. en.ini, de.ini, etc. where you can translate this label. The problem is that once you read the ini file you’ve to deal with those white spaces into the labels, while with Zend_Translate you can simply call them with:

$translate->_("My Label");

 

2. Default values for missing labels

Zend_Translate forces you to define a source file. Which is really great, because you always have a default value. Assuming that you don’t have a specific label translated in one of the ini files, you’ll get the default value from the source file, and defining source and other files is as easy as:

// en.ini
My Label = "My Label"

// de.ini
...

// while the locale is de_DE you'll have
// the default value returned
$translate->_("My Label"); // will return My Label from the en.ini


3. Locales

It’s really great that you can directly setup a locale for this module. Thus you have more flexibility when switching between languages and you can benefit from the power of the framework when reading some browser specific locales. Here’s some code:

$translate = new Zend_Translate('ini', 'en.ini', 'en');
$translate->addTranslation('de.ini', 'de');

// than when you setup the translation locale
$translate->setLocale('en');

 

4. Cache

One of the greatest things about Zend_Translate is the native support of cache. It’s so cool! Actually the labels of a large web system are pretty static and don’t change every day. In other hand in large scale systems there are lots of labels, and the load time will be faster with cache.

$frontendOptions = array('lifetime' => 600, 'automatic_serialization' => true);
$backendOptions = array('cache_dir' => 'path_to_cache_dir');

$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

Zend_Translate::setCache($cache);

It’s a great advantage to use Zend_Translate after all this. Actually I have finished projects without any use of it, but I really can’t imagine how I’ll work now without it!

Related posts:

  1. Theory of caching. Zend_Cache & Zend Optimizer.
  2. Zend_Date – make it work and benefit with locales
  3. php.ini for two web servers

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of Stoimen Popov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}