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

Translation reason #202

Closed
gunter423 opened this issue Dec 13, 2021 · 5 comments · Fixed by #206
Closed

Translation reason #202

gunter423 opened this issue Dec 13, 2021 · 5 comments · Fixed by #206

Comments

@gunter423
Copy link

gunter423 commented Dec 13, 2021

Hi,

I have a question, there are possibility to translate the reason that return for exceptions :

ResetPasswordExceptionInterface
ExpiredResetPasswordTokenException
FakeRepositoryException
InvalidResetPasswordTokenException
TooManyPasswordRequestsException

? :)

@Snowbaha
Copy link

Hello, I Was asking myself the same thing. Is it not possible or just no time to do it?

@gunter423
Copy link
Author

Hello, I Was asking myself the same thing. Is it not possible or just no time to do it?

It's not possible :'(

@AxelBriche
Copy link

AxelBriche commented Jan 27, 2022

Up, class has "final" prefix and cannot be overwrited.
Can you please add some translations for that messages?

I cannot translate the message in red here:

CleanShot 2022-01-27 at 23 47 28@2x

@bocharsky-bw
Copy link
Member

Hey guys! You're correct, it's not translated out of the box (yet) unfortunately - I'll try to think about it. But, you don't need to be able to override that exception class to translate the message - just leverage the Symfony Translator component. Let me give you a workaround.

First of all, thanks to Maker bundle that generated the actual code in your code base - you can easily tweak it if you need. Open your ResetPasswordController::reset(), then inject there Symfony Translator object and wrap the actual message with trans():

public function reset(TranslatorInterface $translator, Request $request, UserPasswordHasherInterface $passwordHasher, string $token = null): Response
{
    // ...
    try {
        // ...
    } catch (ResetPasswordExceptionInterface $e) {
        $this->addFlash('reset_password_error', $translator->trans(sprintf(
            'There was a problem validating your reset request - %s',
            $e->getReason()
        )));

        // ...
    }
}

That's it! That's literally the only workaround you need. Finally, all you need to do is actually translate those complete messages in your translation files, e.g. in my messages.uk.yml I wrote:

# ...
"There was a problem validating your reset request - The reset password link is invalid. Please try to reset your password again.": Виникла проблема під час перевірки вашого запиту на скидання – посилання для скидання пароля недійсне. Спробуйте скинути пароль ще раз.

And so on for other ResetPasswordExceptionInterface possible exception messages, don't forget to prefix all of them with There was a problem validating your reset request - as you see from the controller :)

I just double-checked it in my project - it works for me:
Screenshot 2022-01-28 at 10 57 23

@gunter423
Copy link
Author

Thanks @bocharsky-bw

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

Successfully merging a pull request may close this issue.

4 participants