在 CakePHP 中使用 __ 翻译数组中的字符串
我的控制器中有一系列不同的优先级:var $priorities = array(3 => 'Low', 2 => 'Medium', 1 => 'High');
我现在如何设法使用 __ 来转换这些值-功能?我得到一个数组,PHP 需要一个右括号“)”。这是我尝试使用的代码:var $priorities = array(3 => __('Low'), 2 => __('Medium'), 1 => __('High'));
我用这个变量以在我的添加和编辑操作中设置它。这些是选择输入中的选项,如果有更改,我不想在视图中摆弄。
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 this 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不尝试 array_map 呢?
确保将翻译放在正确的位置。更多信息这里有关国际化和国际化本地化
Why not try
array_map
?Make sure to put translations in proper places. more information here about Internationalization & Localization
从
var
关键字来看,我怀疑您试图在此处声明一个类属性。这是不行的,你只能使用静态值声明属性,即此时你不能调用任何函数或执行任何操作。您需要在稍后的某个时刻转换这些值,或者稍后将它们分配给
$this->priorities
。__construct
方法将是一个好地方,如果它是一个控制器beforeFilter
也不错。您还需要调用
__
函数,并将true
作为第二个参数: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 controllerbeforeFilter
is good too.You'll also need to call the
__
function withtrue
as the second parameter: