Reputation: 59
Please provide me the folder strusture and code for simple small application for zend_translate and zend_locale.
So that I will have some idea about it. I referred framework.zend but could not understand what are the things which I should mention in controller, index, boorstrap even view. It seems hecic for me to work with zend_translate and zend_locale. Please help me.
Upvotes: 3
Views: 2765
Reputation: 1176
This is the solution I am using:
Code for Plugin.php
// BEGIN: Translate
$validLang = $this->getRequest()->getParam('lang');
$translate = new Zend_Translate('csv', 'data/lang/en.csv', 'en');
$translate->addTranslation('data/lang/ro.csv', 'ro');
if($validLang)
{
Zend_Registry::set('lang', $validLang);
$translate->setLocale($validLang);
}
else
{
Zend_Registry::set('lang', 'en');
$translate->setLocale('en');
}
Zend_Registry::set('translate', $translate);
// END: Translate
Code for en.csv
front_user_menu-wall;Wall
Code for ro.csv
front_user_menu-wall;Perete
Code for any Zend View file
echo Zend_Registry::get('translate')->_('front_user_menu-wall');
And some explanations:
In folder data/lang I have 2 .csv translation files. The structure is self explanatory, the semicolon (;) separates the variable name (front_user_menu-wall) from the variable value (Wall). Each variable must be placed on a new line and you can have line comments inside (ex: #this is a comment). The ro.csv file is the romanian translation file, it has the same structure, but obviously different values for the variables.
Inside Plugin.php you have
$validLang = $this->getRequest()->getParam('lang);
Zend equivalent for
$validLang = $_GET['lang'];
On the next 2 lines of code we instantiate a new Zend_Translate class using the 2 translation .csv files. Next we check if $validLang is not empty, if it's not them we set the Zend_Registry lang variable (Zend_Registry is similar with $_SESSION) with the translation file. If $validLang is empty then we default the translation to english.
If you did all the steps above, you can now echo
Zend_Registry::get('translate')->_('front_user_menu-wall');
to get the english or romanian translation.
Upvotes: 1
Reputation: 72
This is how I am doing it,
Bootstrap.php
protected function _initTranslate()
{
$translate = new Zend_Translate(array(
'adapter' => 'gettext',
'content' => APPLICATION_PATH . '/locale/en-US.mo',
'locale' => 'en'
));
Zend_Registry::set('translate', $translate);
Zend_Form::setDefaultTranslator($translate);
$this->bootstrap('view');
$view = $this->getResource('view');
$view->translate = $translate;
}
If I have to send a message from the controller . . I do
AuthController.php
$this->view->message = 'success';
And then in the view
login.phtml
if (isset($this->message)) {
echo $this->translate->_($this->message);
}
The above will output the translated value of 'sucesss' from 'en-US.mo' file.
Hope that helps!
Upvotes: 1
Reputation: 176
It's my solution:
protected function _initTranslate()
{
//set gettext adapter
$translate = new Zend_Translate('Gettext', APPLICATION_PATH . '/languages',
null, array('scan' => Zend_Translate::LOCALE_FILENAME));
// get locale from url
if (preg_match("/^\/([a-zA-Z]{2})($|\/)/", $_SERVER['REQUEST_URI'], $matches)) {
$lang = $matches[1];
} else {
//if locale not exsist in url - get browser locale
$locale = new Zend_Locale(Zend_Locale::BROWSER);
$lang = $locale->getLanguage();
}
if (!$translate->isAvailable($lang)) {
$lang = 'en';
}
$translate->setLocale($lang);
$front = $this->getResource('FrontController');
$front->setBaseUrl('/' . $lang . '/');
Zend_Registry::set('Zend_Translate', $translate);
$locale = $translate->getLocale();
$source = APPLICATION_PATH . '/languages/'.$locale.'.mo';
$translate->addTranslation($source, $locale);
}
Upvotes: 0
Reputation: 1774
To give you an example, for simple translations I have the following defined in my application.ini:
resources.translate.data = APPLICATION_PATH "/translations/nl/"
resources.translate.locale = "nl"
And inside the /translations/nl/ directory I have one or more translation files (.php, .ini, ...).
That's all you need, essentially.
Upvotes: 1