Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upTranslatorInterface::getLocale() and setLocale() #29530
Comments
This comment has been minimized.
This comment has been minimized.
To get the locale, you should use the request instead. |
This comment has been minimized.
This comment has been minimized.
Well yes, to get the requested locale. That is different (from what I understand) from what he's asking. The translation component works independent from the request and should have a getter/setter for the locale, like is implemented but missing from the interface. |
This comment has been minimized.
This comment has been minimized.
IMHO that's an implementation detail of Framework bundle exposes it's default locale, perhaps you can rely on that? |
This comment has been minimized.
This comment has been minimized.
The main issue was on the setter, rather than the getter. But since trans() has a locale parameter, we can replace
with:
|
This comment has been minimized.
This comment has been minimized.
in that case, i definitively suggest the alternative solution yes. That avoids messing with the translator state in a global sense. |
This comment has been minimized.
This comment has been minimized.
For me it's rather the As @fmonts stated above, this still works, just the IDE shows an error. You can get rid of this by adding a phpdoc comment something like
Looks strange, but actually the trait is what you get, right? |
This comment has been minimized.
This comment has been minimized.
@Shoplifter this would be a dirty hack. Relying on Use Request::getLocale() instead. |
This comment has been minimized.
This comment has been minimized.
@nicolas-grekas Thanks for the clarification. Now I know that it had a smell for a reason. |
The interface
Symfony\Contracts\Translation
, which replacesSymfony\Component\Translation
from Symfony 4.2, doesn't have thegetLocale
andsetLocale
methods.So
$translator->getLocale()
in a controller still works, but throws a warning in the IDE about method not found.Since I don't see any reference to this in the UPGRADE file, what should we do?
These methods are useful for example when you are logged in as admin and you're sending an email to an user who has a different language, so the email template needs to be translated into his language, which is different from the current language.