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

[RTM] Translator #1072

Merged
merged 15 commits into from
Sep 18, 2017
Merged

Conversation

ausi
Copy link
Member

@ausi ausi commented Sep 12, 2017

Can be used as following:

->trans('MSC.view', [], 'contao_default');
->trans('ERR.minlength', ['My Field', 1234], 'contao_default');
->trans('tl_settings.websiteTitle.0', [], 'contao_tl_settings');
->trans('MSC.key\.mit\.punkt\.und\\\\backslash', 'contao_default');

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 }}

@leofeyer leofeyer added this to the 4.5.0 milestone Sep 12, 2017
@leofeyer
Copy link
Member

leofeyer commented Sep 12, 2017

I like a lot!

As discussed, we also want to use https://symfony.com/doc/current/service_container/service_decoration.html

return $this->translator->trans($id, $parameters, $domain, $locale);
}

if (!$this->framework->isInitialized()) {
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Done.

private function loadLanguageFile(string $name)
{
/** @var \Contao\System */
$system = $this->framework->getAdapter('System');
Copy link
Member

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.

Copy link
Member Author

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])) {
Copy link
Member

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?

Copy link
Member Author

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.

@ausi ausi changed the title [WIP] Translator [RTM] Translator Sep 14, 2017
Copy link
Contributor

@DanielSchwiperich DanielSchwiperich left a 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
Copy link
Contributor

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

/**
* {@inheritdoc}
*/
public function trans($id, array $parameters = [], $domain = null, $locale = null)
Copy link
Contributor

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for following methods

Copy link
Member Author

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

Copy link
Contributor

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)
Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Member Author

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)
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in bbf0b94.

@ausi
Copy link
Member Author

ausi commented Sep 14, 2017

@DanielSchwiperich Adding PHP 7 features is not part of this pull request and should be done in #1077 IMO

@DanielSchwiperich
Copy link
Contributor

ah ok in that case you would need to remove the string parameter type hints because they are only allowed from php 7 onwards. And then ignore most of my other comments 😄

@aschempp
Copy link
Member

Just found the LoggingTranslatorPass class. Does that affect us?

@ausi
Copy link
Member Author

ausi commented Sep 15, 2017

The logging still works for the Symfony translator, Contao translations don’t get logged.

@leofeyer leofeyer merged commit e275d66 into contao:develop Sep 18, 2017
@leofeyer
Copy link
Member

Thank you @ausi.

@DanielSchwiperich
Copy link
Contributor

@leofeyer you have now merged code which requires php7, you know that?

@Toflar
Copy link
Member

Toflar commented Sep 18, 2017

The develop branch is php 7.1 only.

@DanielSchwiperich
Copy link
Contributor

then I don't understand why this PR was not made using all the php 7.1 features (see my comments).
Either do it php 7(.1) way or not at all. imho

@ausi
Copy link
Member Author

ausi commented Sep 20, 2017

@DanielSchwiperich the PHP 7.1 features for this PR got added in https://github.com/contao/core-bundle/pull/1077/files#diff-144b2e2beae5529dc5a75f36c717cbb7

leofeyer added a commit that referenced this pull request Dec 9, 2019
Description
-----------

Backports the changes from #1067 and #1071.

Commits
-------

8da1a5a2 Backport the CS fixer changes (see #1071)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants