通过创建额外的表格在网站中使用2种语言的最佳方式

时间:2012-07-30 06:02:40

标签: mysql cakephp internationalization

我使用cakephp建立了一个网站。用户可以在两种语言(英语和日语)之间切换以访问所有页面。 在我的后端数据库中,我想用2种语言保存数据客户端(MySQL)。目前我有两种方法:

方法1:使用1个表,其中包含2列field_en和field_ja +客户端 id | client_name_en | client_name_ja |其他... 2 |早上|阿莎| ...

方法2:使用2个表,第二个表将以2种语言存储id和对应的名称 +客户端 id |其他...

+ clients_lang  [client_id,language_id,client_name]

第1行:2 | 1 |早晨

第2行:2 | 2 |阿沙

注意:1表示英语,2表示日语。

哪种方法更适合我的情况。

2 个答案:

答案 0 :(得分:1)

首先见下面的网址

http://book.cakephp.org/2.0/en/core-libraries/internationalization-and-localization.html

OR

试试这个

//国际化您的应用程序

<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;
    }
}

或:

<?php
// View code
echo $this->element(Configure::read('Config.language') . '/tos');

// 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'));
    }
}
?>

///翻译模型验证错误

<?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));

答案 1 :(得分:0)

如果你只有这两种语言是最好的解决方案之一,如果语言可以更多不是最好的解决方案。

我建议您使用第二种方法将相同的代码用于其他网站,并且只需要插入新语言。