如何在cakePhp中本地化字符串?我没有在线文档取得任何成功。谢谢你的帮助。
答案 0 :(得分:3)
有几个步骤:
.po
个文件__()
或__d()
辅助方法以下是我的一个项目的摘录:
# app/app_controller.php
uses ( 'L10n' );
class AppController extends Controller {
public function beforeFilter() {
/**
* Derive the desired locale by reading the subdomain from
* the HTTP_HOST server variable. Locale subdomains can use
* either the 2 or 3 character ISO code. Information on locale
* ISO codes is at http://www.loc.gov/standards/iso639-2/php/code_list.php.
*/
$this->L10n = new L10n();
/** Auto-detect the request language settings */
$this->L10n->get();
/**
* Set the default "domain" for translations. The domain is the
* same as the po file name in a given locale directory. e.g.
* __d( 'homepage', 'message_id' ) would look for the
* message_id key in homepage.po. Using the __() convenience
* function will always look in default.po.
*/
$this->set( 'domain', 'default' );
}
# The rest of your AppController code
}
该片段将设置语言。您需要做的就是在.po
目录中提供相应的/app/locale/eng/LC_MESSAGES/
文件。我认为CakePHP书提供sufficient information on this。
如果您选择仅使用一个.po
文件,则将使用__()
帮助程序包装字符串。我选择了多个.po
文件以避免一个大型文件,因此我使用了__d()
帮助器,以便我可以指定哪个域(domain == .po
文件的名称而不使用{ {1}}扩展名。)
<强>更新强>
我应该补充一点,您需要使用.po
行为来帮助您处理需要翻译的数据库内容。
答案 1 :(得分:0)
CakePHP 3.x本地化
为动态语言更改创建操作
路线
$routes->connect('/locale', ['controller' => ' Users', 'action' => 'languageChange']);
控制器操作
<强> UsersController 强>
use Cake\Http\Session;
use Cake\I18n\I18n;
//Before filter in users controller
public function beforeFilter(\Cake\Event\Event $event)
{
parent::beforeFilter($event);
}
/**
* Change language
*/
public function languageChange()
{
if ($this->request->is('post')) {
$session = $this->getRequest()->getSession();
if (!empty($this->request->getData('locale'))) {
$session->write('Config.language', $this->request->getData('locale'));
$this->redirect($this->referer());
} else {
$session->write('Config.language', I18n::getLocale());
$this->redirect($this->referer());
}
}
}
创建视图文件意味着language_change.ctp文件
<?php
echo $this->Form->create("Localizations", array('url' => '/locale'));
echo $this->Form->radio("locale", [
['value' => 'en_US', 'text' => 'English'],
['value' => 'de_DE', 'text' => 'German'],
['value' => 'fr_FR', 'text' => 'French'],
]);
echo $this->Form->button('Change Language');
echo $this->Form->end();
?>
<h2><?php echo __('Hello'); ?></h2>
<h2><?php echo __('How are you?'); ?></h2>
<h2><?php echo __('Wel Come'); ?></h2>
<h2><?php echo __('Good Job'); ?></h2>
<强> AppController的强>
use Cake\I18n\I18n;
use Cake\Http\Session;
use Cake\Core\Configure;
public function beforeFilter(Event $event)
{
$session = $this->getRequest()->getSession();
if ($session->check('Config.language')) {
I18n::setLocale($session->read('Config.language'));
} else {
$session->write('Config.language', I18n::getLocale());
}
}
在文件中创建此位置 SRC /地点/ EN_US / default.po
msgid "Hello"
msgstr "Hello"
msgid "How are you?"
msgstr "How are you?"
msgid "Wel Come"
msgstr "Wel Come"
msgid "Good Job"
msgstr "Good Job"
的src /地点/ de_DE这个/ default.po
msgid "Hello"
msgstr "Hallo"
msgid "How are you?"
msgstr "Hoe gaat het met je?"
msgid "Wel Come"
msgstr "Wel kom"
msgid "Good Job"
msgstr "Goed gedaan"
在boostrap.php中配置
&#39;会话&#39; =&GT; [ &#39;默认&#39; =&GT; &#39; PHP&#39 ;, &#39;语言&#39; =&GT; &#39; EN_US&#39; ]