Search code examples
symfonylocalizationinternationalizationtranslationmultilingual

Symfony load translations dynamically


I need to load and override translations dynamically based on some user settings, the system I need is actually a combination of translations and string overrides, I tried having overrides xliff files for different user groups and loading them with

$this->translator->addResource('xlf', $path, $lang);

but with that I have caching issues, it doesn't seem appropriate to delete translation caches all the time, is there any better way to achieve this?

The big picture is that I need to support multiple languages, but there is a separate translations team (unaware of the technical implementation), and they need to be able to provide slightly different translations/texts to different user groups.


Solution

  • I ended up deleting translations caches after all, this is the simplest way to do it, I might explore how to delete it less aggressively etc. in the future

    $fileSystem = new Filesystem();
    $fileSystem->remove("$this->cacheDir/translations");
    

    ($this->cacheDir is "%kernel.cache_dir%" injected by DI into the service)