0

I'm looking for an elegant way to have the following behavior in Symfony 6.

I use DoctrineBehaviors/translatable to store some fields in several languages, let's say 'en', 'fr' and 'de'.

The users have a small dropdown on my navbar, with which they can choose the locale in which they read the site. Changing the locale in this dropdown, triggers a redirection to the routes in the format given below, to display the entities in the chosen locale:

/{_locale}/entity/{id}

Now the tricky part: I would like to have two default locales, 'en' and 'fr', as fallbacks for a reader whose 'de' translation is missing.

Let's say my entities have the following translations (-- means translation is ABSENT from database, not empty string):

A : [ en fr de ]
B : [ en -- -- ]
C : [ -- fr -- ]
D : [ en fr -- ]

I would like the following routes to display text like this:

/en/entity/A --> en
/fr/entity/A --> fr
/de/entity/A --> de

/en/entity/B --> en
/fr/entity/B --> en
/de/entity/B --> en

/en/entity/C --> fr
/fr/entity/C --> fr
/de/entity/C --> fr

/en/entity/D --> en
/fr/entity/D --> fr
/de/entity/D --> (not important as long as either en or fr is displayed)

Today, as I can have only one default_locale, I must choose either 'fr' or 'en', but if I choose 'en', then /de/entity/C will display an empty 'en' translation, and if I choose 'fr', then /de/entity/B will display an empty 'fr' translation.

I've tried to setup two fallbacks in translations.yaml but it doesn't work, I fear it's more used by translations in translation files, and not in database.

framework:
    default_locale: en
    translator:
        fallbacks: ['fr', 'en']

Thanks in advance if you can help me

Ryierth
  • 38
  • 1
  • 7

1 Answers1

0

Well I answer my own question, to give the solution I found, in case it interrests anyone future.

In order to achieve what was wanted, I abandonned DoctrineBehaviors/translatable and replaced it with DoctrineExtensions/translatable. What I found convenient with it is the database storage:

  • The translated labels go to a table distinct from the table representing your entity
  • The label column in the entity table is set in the first place with whichever translation provided when entity is created. After that, it remains until anyone provides the translation in the default locale: at that moment, the label in the entity table is replaced by it.

Let's see some examples, based on my question above:

  • Default locale is en
  • Other locales are fr and de

Example 1: creation in the default locale

Sequence of events:

  1. User creates an entity with locale en
Database storage:
   Entity table: en label
   Translations table: empty

So far, everyone sees the entity with en label.

  1. User translates the entity with locale fr
Database storage:
   Entity table: en label
   Translations table: fr label

After that, people set to en see en version, people set to fr see fr version.

Example 2: creation NOT in the default locale

Sequence of events:

  1. User creates an entity with locale fr
Database storage:
   Entity table: fr label
   Translations table: fr label

So far, everyone sees the entity with fr label.

  1. User translates the entity with locale de
Database storage:
   Entity table: fr label
   Translations table: fr and de labels

So far, people set to en see fr version, people set to fr see fr version, people set to de see de version.

  1. User translates the entity with locale en
Database storage:
   Entity table: en label
   Translations table: fr and de labels

After that, people set to en see en version, people set to fr see fr version, people set to de see de version.

That's exactly what I wanted to have. Enjoy, folks!

Ryierth
  • 38
  • 1
  • 7