Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force specific locales for all translations #6455

Open
zoffin opened this issue Sep 23, 2024 · 0 comments
Open

Force specific locales for all translations #6455

zoffin opened this issue Sep 23, 2024 · 0 comments

Comments

@zoffin
Copy link

zoffin commented Sep 23, 2024

Short description

By default EasyAdmin uses the EasyAdminBundle translation domain, which provides the translations for all basic UI elements for multiple locales. While this is great, I can not find a way to use another translation than the current defined locale for those "native" UI elements.

What I want to do

My website currently supports multiple locales, but I only want to provide the EasyAdmin bundle in english. I have the following Symfony configuration set up, such that I have English as a fallback for all translations.

framework:
    default_locale: 'de_DE'
    enabled_locales: ['de', 'fr', 'en']
    translator:
        fallbacks: [ 'en' ]

I then have a translation file for english (EasyAdminBundle.en.yaml) but for french there is no file, so Symfony falls back to the default - which is en. However the "native" elements (like the edit or create button) keep using the french translation, because this bundle provides some translations for this language.

So is there currently a way to exclude these translations or force EasyAdmin to use a specific language for all translations? Something like the following would be nice.

public function configureDashboard(): Dashboard
{
  return Dashboard::new()
      ->setTranslationDomain('someCustomTranslationDomain')
      ->setTranslationLanguage('en');
}

What does not work/what I tried

Use a different translation domain

Using a different translation domain only works for the elements I added manually. EasyAdmin always uses the EasyAdminBundle domain for its own UI elements. For example the following code will not be sufficient:

public function configureDashboard(): Dashboard
{
  return Dashboard::new()
      ->setTranslationDomain('someCustomTranslationDomain');
}
Providing a locale when calling the translation method

Symfonys translation method provides a parameter for the locale. The problem here is the same as above: while this works well for all my custom translations, EasyAdmin will obviously not use this locale.

$translator->trans('some.translation.identifier', locale: 'en');
Overriding the TranslatableMessage

I tried to override the TranslatableMessage object, in the hope that the bundle will use my override instead of the original class but this also does not seem to work.

class EasyAdminTranslatableMessage extends TranslatableMessage
{
  public function trans(TranslatorInterface $translator, ?string $locale = null): string
  {
      return parent::trans($translator, 'en');
  }
}
services
    Symfony\Component\Translation\TranslatableMessage:
        class: App\Translation\Resources\EasyAdminTranslatableMessage
Changing the locale in the request

I tried to set up a service, that checks the current locale and changes it for the current request. However this is no suitable, because my other service rely on the locale to be "correctly" set, which would not be the case anymore if I change the locale. I know that in a perfect project I could simply change the locale from lets say fr_FR to en_FR should to the trick, but in my case the locale for the request must be kept as is.

class LocaleEventListener
{
  public function onKernelRequest(RequestEvent $event): void
  {
      $event->getRequest()->setLocale('en');
  }
}

What would work theoretically

One really ugly option would be to to simply delete the french translation file from my project from vendor/easycorp/easyadmin-bundle/src/Resources/translations/EasyAdminBundle.fr.php. Another option would be to simply copy the content of the english translation file and paste it in a file for fr but this also would be quite ugly and I would really prefer to not do it that way...

Any help or ideas would be really appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant