Skip to content

[2.3] Issue when importing translated data when having more than 2 languages #745

@mariavilaro

Description

@mariavilaro

If I have the following languages configured in October 3: en (default), es, fr

And I try to import the following data (in a .yaml file)

- title: 'Project 1'
  translations:
    es:
      title: 'Proyecto 1'
    fr:
      title: 'Projet 1'

Using this code:

public function importProjects($path)
{
    $result = (array)Yaml::parseFile($path);

    foreach ($result as $projectdata) {
        $translations = isset($projectdata['translations']) ? $projectdata['translations'] : [];
        unset($projectdata['translations']);
        $record = Project::create($projectdata);
        foreach ($translations as $locale => $translation) {
            $record->translateContext($locale);
            $record->fill($translation);
        }
        $record->save();
    }
}

The french translation is created with the spanish text:
image

There is no problem with only 2 languages, this only happens with more than 2 languages.

If I add more languages, the problem "moves" to the last language:

image

I found this problem while importing new data to an existing table, and there it's even worse, because the last language translations for the model are completely overriden. This is a simplified example where I add a new "description" field to the Project model and then import the texts from a yaml file:

- id: 1
  description: 'Project 1 description'
  translations:
    es:
      description: 'Descripción del proyecto 1'
    fr:
      description: 'Description du projet 1'
public function updateProjects($path)
{
    $result = (array)Yaml::parseFile($path);

    foreach ($result as $projectdata) {
        $translations = isset($projectdata['translations']) ? $projectdata['translations'] : [];
        unset($projectdata['translations']);
        $record = Project::find($projectdata['id']);
        $record->description = $projectdata['description'];
        $record->save();
        foreach ($translations as $locale => $translation) {
            $record->translateContext($locale);
            $record->fill($translation);
        }
        $record->save();
    }
}

image


I found a workaround, if I modify the code to this, to instanciate a new model and save just a translation each time:

foreach ($translations as $locale => $translation) {
    $recordCopy = Project::find($record->id);
    $recordCopy->translateContext($locale);
    $recordCopy->fill($translation);
    $recordCopy->save();
}

Then the records are created ok
image

But I think that this shouldn't be the behaviour when using translateContext() and then adding new translations to a nodel.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions