2013-04-27 85 views
0

我查詢所有的行從表中,我得到的所有排在所有語言......我怎麼能從i18n表中得到一個結果? (CakePHP的)

例如: GalleryCategoriesController.php

<?php 
class GalleryCategoriesController extends AppController 
   { 
   function index() 
       { 
       $galleryCategories = $this->GalleryCategory->find('all'); 
       print_r($galleryCategories); 
       $this->set('galleryCategories', $galleryCategories); 
       } 
   } 
?> 

GalleryCategory.php(模型)

<?php 
class GalleryCategory extends AppModel 
   { 
   public $tablePrefix = 'ef_'; 
   var $name = 'GalleryCategory'; 

   public $actsAs = array('Translate' => array(
           'title' => 'titleTranslation', 
           'title_sub' => 'titleSubTranslation', 
           'description' => 'descriptionTranslation' 
           ) 
       ); 
   } 
?> 

結果:

Array 
   (
   [0] => Array 
       (
       [GalleryCategory] => Array 
           (
           [id] => 1 
           [gallery_category_id] => 0 
           [title] => Test title 
           [title_sub] => Test subtitle 
           [description] => Test description 
           [status] => 2 
           [locale] => hun 
           ) 
        [titleTranslation] => Array 
           (
           [0] => Array 
               (
               [id] => 65 
               [locale] => hun 
               [model] => GalleryCategory 
               [foreign_key] => 1 
               [field] => title 
               [content] => Test title hungarian 
               ) 
           [1] => Array 
               (
               [id] => 80 
               [locale] => eng 
               [model] => GalleryCategory 
               [foreign_key] => 1 
               [field] => title 
               [content] => Test title english 
               ) 
           ) 
        [titleSubTranslation] => Array 
           ( 
           [0] => Array 
               ( 
               [id] => 66 
               [locale] => hun 
               [model] => GalleryCategory 
               [foreign_key] => 1 
               [field] => title_sub 
               [content] => Test subtitle hungarian 
               ) 
           [1] => Array 
               ( 
               [id] => 81 
               [locale] => eng 
               [model] => GalleryCategory 
               [foreign_key] => 1 
               [field] => title_sub 
               [content] => Test subtitle english 
               ) 
           ) 
        [descriptionTranslation] => Array 
           ( 
           [0] => Array 
               ( 
               [id] => 67 
               [locale] => hun 
               [model] => GalleryCategory 
               [foreign_key] => 1 
               [field] => description 
               [content] => Test description hungarian 
               ) 
           [1] => Array 
               ( 
               [id] => 82 
               [locale] => eng 
               [model] => GalleryCategory 
               [foreign_key] => 1 
               [field] => description 
               [content] => Test description english 
               ) 
           ) 
       ) 
   ) 

我怎樣才能得到匈牙利語翻譯?因爲如果我在網站上有六種語言,並且我查詢了任何內容,我就可以用六種語言獲得它...這太可怕了......

回答

1

您的模型定義必須根據the manual更改: 嘗試類似:

<?php 
class GalleryCategory extends AppModel 
    { 
    public $tablePrefix = 'ef_'; 
    var $name = 'GalleryCategory'; 

    public $actsAs = array('Translate' => array(
      'title', 'title_sub', 'description' 
      ) 
     ); 
    } 
?> 

當然你還需要選擇語言環境:

$this->GalleryCategory->locale = <hun|eng> 
在你的控制器動作

+0

謝謝!它正在工作! 但是,我使用此: '會話級>寫入( 'Config.language',$朗); \t \t \t} \t \t $ this-> redirect(Controller :: referer()); \t \t die; \t \t} \t} >' 因此,有沒有必要? '$這個 - > GalleryCategory->區域= ' – 2013-04-28 06:30:42

相關問題