Reputation: 1233
I want to be able to test my cakephp web page using the localization features.
I use the translate function __() and I also use the date and time function: toLocaleString()
I wanted to know how can I test the translation and localization in a simple way.
I know that toLocaleString()
will output the date and time in the local format.
I tried using the following code in my controller's beforeFilter():
$this->Session->write('Config.langauge', 'ger');
Configure::write('Config.language', 'fre');
The above two lines of code didn't work. This did not work either:
setlocale(LC_ALL, 'de', 'ge');
I am using Ubuntu 10.04. I also installed a spanish, french and german language pack.
In the cakephp debug toolkit, it shows that the language has changed, but the date and time string do not change at all. I am not sure what I am doing wrong.
As for testing, the date and time should work once the locale is set, but for the translate functions, how do I test those? I looked into the cakephp documentation, but it says to use the i18n console commang. I tried running the command to extract pot files and I chose my source and output directory, but nothing showed up in the directory when it was done.
Thanks
Upvotes: 0
Views: 5303
Reputation: 7762
See the below url
http://book.cakephp.org/2.0/en/core-libraries/internationalization-and-localization.html
Or try this:
//Internationalizing Your Application
<h2><?php echo __('Posts'); ?></h2>
The default domain is ‘default’, therefore your locale folder would look something like this:
/app/Locale/eng/LC_MESSAGES/default.po (English)
/app/Locale/fre/LC_MESSAGES/default.po (French)
/app/Locale/por/LC_MESSAGES/default.po (Portuguese)
<?php
// App Controller Code.
public function beforeFilter() {
$locale = Configure::read('Config.language');
if ($locale && file_exists(VIEWS . $locale . DS . $this->viewPath)) {
// e.g. use /app/View/fre/Pages/tos.ctp instead of /app/View/Pages/tos.ctp
$this->viewPath = $locale . DS . $this->viewPath;
}
}
or:
<?php
// View code
echo $this->element(Configure::read('Config.language') . '/tos');
//Localization in CakePHP
<?php
Configure::write('Config.language', 'fre');
?>
<?php
$this->Session->write('Config.language', 'fre');
?>
<?php
class AppController extends Controller {
public function beforeFilter() {
Configure::write('Config.language', $this->Session->read('Config.language'));
}
}
?>
///Translating model validation errors
<?php
class User extends AppModel {
public $validationDomain = 'validation';
public $validate = array(
'username' => array(
'length' => array(
'rule' => array('between', 2, 10),
'message' => 'Username should be between %d and %d characters'
)
)
)
}
?>
//Which will do the following internal call:
<?php
__d('validation', 'Username should be between %d and %d characters', array(2, 10));
Upvotes: 3