首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP 3:如何接收翻译的内容?

CakePHP 3:如何接收翻译的内容?
EN

Stack Overflow用户
提问于 2015-09-15 08:54:55
回答 1查看 813关注 0票数 2

我需要收到一个单独的翻译内容,以填写表格。

控制器中的

代码语言:javascript
复制
public function translate($id = null)
    {
        $this->Apartments->locale('deu');
        $apartment = $this->Apartments->get($id);
        .....
        $this->set(compact('apartment'));
        $this->set('_serialize', ['apartment']);
    }

我还没收到翻译的内容!

中的

代码语言:javascript
复制
public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('apartments');
        $this->displayField('name');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');
        $this->addBehavior('Translate', ['fields' => ['description']]);
    }

接收翻译后的内容的正确方法是什么?我需要在表类和实体类中添加什么才能使其工作?

更新 SQL查询

代码语言:javascript
复制
SELECT Apartments.id AS `Apartments__id`,
       Apartments.user_id AS `Apartments__user_id`,
       Apartments.name AS `Apartments__name`,
       Apartments.slug AS `Apartments__slug`,
       Apartments.description AS `Apartments__description`,
       Apartments.number_of_rooms AS `Apartments__number_of_rooms`,
       Apartments.number_of_beds AS `Apartments__number_of_beds`,
       Apartments.badroom AS `Apartments__badroom`,
       Apartments.surface AS `Apartments__surface`,
       Apartments.internet AS `Apartments__internet`,
       Apartments.air_condition AS `Apartments__air_condition`,
       Apartments.parking AS `Apartments__parking`,
       Apartments.pets AS `Apartments__pets`,
       Apartments.created AS `Apartments__created`,
       Apartments.modified AS `Apartments__modified`,
       Apartments.active AS `Apartments__active`,
       Apartments_description_translation.id AS `Apartments_description_translation__id`,
       Apartments_description_translation.locale AS `Apartments_description_translation__locale`,
       Apartments_description_translation.model AS `Apartments_description_translation__model`,
       Apartments_description_translation.foreign_key AS `Apartments_description_translation__foreign_key`,
       Apartments_description_translation.field AS `Apartments_description_translation__field`,
       Apartments_description_translation.content AS `Apartments_description_translation__content`
FROM apartments Apartments
LEFT JOIN i18n Apartments_description_translation ON (Apartments_description_translation.model = 'Apartments'
                                                      AND Apartments_description_translation.field = 'description'
                                                      AND Apartments_description_translation.locale = 'deu'
                                                      AND Apartments.id = (Apartments_description_translation.foreign_key))
WHERE Apartments.id = 1 LIMIT 1
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-15 09:54:54

你在评论中提到:

我使用一个现有的数据库,其中包含基于CakePHP 2的应用程序输入的数据。..。在i18n表中,我有一个模型名公寓,但是cakephp3搜索公寓。

模型字段值是CakePHP 2(单数)中模型的名称,CakePHP 3中的表名(复数)。

要解决这个问题,您可以:

迁移翻译表中的数据

这是最符合逻辑的解决方案,除非仍然有CakePHP 2.x代码查看翻译记录,只需更新翻译记录:

代码语言:javascript
复制
UPDATE i18n set model = "Apartments" where model = "Apartment";
... etc ...

更改所使用的引用

默认情况下,模型字段是表类的名称,但它不一定是可配置

代码语言:javascript
复制
public function initialize(array $config)
{
    parent::initialize($config);

    ...
    $this->addBehavior('Translate', [
        'fields' => ['description'],
        'referenceName' => 'Apartment' # <- 
    ]);
}

通过这种方式,CakePHP 3.x代码将与早期版本兼容。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32581772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档