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.
Name | Value |
---|---|
Function | public function mapTranslations(Localizedfield $field, array $fieldNames = [], array $map = [], ?string $defaultLanguage = ''): array |
Argument $field | Localized fields of the object |
(Optional) Argument $fieldNames | Pimcore field identifier of the field whose values should be exported |
(Optional) Argument $map | Defines the name of the target field in the export |
(Optional) Argument $defaultLanguage | If a value is missing in a language, it is taken from the specified default language |
Return value | see 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.
Name | Value |
---|---|
Function | public function prepareTranslation(array $translations, array $keyMap = []): array |
Argument $translations | Passing the translation arrays from export |
(Optional) Argument $keyMap | Mapping of fields: export field to target field. |
Return value | Array |
Example
protected function store(Result $result): ?EntityWrittenContainerEvent
{
// ...
foreach ($result as $entry) {
$records[] = [
'id' => $entry['uuid'],
'translations' => $this->service->prepareTranslation($entry['texts']),
// ...,
];
}
// ...
}