Mysql 在网站中使用两种语言的最佳方法是创建额外的表
我用cakephp建立了一个网站。用户可以在两种语言(英语和日语)之间切换以访问所有页面。 在我的后端数据库中,我想用两种语言保存数据客户端(MySQL)。目前我有两种方法: 方法1:使用1个表和2列字段en和字段ja +客户 id |客户名称|客户名称|其他。。。 2 |早上|阿莎| 方法2:使用2个表,第二个表将以2种语言存储id和相应的名称 +客户 id |其他人 +乌朗 [客户id、语言id、客户名称] 第1排:2 | 1 |上午 第2排:2 | 2 |阿莎 注:1为英语,2为日语Mysql 在网站中使用两种语言的最佳方法是创建额外的表,mysql,cakephp,internationalization,Mysql,Cakephp,Internationalization,我用cakephp建立了一个网站。用户可以在两种语言(英语和日语)之间切换以访问所有页面。 在我的后端数据库中,我想用两种语言保存数据客户端(MySQL)。目前我有两种方法: 方法1:使用1个表和2列字段en和字段ja +客户 id |客户名称|客户名称|其他。。。 2 |早上|阿莎| 方法2:使用2个表,第二个表将以2种语言存储id和相应的名称 +客户 id |其他人 +乌朗 [客户id、语言id、客户名称] 第1排:2 | 1 |上午 第2排:2 | 2 |阿莎 注:1为英语,2为日语 哪种
哪种方法更适合我的情况。如果您只有这两种语言,那么解决方案一更好,如果语言可以更多,那么这不是最佳解决方案 我建议您的方法是使用第二种方法,在其他站点中使用相同的代码,您只需插入新的语言。请先参见下面的url 或 试试这个 //国际化您的应用程序
<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;
}
}
//默认域为“默认”,因此您的区域设置文件夹如下所示:
/app/Locale/eng/LC_MESSAGES/default.po(英语)
/app/Locale/fre/LC_MESSAGES/default.po(法语)
/app/Locale/por/LC_MESSAGES/default.po(葡萄牙语)
///转换模型验证错误
<?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));
//将执行以下内部调用:
内置的TranslateBehavior怎么样?问问你自己……当你必须添加另一种语言时,哪种方法最简单。
<?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'));
}
}
?>
<?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));