0

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

方法 1:使用 1 个包含 2 列 field_en 和 field_ja +clients id | 的表 client_name_zh | 客户名称_ja | 其他... 2 | 早上 | 阿莎 | ...

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

+clients_lang [client_id, language_id, client_name]

第 1 行:2 | 1 | 早晨

第 2 行:2 | 2 | 阿莎

注:1 为英文,2 为日文。

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

4

2 回答 2

1

先看下面的网址

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

或者

尝试这个

//国际化你的应用程序

<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));
于 2012-07-30T15:15:15.623 回答
0

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

我建议您的方法是使用第二种方法在其他站点中使用相同的代码,您只需插入新语言。

于 2012-07-30T09:56:50.760 回答