Skip to content

LocaleSwitcher doesn't set the correct Locale for form labels #53904

Closed
@trsteel88

Description

@trsteel88

Symfony version(s) affected

6.4

Description

I am using the LocaleSwitcher to modify the Locale:

public function indexAction(
        Request $request,
        ProgramRepository $programRepository,
        LocaleSwitcher $localeSwitcher,
        int $id,
    ): Response {
        if (null === $program = $programRepository->find($id)) {
            throw $this->createNotFoundException();
        }

        $localeSwitcher->setLocale($program->getLocale());

I have a form that is created after setting the Locale. However, the labels do not come through in the correct language.

image

You will notice above that the Validation errors translate fine which is correct.

The correct language for the label only comes through if I specifically setLocale on the Request.

public function indexAction(
        Request $request,
        ProgramRepository $programRepository,
        LocaleSwitcher $localeSwitcher,
        int $id,
    ): Response {
        if (null === $program = $programRepository->find($id)) {
            throw $this->createNotFoundException();
        }

        $request->setLocale($program->getLocale());
        $localeSwitcher->setLocale($program->getLocale());
image

Is this the intended logic? Seems strange that I would need to set the locale on the switcher as well as the Request

How to reproduce

  • Create a form with translated labels
  • Use the LocaleSwitcher to change the language

Possible Solution

No response

Additional Context

No response

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