webfactory / WebfactoryPolyglotBundle

Symfony bundle simplifying translations in Doctrine.
MIT License
3 stars 3 forks source link

Allow per-entity instance primary locales #50

Open mpdude opened 7 months ago

mpdude commented 7 months ago

For now, each entity class has one fixed primary locale, expressed through the Locale attribute.

We have encountered cases in which some records were only available in a language different from the primary locale.

Therefore, it would be nice if we could remove the primary locale attribute and retrieve this information from a property, on a per-entity instance level. This would allow every entity instance to define its own primary locale.

martingold commented 1 month ago

Thank you for the wonderful library.

I am now refactoring an application where the primary language of the entity is stored in different entity (😐) and this issue is a blocker for me to use this nice library. I was thinking of two approaches to this. Just ideas and I would like to discuss them before implementing them.

Interface

public function injectNewPersistentTranslatables(object $entity, EntityManager $entityManager, DefaultLocaleProvider $defaultLocaleProvider): void
    {
        foreach ($this->translatedProperties as $fieldName => $property) {
            $persistentTranslatable = new PersistentTranslatable(
                $entityManager->getUnitOfWork(),
                $this->class,
                $entity,
                // Handle case of missing #[Locale] attribute while no being instance of DynamicLocaleEntity
                $entity instanceof DynamicLocaleEntity ? $entity->getLocale() : $this->primaryLocale,
                $defaultLocaleProvider,
                $this->translationFieldMapping[$fieldName],
                $this->translationsCollectionProperty,
                $this->translationClass,
                $this->translationLocaleProperty,
                $this->translationMappingProperty,
                $property,
                $this->logger
            );
            $persistentTranslatable->inject();
        }
    }

Questions: Is the Locale attribute needed? The getLocale() could return literal string.

Extend Locale attribute

Questions:

Which of these two approaches would have better chance of getting merged if i were to implement it? Am I missing something critical @mpdude ?

Thank you!