Reputation: 11
I use 2 different loader for symfony translations :
The first loader is for developer purpose (having a translation when the code is displayed in localhost without having to maintain a db). The second is for the stage / production env. Basically, POs are using this solution and developers don't have to trigger all the CI/CD process for just change one or two translations.
So, we have the same keys into the two loaders. I want the database Loader to be displayed in priority if the translation key exists in the table, else the yaml Loader take the lead.
Unfortunately, I can display database translation for some keys but, since I have multiple yaml files for the translation, some of them are not overwritten.
Is it possible in Symfony 4 to set a default Loader, or to define priorities ? If yes, how ?
If no, can we easely force the loading of all translation files in a certain order to be sure that the last file loading is the db file ?
Upvotes: 1
Views: 455
Reputation: 113
This feature is not documented, but the loaders are loaded in ascending order of the extension (or alias). So if database loader uses .xen
as extension, it will be loaded after .php
files but before .xml
or .yaml
files.
For your use case, I'd suggest to use .zzz
(or similar) as file extension (as alias of database translations loader).
We did the same a couple of years ago and it has been working since in production.
For testing, you can register two array loaders with different alias (xen
and zen
) and confirm that zen
loader overrides translations from xen
loader.
Upvotes: 1
Reputation: 656
You have multiple possibilities:
your custom loader can have a priority (higher means it called earlier). When your DB service should be last, set it to a negative number:
# config/services.yaml
services:
App\CustomTranslator:
tags:
- { name: 'translation.loader', priority: -100 }
Implement a new service that decorates the \Symfony\Contracts\Translation\TranslatorInterface
service. Within this new service, override doLoadCatalogue
. This function is responsible for loading all data from the configured loaders. In the overridden function you can customize the logic completely.
Upvotes: 0