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
 /**
  * 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']);
 }
 /**
  * Determine the link to this object given the specified $locale.
  * Returns null for DataObjects that do not have a 'Link' function.
  *
  * @param string $locale
  * @return string
  */
 public function LocaleLink($locale)
 {
     // Skip dataobjects that do not have the Link method
     if (!$this->owner->hasMethod('Link')) {
         return null;
     }
     // Return locale root url if unable to view this item in this locale
     if ($this->owner->hasMethod('canViewInLocale') && !$this->owner->canViewInLocale($locale)) {
         return $this->owner->BaseURLForLocale($locale);
     }
     // Mock locale and recalculate link
     $id = $this->owner->ID;
     $class = $this->owner->ClassName;
     return Fluent::with_locale($locale, function () use($id, $class, $locale) {
         $link = DataObject::get($class)->byID($id)->Link();
         // Prefix with domain if in cross-domain mode
         if ($domain = Fluent::domain_for_locale($locale)) {
             $link = Controller::join_links(Director::protocol() . $domain, $link);
         }
         return $link;
     });
 }
 /**
  * Test that records created in non-default locale don't have missing values for default fields
  */
 public function testCreateInNonDefaultLocale()
 {
     Fluent::set_persist_locale('es_ES');
     // Create a record in this locale
     $record = new FluentTest_TranslatedObject();
     $record->Title = 'es title';
     $record->Description = 'es description';
     $record->write();
     $recordID = $record->ID;
     $row = DB::query(sprintf("SELECT * FROM \"FluentTest_TranslatedObject\" WHERE ID = %d", $recordID))->first();
     // Check that the necessary fields are assigned
     $this->assertEquals('es title', $row['Title']);
     $this->assertEquals('es title', $row['Title_es_ES']);
     $this->assertEquals('es title', $row['Title_fr_CA']);
     $this->assertEmpty($row['Title_en_NZ']);
     $this->assertEquals('es description', $row['Description']);
     $this->assertEquals('es description', $row['Description_es_ES']);
     $this->assertEquals('es description', $row['Description_fr_CA']);
     $this->assertEmpty($row['Description_en_NZ']);
     // modify locale in default locale
     Fluent::with_locale('fr_CA', function () use($recordID) {
         $record = FluentTest_TranslatedObject::get()->byID($recordID);
         $record->Title = 'new ca title';
         $record->write();
     });
     // Check that the necessary fields are assigned
     $row = DB::query(sprintf("SELECT * FROM \"FluentTest_TranslatedObject\" WHERE ID = %d", $recordID))->first();
     $this->assertEquals('new ca title', $row['Title']);
     $this->assertEquals('es title', $row['Title_es_ES']);
     $this->assertEquals('new ca title', $row['Title_fr_CA']);
     $this->assertEmpty($row['Title_en_NZ']);
     $this->assertEquals('es description', $row['Description']);
     $this->assertEquals('es description', $row['Description_es_ES']);
     $this->assertEquals('es description', $row['Description_fr_CA']);
     $this->assertEmpty($row['Description_en_NZ']);
     // modify in another locale
     Fluent::with_locale('en_NZ', function () use($recordID) {
         $record = FluentTest_TranslatedObject::get()->byID($recordID);
         $record->Title = 'nz title';
         $record->Description = 'nz description';
         $record->write();
     });
     // Check that the necessary fields are assigned
     $row = DB::query(sprintf("SELECT * FROM \"FluentTest_TranslatedObject\" WHERE ID = %d", $recordID))->first();
     $this->assertEquals('new ca title', $row['Title']);
     $this->assertEquals('es title', $row['Title_es_ES']);
     $this->assertEquals('new ca title', $row['Title_fr_CA']);
     $this->assertEquals('nz title', $row['Title_en_NZ']);
     $this->assertEquals('es description', $row['Description']);
     $this->assertEquals('es description', $row['Description_es_ES']);
     $this->assertEquals('es description', $row['Description_fr_CA']);
     $this->assertEquals('nz description', $row['Description_en_NZ']);
 }