Fluent - Set Localised Fields


#1

**Silverstripe Version: 4.3.3 **

Question: How do I translate an existing Page in PHP

After an CMS upgrade from SS3. I’am importing my old News from a Rest-API. But how do I translate them?

$page = SiteTree::get()->first();
$page->Title = [LOCALISED TITLE HERE];
$page->doPublish() //Publish Localised Page.

#2

I believe that when an object has been localized like say SiteTree etc, it checks the current ‘locale’ from the FluentState Class - silverstripe-fluent/FluentState.php at master · tractorcow-farm/silverstripe-fluent · GitHub

Based off of that class when you call write, publish, etc, whatever the current locale is set to is what will get modified. So If you need to update just some other locale than the default, you would probably need to call something like -

FluentState::singleton()->setLocale($thelocale);

Then do the publish/write. I haven’t tested this but that is where I would start. Now if your site is configured with proper fallbacks, then you should just need to do the normal write/publish and other locales will then fall back to that.


#3

Thanks,
you’re right. I did it with FluentState::singleton().

FluentState::singleton() ->withState(function (FluentState $state) use ($deNews, $instanceLocale, $jn) {...