当使用Laravel的Eloquent ORM时,我似乎无法动态地在我的模型上设置$ hidden和$ visible属性.
示例1:这有效:
class User extends Eloquent {
$this->visible = array('field_name');
function read()
{
return User::all();
}
}
Run Code Online (Sandbox Code Playgroud)
示例2:动态设置Eloquent类的visible属性不起作用:
class User extends Eloquent {
function read($visible = array('field_name'))
{
$this->visible = $visible; // Also tried: $this->setVisible($visible);
return User::all();
}
}
Run Code Online (Sandbox Code Playgroud)
示例3:适用于模型本身的解决方案,但不适用于急切加载的模型:
class User extends Eloquent {
function read($visible = array('field_name'))
{
$users = User::all();
return $users->get()->each(function($row) use ($visible) {
$row->setVisible($visible);
});
}
}
Run Code Online (Sandbox Code Playgroud)
为了在Eagerly Loaded模型上动态设置$ visible属性,我没有看到另一个解决方案,而不是让示例2工作.但是怎么样?
我已经成功实现了i18next,顺便说一句,它是一个很棒的库!虽然我仍在寻找"最佳实践".这是我现在的设置,一般来说我喜欢:
var userLanguage = 'en'; // set at runtime
i18n.init({
lng : userLanguage,
shortcutFunction : 'defaultValue',
fallbackLng : false,
load : 'unspecific',
resGetPath : 'locales/__lng__/__ns__.json'
});
Run Code Online (Sandbox Code Playgroud)
在DOM中我做这样的事情:
<span data-i18n="demo.myFirstExample">My first example</span>
Run Code Online (Sandbox Code Playgroud)
在JS中我做这样的事情:
return i18n.t('demo.mySecondExample', 'My second example');
Run Code Online (Sandbox Code Playgroud)
这意味着我在代码本身内维护英文翻译.但是我translation.json
使用i18next-parser 使用单独的文件维护其他语言:
gulp.task('i18next', function()
{
gulp.src('app/**')
.pipe(i18next({
locales : ['nl','de'],
output : '../locales'
}))
.pipe(gulp.dest('locales'));
});
Run Code Online (Sandbox Code Playgroud)
这一切都很棒.唯一的问题是,当我设置'en'
为userLanguage
,i18next
坚持提取/locales/en/translation.json
文件,即使它不包含任何翻译.为了防止404,我当前{}
在该文件中提供了一个空的json对象.
有没有办法防止加载空的.json文件?