2016-11-11 5 views
0

私は多言語で私のアプリケーションをコーディングしようとしています。私がdefault.potを作成するとき、私の選択オプションは翻訳されません。CakePHP3の選択肢を翻訳するには

//UsersTable 
<?php 

class UsersTable extends Table { 

    public $userRoles = ['admin' => 'Administrator', 'author' => 'Author' ...]; 

} 


// UsersController 
public function edit($userId = 0) { 
    $user = $this->Users->newEntity(); 
    $this->set("user", $user); 
    $this->set("userRoles", $this->Users->userRoles); 
} 

//edit.ctp 
<?php 
echo $this->Form->create($user); 
echo $this->Form->input("role",["type" => "select","options" => $userRoles,"label" => __("Userrole")]); 

私はこのような何か試してみました:

$_userRoles = []; 
foreach ($userRoles as $key => $value) { 
    $_userRoles[$key] = __($value); // tried also __($value, true) 
} 

をが、これは国際化エキス中にエラーが発生します。

このエントリの翻訳をどのように入手できますか?

マット

答えて

0

ではなく、テーブルクラスの私はあなたがエンティティクラスのオプションを定義するために持っていると思うありがとう

がUserControllerの

namespace App\Model\Entity; 
use Cake\ORM\Entity; 
use Cake\I18n\I18n; //important to use this 

class User extends Entity { 

    ..... 

    private $userRoles = ['admin' => 'Administrator', 'author' => 'Author' ...]; 

    ...... 

    protected function _getUserRoles() { 
     $return=(); 
     foreach($this->userRoles as $k=>$val) 
     { 
      $return[$k]=__($value); 
     } 
     return $return; 
    } 

    ...... 
} 

編集方法以下のようにエンティティの変更を行います

public function edit($userId = 0) { 
    $user = $this->Users->newEntity(); 
    $this->set("user", $user); 
    $this->set("userRoles", $user->user_roles); 
} 

あなたedit.ctp

echo $this->Form->create($user); 
echo $this->Form->input("role",["type" => "select","options" => $userRoles,"label" => __("Userrole")]); 

OR

私はそれがHi

+0

あなたに役立つことを願っています

use Cake\I18n\I18n; class UsersTable extends Table { 

\クラス宣言の前にテーブルのクラスファイルの行の下に追加します。ご協力いただきありがとうございます。 "Table" App \ Model \ Table \ UsersTable "は" userRoles "に関連付けられていません。 UserTableの行を追加しても効果はありません。 –

+0

Sry、私のせい。私のテーブルクラスには残った残骸がありました。実行時にエラーはありませんが、ポットファイルを抽出しようとすると「無効なマーカーコンテンツ__($ val)」が表示されます。 –

関連する問題