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
With the language mapping de: de_DE and en: en_GB:
[
'de_DE' => [
'name' => 'Example Name',
'description' => 'Example Description'
],
'en_GB' => [
'name' => 'Example Name',
'description' => 'Example Description'
]
]
Method getSupportedLanguageMapping
The method returns getLanguageMapping() restricted to the passed Pimcore languages. With an empty array, it returns the complete getLanguageMapping(). The product export passes the option supported_languages of the export target (see "Configure Language Mapping").
| Name | Value |
|---|---|
| Function | public function getSupportedLanguageMapping(array $supportedLanguages): array |
| Parameter | $supportedLanguages: Pimcore languages, e.g., ['de', 'en'] |
| Return value | Pimcore language => Shopware locale, e.g., ['de' => 'de_DE', 'en' => 'en_GB'] |
In contrast, getLanguageMapping() – and therefore mapTranslations() – contains all valid and not disabled Pimcore languages, languages without mapping under their Pimcore key. The product export (ProductAdapterAbstract) exports the languages of getSupportedLanguageMapping() (see "Pimcore language and Shopware locale").
Shopware
In Shopware, the ModuleService is available in every import module via $this->service, e.g., $this->service->prepareTranslation().
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. The keys of the translations are mapped to the languages in Shopware via JsonImportConfiguration::getLocaleMapping(), e.g. de => de_DE and en => en_GB (see "Which locales are accepted?").
| 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. With a mapping only the mapped fields are taken over |
| Return value | Array |
Example
protected function store(Result $result): ?EntityWrittenContainerEvent
{
// ...
foreach ($result as $entry) {
$records[] = [
'id' => $entry['uuid'],
'translations' => $this->service->prepareTranslation($entry['texts']),
// ...,
];
}
// ...
}