AMA*_*oft 2 multiple-languages laravel eloquent laravel-4
在数据库中我有这个:
name_en | name_fr
Run Code Online (Sandbox Code Playgroud)
当用户选择法语时 - 例如 - 我想获得name_fr字段,如果他选择另一种语言则相同
假设您使用以下方法在应用程序中设置区域设置:
App::setLocale($lang);
Run Code Online (Sandbox Code Playgroud)
如果你使用Eloquent,你可以添加到你的模型类accesssor:
public function getNameAttribute($value) {
return $this->{'name_'.App::getLocale()};
}
Run Code Online (Sandbox Code Playgroud)
还有变异器:
public function setNameAttribute($value) {
$this->{'name_'.App::getLocale()} = $value;
}
Run Code Online (Sandbox Code Playgroud)
假设您已将这些功能添加到Content模型中,您现在可以使用:
$content = Content::first(); // find first article
echo $content->name; // displaying its name
$content->name = 'updated content'; // changing its name
$content->save(); // saving
Run Code Online (Sandbox Code Playgroud)
name_{$lang}如果您使用lang设置,这将导致显示和更改setLocale
| 归档时间: |
|
| 查看次数: |
499 次 |
| 最近记录: |