Skip to end of metadata
Go to start of metadata

<p>This code snippet provides a Zend Framework (1.5.0) plugin that will auto-detect the browser language setting and make translations available to views via Zend_Registry.</p>

<h3>MyPlugin_Language class</h3>

<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
/**

  • Requires ZF 1.5.x or above
    */
    class MyPlugin_Language extends Zend_Controller_Plugin_Abstract
    {
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
    $locale = new Zend_Locale();

$frontendOptions = array('cache_id_prefix' => 'MyApp', 'lifetime' => 86400);
$backendOptions = array('cache_dir' => 'cache');
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
Zend_Translate::setCache($cache);

$options = array('scan' => Zend_Translate::LOCALE_DIRECTORY);

$translate = new Zend_Translate('csv', 'languages', 'auto', $options);

if (!$translate->isAvailable($locale->getLanguage()))

Unknown macro: { $locale->setLocale('en'); }

$translate->setLocale($locale);

setcookie('lang', $locale->getLanguage(), null, '/');

Zend_Registry::set('locale', $locale->getLanguage());
Zend_Registry::set('Zend_Translate', $translate);
}
}
]]></ac:plain-text-body></ac:macro>

<p>Then when in a view script you can simply call:</p>

<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
$this->translate('stringToTranslate');
]]></ac:plain-text-body></ac:macro>

<p>If you need translation in a helper you need to add a <code>setView</code> function e.g.</p>

<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[

class Zend_View_Helper_Preferences
{
public $view;

public function setView(Zend_View_Interface $view)

Unknown macro: { $this->view = $view; }

public function preferences()
{
$content =<<<EOH
<div class="title"><label>{$this->view->translate('timeZone')}</label></div>
<div>
<select name="_timeZone">
<option value="Europe/London">Europe/London</option>
</select>
</div>
</div>

EOH;
return $content;
}
}
]]></ac:plain-text-body></ac:macro>

Labels:
zend_translate zend_translate Delete
translate translate Delete
zend_locale zend_locale Delete
locale locale Delete
language language Delete
plugin plugin Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.