Tuesday, 5 February 2013

Using translation (internationalization) in Zend Framework

In Zend, if we developing a site with multiple lanuguage than here is the step to go ahead:
Step 1. Make changes in bootstrap file

protected function _initTranslation(){
$defaultLocale= 'en';

$options = array('delimiter' => ';','limit' => 0,'enclosure' => '"');

$translate = new Zend_Translate_Adapter_Csv(
array(
'adapter' => 'csv',
'content' => APPLICATION_PATH.'/lang/lang-en.csv',
'locale' => 'en',
)
);
$translate->addTranslation(APPLICATION_PATH.'/lang/lang-tr.csv', 'tr');
$language = new Zend_Session_Namespace('translation');
$language->lang = isset($language->lang) ? $language->lang : $defaultLocale;
$translate->setLocale($language->lang);
//Get the list of all locale
//pr($translate->getList());
//Get current locale
//$translate->getLocale();
Zend_Registry::set('Zend_Translate', $translate);
}


STEP 2: Create a folder lang under the application and put 2 csv files lang-en.csv and lang-tr.csv
each file contents keywords and their corresponding language translation in the file.

STEP 3: Create a view helper named Translate.php

class Zend_View_Helper_Translate {
public function translate($message = null) {
$translate = Zend_Registry::get('Zend_Translate');
return $translate->_($message);
}
}


STEP 4: Now you can use in the phtml like

translate('About')?>


Thats all

2 comments :

  1. how to create lang-en.csv and lang-tr.csv

    ReplyDelete
  2. Very Simple Suresh,
    1. Open any spreedsheet program.
    2. For lang-en.csv:
    Put english words in both the column
    For lang-tr.csv:
    Put english in first column and corresponding turkey translation in next column
    3. Save the files as csv with the delimator as defied in the option just here ";"
    4. Put these file in the 'lang' as specified.

    Hope it help you

    ReplyDelete