Internationalization in CakePHP
Using locale data in your application is simple. The first step is to tell CakePHP which language the application should utilize to serve up content. This can be done by detecting subdomains (en.example.com versus fra.example.com), or by gleaning from the browser’s user-agent, among other things.
Language switching is best done in the controller:
Простой текст$this->L10n = new L10n();$this->L10n->get("eng");Configure::write('Config.language', "en");
You may wish to place this code in the beforeFilter so that each action in the controller is displayed using the correct language, or you may want to place this in an action that handles authentication or locale switching from a default.
Displaying localized content is done using the __() convenience function. This function is globally available, but it will probably be best utilized in your views. The first parameter of the function is the msgid defined in the .po files. The localized content is echo()’d by default, but an optional second parameter causes the value be returned instead (handy for highlighting or linking using the TextHelper, for example). The short example below shows how to output localized content using the __() function. The actual output depends on the locale that has been selected using the L10n class and the active configuration setting.
Простой текст<?php __("purchase"); ?>
Remember to use the escape parameter of different helper methods if you plan to use localized data as part of helper method call. Notice the usage of the second parameter of __() to return the data rather than echo() it:
Простой текст<?phpecho $form->error('Card.cardNumber',__("errorCardNumber", true),array('escape' => false));?>
If you would like to have all of your validation error messages translated by default, a simple solution would be to add the following code in you app_model.php:
Простой текстfunction invalidate($field, $value = true) {return parent::invalidate($field, __($value, true));}


Коментарии:
Добавить коментарий