Search code examples
phpcronsilverstripefluent

Silverstripe fluent set locale


Good afternoon,

Does somebody know if there is a method to set locale manual? I want to update some locale based items in the database by a cronjob, but to make it work I have to set locale based on some variables instead of the locale of the server.


Solution

  • In the SilverStripe 3 version of Fluent you can use Fluent::with_locale to perform a callback under the context of a given locale, e.g.:

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

    For reference, in the SilverStripe 4 version you can do this instead:

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