Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mariavilaro opened this issue Jul 28, 2024 · 1 comment

Comments

@mariavilaro
Copy link
Contributor

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.

@daftspunk
Copy link
Member

daftspunk commented Aug 1, 2024

A fix for this has been added in 876f6b1

It looks like we may need to improve the internals to make this more robust, at the moment, the default locale doesn't seem to save with this test code:

$s = rand();

$c = October\Test\Models\Country::find(1);
$c->name = 'english'.$s;

$c->translateContext('fr');
$c->name = 'french'.$s;

$c->translateContext('de');
$c->name = 'german'.$s;

$c->save();

A new release (2.3) is needed.

@daftspunk daftspunk reopened this Aug 1, 2024
@daftspunk daftspunk changed the title Issue when importing translated data when having more than 2 languages [2.3] Issue when importing translated data when having more than 2 languages Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants