Translate string within array with __ in CakePHP
I have an array of different priorities in my controller:
var $priorities = array(3 => 'Low', 2 => 'Medium', 1 => 'High');
How do I now manage to translate these values with the __-Function? I get an array that PHP expects a closing ')' bracket. This is the code I tried to use:
var $priorities = array(3 => __('Low'), 2 => __('Medium'), 1 => __('High'));
I use t开发者_StackOverflow社区his variable to set it in my add and edit-action. These are options in a select-input and if theres a change, I don't want to fiddle around in the views.
Judging by the var
keyword I suspect you're trying to declare a class property here. This doesn't work, you can only declare properties using static values, i.e. you can't call any functions at this point or do any operations.
You'll need to translate the values at some later point, or assign them to $this->priorities
later on. The __construct
method would be a good place, if it's a controller beforeFilter
is good too.
You'll also need to call the __
function with true
as the second parameter:
$this->priorities = array(3 => __('Low', true), 2 => __('Medium', true), 1 => __('High', true));
Why not try array_map
?
var $priorities = array(3 => 'Low', 2 => 'Medium', 1 => 'High');
$priorities = array_map("__", $priorities, true);
Make sure to put translations in proper places. more information here about Internationalization & Localization
精彩评论