Fluent: Adding translations programatically overrides existing translation

<%- if @topic_view.topic.tags.present? %>
<%= t 'js.tagging.tags' %>: <%- @topic_view.topic.tags.each do |t| %> <%= t %> <%- end %>
<% end %>

Hello,

I am working on a DevTask which imports all data from a legacy project (SilverStripe 2.4) into a new SilverStripe 4.5.

Right now I am stuck with the creation of the other locales.
My import data is a huge json. For Pages (SiteTree) it looks like this:

{
"SiteTree": [
{
  "ClassName": "HomePage",
  "Created": "2013-06-24 11:44:57",
  "LastEdited": "2014-11-07 13:51:24",
  "URLSegment": "home",
  "Title": "Startseite",
  "ShowInMenus": "1",
  "ShowInSearch": "1",
  "ProvideComments": "0",
  "Sort": "1",
  "HasBrokenFile": "0",
  "HasBrokenLink": "0",
  "Status": "Published",
  "CanViewType": "Inherit",
  "CanEditType": "Inherit",
  "Version": "9",
  "Locale": "de_DE",
  "ParentID": "0",
  "ContentWidth": "0",
  "HideContent": "1",
  "ShowDropdown": "0",
  "BackgroundImageID": "8",
  "ID": 1,
  "RecordClassName": "HomePage",
  "Translation": {
    "ClassName": "HomePage",
    "Created": "2014-11-06 20:27:05",
    "LastEdited": "2014-11-08 21:32:10",
    "URLSegment": "english",
    "Title": "Home Page",
    "MetaTitle": "Home Page",
    "ShowInMenus": "1",
    "ShowInSearch": "1",
    "ProvideComments": "0",
    "Sort": "1",
    "HasBrokenFile": "0",
    "HasBrokenLink": "0",
    "Status": "Published",
    "CanViewType": "Inherit",
    "CanEditType": "Inherit",
    "Version": "2",
    "Locale": "en_GB",
    "ParentID": "0",
    "ContentWidth": "0",
    "HideContent": "1",
    "ShowDropdown": "0",
    "BackgroundImageID": "8",
    "ID": 22,
    "RecordClassName": "HomePage"
  },
  "Children": []
},
...

My import for SiteTree based Objects:

 protected function createPage($data, $parent_id = 0) {
    $className = '\\'.$this->getPageClass($data->ClassName);

    $attributes = (array)$data;
    $attributes['ClassName'] = ltrim($className, '\\');
    try {
        /** @var SiteTree $page */
        $page = $className::create($attributes);
        $page->ParentID = $parent_id;
        $page->write();

        if (isset($data->Translation)) {
            FluentState::singleton()
                ->withState(function (FluentState $state) use ($page, $data) {
                    $translatable = array_merge(
                        SiteTree::singleton()->getLocalisedFields(),
                        $page->getLocalisedFields()
                    );

                    if ($translatable) {
                        $state->setLocale('en_GB');
                        $en = (array)$data->Translation;
                        $translations = array_intersect_key($en, $translatable);
                        $page->update($translations);
                        $page->write();
                    }
                });
        }
        $page->writeToStage('Live');

    } catch (ValidationException $exception) {
        echo $exception->getMessage()."<br>";
        pd($page, $data, $parent_id);
    }

    if ($data->Children) {
        foreach ($data->Children as $child) {
            $this->createPage($child, $page->ID);
        }
    }
}

All records arrive in the database. So far so good.
BUT: I end up with the same values for the localised fields for de_DE and en_GB…

What am I missing?

Thanks in advance :slight_smile:

It turns out I misread the documentation a little bit.

This essentially requires the re-publication of content in each locale, once content is localised.
See the Docs

So my solution for SiteTree Objects:

    protected function createPage($data, $parent_id = 0) {
    $className = '\\'.$this->getPageClass($data->ClassName);

    $attributes = (array)$data;
    $attributes['ClassName'] = ltrim($className, '\\');
    try {
        /** @var SiteTree $page */
        $page = $className::create($attributes);
        $page->ParentID = $parent_id;
        $page->write();
        $page->writeToStage('Live'); //<----- ADDED this

        if (isset($data->Translation)) {
            FluentState::singleton()
                ->withState(function (FluentState $state) use ($page, $data) {
                    $translatable = array_merge(
                        SiteTree::singleton()->getLocalisedFields(),
                        $page->getLocalisedFields()
                    );

                    if ($translatable) {
                        $state->setLocale('en_GB');
                        $en = (array)$data->Translation;
                        $translations = array_intersect_key($en, $translatable);
                        $page->update($translations);
                        $page->write();
                        $page->writeToStage('Live'); // <---- AND THIS
                    }
                });
        }


    } catch (ValidationException $exception) {
        echo $exception->getMessage()."<br>";
        pd($page, $data, $parent_id);
    }

    if ($data->Children) {
        foreach ($data->Children as $child) {
            $this->createPage($child, $page->ID);
        }
    }
}

:slight_smile: