My translation set-up works perfectly (I mean, fine :) ) in Firefox on OSX. On other browser the translation stay in french, my default language.
I var_dump this in the view:
var_dump($this->translate('Envoyer')):
string 'Send' (length=4)
var_dump($this->translate()->getLocale()):
string 'en' (length=2)
Here my init_translate in bootstrap.php:
public function _initTranslate()
{
Zend_Loader::loadClass('Zend_Translate');
Zend_Loader::loadClass('Zend_Registry');
////////////////////////////
// Validator messages translation
$language = 'fr';
// On définit le traducteur à utiliser
$translate = new Zend_Translate(
array(
'adapter' => 'array',
'content' => APPLICATION_PATH . '/../resources/languages',
'locale' => $language,
'scan' => Zend_Translate::LOCALE_DIRECTORY
));
// Apply only on validation messages :
Zend_Validate_Abstract::setDefaultTranslator($translate);
///////////////////////////
// Site translation
// Get current registry
$registry = Zend_Registry::getInstance();
/**
* Set application wide source Locale
* This is usually your source string language;
* i.e. $this->translate('Hi I am an English String');
*/
$locale = new Zend_Locale('fr_CA');
Zend_Registry::set('Zend_Locale', $locale);
$session = new Zend_Session_Namespace('session');
//$langLocale = isset($session->lang) ? $session->lang : $locale;
//$langLocale = $locale;
/**
* Set up and load the translations (all of them!)
* resources.translate.options.disableNotices = true
* resources.translate.options.logUntranslated = true
*/
$translate = new Zend_Translate('gettext',
APPLICATION_PATH . DIRECTORY_SEPARATOR .'languages', 'auto',
array(
'disableNotices' => true, // This is a very good idea!
'logUntranslated' => false, // Change this if you debug
)
);
/**
* Both of these registry keys are magical and makes
* ZF 1.7+ do automagical things.
*/
$registry->set('Zend_Locale', $locale);
$registry->set('Zend_Translate', $translate);
return $registry;
}
My language switcher plugin (Author: Danny Froberg ):
class My_Controller_Plugin_LangSelector extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$registry = Zend_Registry::getInstance();
// Get our translate object from registry.
$translate = $registry->get('Zend_Translate');
$currLocale = $translate->getLocale();
// Create Session block and save the locale
$session = new Zend_Session_Namespace('session');
$lang = $request->getParam('lang','');
// Register all your "approved" locales below.
switch($lang) {
case "fr":
$langLocale = 'fr_CA'; break;
case "en":
$langLocale = 'en_CA'; break;
default:
/**
* Get a previously set locale from session or set
* the current application wide locale (set in
* Bootstrap)if not.
*/
//$langLocale = isset($session->lang) ? $session->lang: $currLocale;
$langLocale = 'fr_CA'; break;
}
$newLocale = new Zend_Locale();
$newLocale->setLocale($langLocale);
$registry->set('Zend_Locale', $newLocale);
$translate->setLocale($langLocale);
$session->lang = $langLocale;
// Save the modified translate back to registry
$registry->set('Zend_Translate', $translate);
}
}
And my selector in view:
<a href="#" onclick="dopostLang(this.href, 'fr');">FR</a>
<a href="#" onclick="dopostLang(this.href, 'en');">EN</a>
The javascript do is job (the var_dump show the actual locale; "fr" when "FR" clicked and "en" when "EN" clicked), so I dont understand why I'm facing a browser issue?
I'm lost, if you may get a look at it and point me a direction to go please, I'll really appreciate.
thanks,
Yahoo! Self-Resolved! (but dont know why) :)
But if somebody else fall in the same dummer trap :)
First of all enable translate notice in .config file:
Secondo, follow the official guide first then try something else. For a mysterious reason, this lines cause my problem:
I replaced with this lines (from the offical doc: http://framework.zend.com/manual/1.12/en/zend.translate.using.html):
thanks :)