Skip to main content

Handling of Translations

In some cases, it is necessary to export translations and then import them into Shopware.

Pimcore

The TranslationService is available in Pimcore. It can be obtained in the adapter via the method $this->translationHelper().

Class

Twocream\ShopwareConnectorBundle\Service\TranslationService

Method mapTranslations

The method retrieves the values in all available Pimcore languages and returns an array. Field names can be renamed during export to simplify subsequent processing in Shopware.

NameValue
Functionpublic function mapTranslations(Localizedfield $field, array $fieldNames = [], array $map = [], ?string $defaultLanguage = ''): array
Argument $fieldLocalized fields of the object
(Optional) Argument $fieldNamesPimcore field identifier of the field whose values should be exported
(Optional) Argument $mapDefines the name of the target field in the export
(Optional) Argument $defaultLanguageIf a value is missing in a language, it is taken from the specified default language
Return valuesee Output

Example

protected function prepare($object): array
{
$texts = $this->translationHelper()->mapTranslations(
$object->getLocalizedfields(),
['articleName', 'description'],
['articleName' => 'name']
);

return [
// ...,
'translations' => $texts ?? [],
// ...,
];
}

Output

[
"de" => {
"name" => "Example Name",
"description" => "Example Description"
},
"en" => {
"name" => "Example Name",
"description" => "Example Description"
}
]

Shopware

In Shopware, the ModuleService is available and is called via $this->translationHelper().

Class

Twocream\JsonImporter\Core\System\Import\Module\Service\ModuleService

Method prepareTranslation

The method transforms the exported translations into the appropriate Shopware import format. Additionally, optional renaming of fields can be performed via a mapping.

NameValue
Functionpublic function prepareTranslation(array $translations, array $keyMap = []): array
Argument $translationsPassing the translation arrays from export
(Optional) Argument $keyMapMapping of fields: export field to target field.
Return valueArray

Example

protected function store(Result $result): ?EntityWrittenContainerEvent
{
// ...

foreach ($result as $entry) {
$records[] = [
'id' => $entry['uuid'],
'translations' => $this->service->prepareTranslation($entry['texts']),
// ...,
];
}

// ...
}