Skip to content

[Translator] Add LocaleSwitcher Service #35925

Closed
@alexander-schranz

Description

@alexander-schranz

See comments beneath.

Description

Similar to trans_default_domain it would be nice to also have a trans_default_locale. Currently in Symfony 4 I did mostly do something like:

$currentLocale = $this->translator->getLocale();
$this->translator->setLocale($adminUser->getLocale());

try {
    $content = $this->twig->render('my.html.twig');
} finally {
    $this->translator->setLocale($currentLocale);
}

But with the introducing of the new contracts TranslatorInterface this is not longer part of that interface. And currently it seems no official way how to render a specific twig template or a part of it in a specific language.

Example

{% trans_default_locale adminUser.locale %}

{{ 'Hello %name%'|trans }}

Edit

As mention by @stof we should not focus here on the translator instead of something like a LocaleSwitcher service which is used to update all LocaleAwareInterface services. I think the LocalSwitcher could live Translator component and be registered in the FrameworkBundle.

Example

$this->localSwitcher->setLocale($message->getLocale());

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions