/** * Method executed at the end of the response. Save all entity translations * previously generated and waiting for being flushed into database and * cache layer */ public function saveEntityTranslations() { if (empty($this->translationsBackup)) { return null; } foreach ($this->translationsBackup as $formHash => $entities) { foreach ($entities as $entityData) { $entity = $entityData['object']; $entityIdGetter = $entityData['idGetter']; $entityAlias = $entityData['alias']; $fields = $entityData['fields']; foreach ($fields as $fieldName => $locales) { foreach ($locales as $locale => $translation) { $this->entityTranslationProvider->setTranslation($entityAlias, $entity->{$entityIdGetter}(), $fieldName, $translation, $locale); } } } } $this->entityTranslationProvider->flushTranslations(); }
/** * Saves object translations * * $translations = array( * 'es' => array( * 'name' => 'Nombre del producto', * 'description' => 'Descripción del producto', * ), * 'fr' => array( * 'name' => 'Nom du produit', * 'description' => 'Description du produit', * ), * ); * * @param Object $object Object * @param array $translations Translations * * @return $this Self object */ public function save($object, array $translations) { $classStack = $this->getNamespacesFromClass(get_class($object)); foreach ($classStack as $classNamespace) { if (!array_key_exists($classNamespace, $this->configuration)) { continue; } $configuration = $this->configuration[$classNamespace]; $idGetter = $configuration['idGetter']; $entityId = $object->{$idGetter}(); foreach ($translations as $locale => $translation) { foreach ($configuration['fields'] as $fieldName => $fieldConfiguration) { if (isset($translation[$fieldName])) { $this->entityTranslationProvider->setTranslation($configuration['alias'], $entityId, $fieldName, $translation[$fieldName], $locale); } } } } $this->entityTranslationProvider->flushTranslations(); return $this; }
/** * Set translation * * @param string $entityType Type of entity * @param string $entityId Id of entity * @param string $entityField Field of entity * @param string|boolean $translationValue Translated value * @param string $locale Locale * * @return $this Self object */ public function setTranslation($entityType, $entityId, $entityField, $translationValue, $locale) { $cacheKey = $this->buildKey($entityType, $entityId, $entityField, $locale); $this->cache->save($cacheKey, $translationValue); return $this->entityTranslatorProvider->setTranslation($entityType, $entityId, $entityField, $translationValue, $locale); }