PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Handling languages

When working with different languages there are a few methods which will be useful.

The getLocale() method can be used to get the currently set language. It can either hold an instance of Zend_Locale or the identifier of a locale.

The setLocale() method sets a new standard language for translation. This prevents the need of setting the optional language parameter more than once to the translate() method. If the given language does not exist, or no translation data is available for the language, setLocale() tries to downgrade to the language without the region if any was given. A language of en_US would be downgraded to en. When even the downgraded language can not be found an exception will be thrown.

The isAvailable() method checks if a given language is already available. It returns TRUE if data for the given language exist.

And finally the getList() method can be used to get all currently set languages for an adapter returned as array.

Example 970. Handling languages with adapters

<?php
// returns the currently set language
$actual $translate->getLocale();

// you can use the optional parameter while translating
echo $translate->_("my_text""fr");
// or set a new language
$translate->setLocale("fr");
echo 
$translate->_("my_text");
// refer to the base language
// fr_CH will be downgraded to fr
$translate->setLocale("fr_CH");
echo 
$translate->_("my_text");

// check if this language exist
if ($translate->isAvailable("fr")) {
    
// language exists
}

Automatical handling of languages

Note that as long as you only add new translation sources with the addTranslation() method Zend_Translate will automatically set the best fitting language for your environment when you use one of the automatic locales which are 'auto' or 'browser'. So normally you will not need to call setLocale(). This should only be used in conjunction with automatic source detection.

The algorithm will search for the best fitting locale depending on the user's browser and your environment. See the following example for details:

Example 971. Automatically language detection

<?php
// Let's expect the browser returns these language settings:
// HTTP_ACCEPT_LANGUAGE = "de_AT=1;fr=1;en_US=0.8";

// Example 1:
// When no fitting language is found, the message ID is returned
$translate = new Zend_Translate(
    array(
        
'adapter' => 'gettext',
        
'content' => 'my_it.mo',
        
'locale'  => 'auto',
        
'scan' => Zend_Translate::LOCALE_FILENAME
    
)
);

// Example 2:
// Best found fitting language is 'fr'
$translate = new Zend_Translate(
    array(
        
'adapter' => 'gettext',
        
'content' => 'my_fr.mo',
        
'locale'  => 'auto',
        
'scan' => Zend_Translate::LOCALE_FILENAME
    
)
);

// Example 3:
// Best found fitting language is 'de' ('de_AT' will be degraded)
$translate = new Zend_Translate(
    array(
        
'adapter' => 'gettext',
        
'content' => 'my_de.mo',
        
'locale'  => 'auto',
        
'scan' => Zend_Translate::LOCALE_FILENAME
    
)
);

// Example 4:
// Returns 'it' as translation source and overrides the automatic settings
$translate = new Zend_Translate(
    array(
        
'adapter' => 'gettext',
        
'content' => 'my_it.mo',
        
'locale'  => 'auto',
        
'scan' => Zend_Translate::LOCALE_FILENAME
    
)
);

$translate->addTranslation(array('content' => 'my_ru.mo''locale' => 'ru'));
$translate->setLocale('it_IT');

After setting a language manually with the setLocale() method the automatic detection will be switched off and overridden.

If you want to use it again, you can set the language auto with setLocale() which will reactivate the automatic detection for Zend_Translate.

Since Zend Framework 1.7.0 Zend_Translate also recognises an application wide locale. You can simply set a Zend_Locale instance to the registry like shown below. With this notation you can forget about setting the locale manually with each instance when you want to use the same locale multiple times.

<?php
// in your bootstrap file
$locale = new Zend_Locale();
Zend_Registry::set('Zend_Locale'$locale);

// default language when requested language is not available
$defaultlanguage 'en';

// somewhere in your application
$translate = new Zend_Translate(
    array(
'adapter' => 'gettext''content' => 'my_de.mo')
);

if (!
$translate->isAvailable($locale->getLanguage())) {
    
// not available languages are rerouted to another language
    
$translate->setLocale($defaultlanguage);
}

$translate->getLocale();

Using a country as language

You can also use a country as locale parameter. This could be useful when you provide your user with flags, which represent the country in which he lives, and when he selects his flag, he would automatically get the default language for this country.

For example, when the user selected US then you would get en_US in return as locale which is being used. This leads automatically to the language en which is the default language for the country US.

<?php
$translate 
= new Zend_Translate(
    array(
        
'adapter' => 'gettext',
        
'content' => 'my_de.mo',
        
'locale'  => 'US'
    
)
);

Always uppercase countries

Using this syntax you should always uppercase the input when you know that it's a country. The reason is that there are also languages which have the same letters as a country. Take for example om. You could expect to get ar_OM when you mean the country "Oman", or you could expect the language "Oromo" which is spoken in Kenia for example.

As Zend_Translate is related to languages it will always use the language in this case. Therefor always uppercase the locale when you want it to be recognised as country.

Zend Framework