You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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:
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();
}
}
I found a workaround, if I modify the code to this, to instanciate a new model and save just a translation each time:
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:
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
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)
Using this code:
The french translation is created with the spanish text:
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:
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:
I found a workaround, if I modify the code to this, to instanciate a new model and save just a translation each time:
Then the records are created ok
But I think that this shouldn't be the behaviour when using translateContext() and then adding new translations to a nodel.
The text was updated successfully, but these errors were encountered: