Silverstripe Fluent 设置语言环境

Silverstripe fluent set locale

下午好,

有人知道有没有手动设置locale的方法?我想通过 cronjob 更新数据库中一些基于语言环境的项目,但为了让它工作,我必须根据一些变量而不是服务器的语言环境来设置语言环境。

在 Fluent 的 SilverStripe 3 版本中,您可以使用 Fluent::with_locale 在给定语言环境的上下文中执行回调,例如:

Fluent::with_locale('de_DE', function () {
    $myObject = MyObject::create();
    $myObject->Title = 'German title';
    $myObject->write();
});

供参考,在 SilverStripe 4 版本中,您可以改为这样做:

FluentState::singleton()->withState(function (FluentState $newState) {
    $newState->setLocale('de_DE');
    // ...
});