2014-11-04

Localization in zend framework 1

In zf1 you can localize text, date, numbers and many other.
First of all you have to create folders for each locale. E.g. for russian locale: application/languages/ru_RU/, where ru - locale, RU - country.
Then you can create files with translations. As example create FormLabel.csv in your ru_RU folder:
"Login:";"Логин:"
"Password:";"Пароль:"
"Password confirmation:";"Подтверждение пароля:"
"First name:";"Имя:"
"Last name:";"Фамилия:"
Also you need to add locale initialization to your bootstrap file (application/Bootstrap.php):
protected function _initTranslate()
{
  $locale = new Zend_Locale('ru_RU');
  $registry = Zend_Registry::getInstance();
  $registry->set('Zend_Locale', $locale);

  $translate = new Zend_Translate(Zend_Translate::AN_CSV, APPLICATION_PATH . '/languages', null,
      ['scan' => Zend_Translate::LOCALE_DIRECTORY]);

  $registry->set('Zend_Translate', $translate);
}
From this moment localization will be enabled and if you create form with label "Login:" it'll be displayed as "Логин:".
But if you want to handy translate text in your templates add this function
function __()
{
    return Zend_Registry::get('Zend_Translate')->translate(func_get_args());
}
after
require_once 'Zend/Application.php';
to public/index.php.
And then in your phtml you can translate text:
<?= __('Password:') ?>

No comments:

Post a Comment