-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
[RTM] Translator #1072
[RTM] Translator #1072
Conversation
|
I like a lot! As discussed, we also want to use https://symfony.com/doc/current/service_container/service_decoration.html |
src/Translation/Translator.php
Outdated
| return $this->translator->trans($id, $parameters, $domain, $locale); | ||
| } | ||
|
|
||
| if (!$this->framework->isInitialized()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we initialize the framework here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Done.
src/Translation/Translator.php
Outdated
| private function loadLanguageFile(string $name) | ||
| { | ||
| /** @var \Contao\System */ | ||
| $system = $this->framework->getAdapter('System'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You must always use the FQCN, or just use System::class and import the class from Contao\System.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| $item = &$GLOBALS['TL_LANG']; | ||
|
|
||
| foreach ($parts as $part) { | ||
| if (!isset($item[$part])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if $item is not an array here? Will we get an error message trying to access a key of a scalar variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, isset() doesn’t generate a warning for such cases. Tested in aea72e3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
main request changes regarding use of php 7 features
| @@ -0,0 +1,138 @@ | |||
| <?php | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since this PR is targeted for 4.5.0 (same as #1077) and is already using php 7 features like the string parameter type hints it should fully make use of it.
right here declare strict declaration is missing
src/Translation/Translator.php
Outdated
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function trans($id, array $parameters = [], $domain = null, $locale = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the symfony interface is php5, it is ok to use parameter hints in the implementing method, also return type declaration is missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for following methods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, parameter hints are not allowed for implemented methods in PHP 7.1, only in 7.2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are absolutely right on this. Thought I had done this successfully in a project but was wrong
| * | ||
| * @return string|null | ||
| */ | ||
| private function getFromGlobals(string $id, string $domain) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw leo using nullable return type, so I assume we have php 7.1 features available and we can set a return type : ?string
| * | ||
| * @author Martin Auswöger <martin@auswoeger.com> | ||
| */ | ||
| class Translator implements TranslatorInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this Translator is only for usage with $Globals['TL_LANG'] and hopefully replaced someday by a solution which doesn't need to use a superglobal I would recommend a name like GlobalsTranslator or so
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where this translator gets the messages from may change in the future. In the optimal case this class gets removed in the future and the default translater service can be used.
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function setLocale($locale) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend to give info in the doc block that this method and the following do nothing in regard of contao.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in bbf0b94.
|
@DanielSchwiperich Adding PHP 7 features is not part of this pull request and should be done in #1077 IMO |
|
ah ok in that case you would need to remove the |
|
Just found the |
|
The logging still works for the Symfony translator, Contao translations don’t get logged. |
|
Thank you @ausi. |
|
@leofeyer you have now merged code which requires php7, you know that? |
|
The develop branch is php 7.1 only. |
|
then I don't understand why this PR was not made using all the php 7.1 features (see my comments). |
|
@DanielSchwiperich the PHP 7.1 features for this PR got added in https://github.com/contao/core-bundle/pull/1077/files#diff-144b2e2beae5529dc5a75f36c717cbb7 |
Can be used as following:
Twig:
{% trans_default_domain 'contao_default' %} {{ 'MSC.view'|trans }} {{ 'ERR.minlength'|trans(['My Field', 1234]) }} {{ 'tl_settings.websiteTitle.0'|trans({}, 'contao_tl_settings') }} {{ 'MSC.key\\.mit\\.punkt\\.und\\\\backslash'|trans }}