Fluent::with_locale PHP Method

with_locale() public static method

Warning: Existing DataObjects will contain fields in the actual locale if already lazily loaded, or if used within the callback, will populate itself with the overriding locale. The inverse will occur once the callback is complete. The best practice is to consider this a sandbox, and re-requery all objects required, discarding these afterwards.
public static with_locale ( string $locale, callable $callback ) : mixed
$locale string The locale to set
$callback callable The callback
return mixed The returned value from the $callback
    public static function with_locale($locale, $callback)
    {
        // Check and set locale
        if (self::$_override_locale) {
            throw new BadMethodCallException("Fluent::with_locale cannot be nested");
        }
        if (!in_array($locale, self::locales())) {
            throw new BadMethodCallException("Invalid locale {$locale}");
        }
        self::$_override_locale = $locale;
        DataObject::flush_and_destroy_cache();
        // Callback
        $result = call_user_func($callback);
        // Reset
        self::$_override_locale = null;
        DataObject::flush_and_destroy_cache();
        return $result;
    }

Usage Example

 /**
  * Test versioning of localised objects
  */
 public function testPublish()
 {
     // == Setup ==
     Fluent::set_persist_locale('fr_CA');
     Versioned::reading_stage('Stage');
     // Create new record in non-default locale
     $id = Fluent::with_locale('es_ES', function () {
         $page = new Page();
         $page->Title = 'ES Title';
         $page->MenuTitle = 'ES Title';
         $page->write();
         return $page->ID;
     });
     // == Check stage ==
     // Check that the record has a title in the default locale
     $page = Versioned::get_one_by_stage("SiteTree", "Stage", "\"SiteTree\".\"ID\" = {$id}");
     $this->assertEquals('ES Title', $page->Title);
     $this->assertEquals('ES Title', $page->MenuTitle);
     // Check that the record has a title in the foreign locale
     $record = Fluent::with_locale('es_ES', function () use($id) {
         $page = Versioned::get_one_by_stage("SiteTree", "Stage", "\"SiteTree\".\"ID\" = {$id}");
         return $page->toMap();
     });
     $this->assertEquals('ES Title', $record['Title']);
     $this->assertEquals('ES Title', $record['MenuTitle']);
     // == Publish ==
     // Save title in default locale
     $page = Versioned::get_one_by_stage("SiteTree", "Stage", "\"SiteTree\".\"ID\" = {$id}");
     $page->Title = 'Default Title';
     $page->MenuTitle = 'Custom Title';
     $page->write();
     // Publish this record in the custom locale
     Fluent::with_locale('es_ES', function () use($id) {
         $page = Versioned::get_one_by_stage("SiteTree", "Stage", "\"SiteTree\".\"ID\" = {$id}");
         $page->doPublish();
     });
     // == Check live ==
     // Check the live record has the correct title in the default locale
     $page = Versioned::get_one_by_stage("SiteTree", "Live", "\"SiteTree\".\"ID\" = {$id}");
     $this->assertEquals('Default Title', $page->Title);
     $this->assertEquals('Custom Title', $page->MenuTitle);
     // Check the live record has the correct title in the custom locale
     $record = Fluent::with_locale('es_ES', function () use($id) {
         $page = Versioned::get_one_by_stage("SiteTree", "Live", "\"SiteTree\".\"ID\" = {$id}");
         return $page->toMap();
     });
     $this->assertEquals('ES Title', $record['Title']);
     $this->assertEquals('ES Title', $record['MenuTitle']);
 }
All Usage Examples Of Fluent::with_locale