/** * Get translation * * @param string $entityType Type of entity * @param string $entityId Id of entity * @param string $entityField Field of entity * @param string $locale Locale * * @return string|boolean Value fetched */ public function getTranslation($entityType, $entityId, $entityField, $locale) { $cacheKey = $this->buildKey($entityType, $entityId, $entityField, $locale); $translation = $this->cache->fetch($cacheKey); if ($translation === false) { $translation = $this->entityTranslatorProvider->getTranslation($entityType, $entityId, $entityField, $locale); $this->cache->save($cacheKey, $translation); } return $translation; }
/** * Translate object * * @param Object $object Object * @param string $locale Locale to be translated * * @return Object Translated Object */ public function translate($object, $locale) { $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 ($configuration['fields'] as $fieldName => $fieldConfiguration) { $setter = $fieldConfiguration['setter']; $translation = $this->entityTranslationProvider->getTranslation($configuration['alias'], $entityId, $fieldName, $locale); if ($translation || !$this->fallback) { $object->{$setter}($translation); } } } return $object; }
/** * Buildform function. * * @param FormBuilderInterface $builder the formBuilder * @param array $options the options for this form */ public function buildForm(FormBuilderInterface $builder, array $options) { $entityAlias = $this->entityConfiguration['alias']; $entityIdGetter = $this->entityConfiguration['idGetter']; $fieldOptions = $this->formConfig->getOptions(); $fieldType = $this->formConfig->getType()->getName(); foreach ($this->locales as $locale) { $translatedFieldName = $locale . '_' . $this->fieldName; $entityId = $this->entity->{$entityIdGetter}(); $translationData = $entityId ? $this->entityTranslationProvider->getTranslation($entityAlias, $entityId, $this->fieldName, $locale) : ''; $builder->add($translatedFieldName, $fieldType, ['required' => isset($fieldOptions['required']) ? $this->evaluateRequired($fieldOptions['required'], $locale) : false, 'mapped' => false, 'label' => $fieldOptions['label'], 'data' => $translationData, 'constraints' => $fieldOptions['constraints']]); } }