cakephp 的简单本地化问题
我有一个包含两个字段的表,name 和 name_spa
要检索正确的语言,我必须在视图中执行类似的操作:
if($locale=='eng'){
echo $var['Model']['name'];
}else{
echo $var['Model']['name_spa'];
}
但是,我想做这样的操作:
echo $var['Model']['lname'][$locale];
我尝试使用虚拟字段,但这不起作用。我没有使用 i18n,因为它对于我的需要来说太复杂了。
public $virtualFields = array($lname => array(
'enh' => $this->name,
'spa' => $this->name_spa,
));
I have a table with two fields, name and name_spa
To retrieve the correct language, I have to do something like this in the view:
if($locale=='eng'){
echo $var['Model']['name'];
}else{
echo $var['Model']['name_spa'];
}
However, Id like to do something like this:
echo $var['Model']['lname'][$locale];
I tried using virtual fields but this didnt work. I didn't use i18n because it was way too complex for what I needed.
public $virtualFields = array($lname => array(
'enh' => $this->name,
'spa' => $this->name_spa,
));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的 virtualFields 数组对我来说似乎无效。正如其他人提到的,您不应该在您的视图中执行这些操作。一种选择可能是在 find() 之前构建 virtualFields 数组,如下所示:
在模型内部:
在控制器中,使用:
并在视图中使用:
这将输出所请求的 $locale 中的名称。
不过,您最好重新考虑您的本地化策略
Your virtualFields array seems invalid to me. As others mentioned, you should not be performing these things inside your view. One option might be to construct the virtualFields array just before the find(), something like:
Inside your model:
In your controller, use:
And use in view:
Which will outout the name in the requested $locale.
Still, you'd better rethink your strategy for localization