Zend Framework set on bootstrap.php custom locale

550 views Asked by At

how to set custom locale in zend framework by create a function in bootstrap file? I need to change error messege in zend form.

Thanks for support

1

There are 1 answers

3
Nandakumar V On

You can use this code to set the locale

// within your bootstrap
$locale = new Zend_Locale('de_AT');
Zend_Registry::set('Zend_Locale', $locale);

// within your model or controller
$date = new Zend_Date();
print $date->getLocale();
echo $date->getDate();   

More info on the Zend_Locale

For adding translated error message to the form validators you have to add them to the Zend_Validate_Abstract

From the ZF manual

Zend Framework is shipped with more than 45 different validators with more than 200 failure messages. It can be a tedious task to translate all of these messages. But for your convenience Zend Framework comes with already pre-translated validation messages. You can find them within the path /resources/languages in your Zend Framework installation. So to translate all validation messages to German for example, all you have to do is to attach a translator to Zend_Validate using these resource files.

 $translator = new Zend_Translate(
      'array',
      '/resources/languages',
      $language,
      array('scan' => Zend_Locale::LOCALE_DIRECTORY)
  );
  Zend_Validate_Abstract::setDefaultTranslator($translator);  

More info on the validator error message is in their manual Using pre-translated validation messages

I havent personally used the translation adaptors, but from the manual Creating CSV source files for Translation it seems the below code will add the translator to the application

$translate = new Zend_Translate(
    array(
        'adapter' => 'csv',
        'content' => '/path/to/mytranslation.csv',
        'locale'  => 'de'
    )
);  

Also a sample of the .csv format

message1;Nachricht1
message2;Nachricht2