php-translation / extractor

Extracts translation strings from source code
MIT License
126 stars 33 forks source link

Issue with 'label' key in an array (outside of the Symfony Form context) [SF4] #71

Closed noofaq closed 6 years ago

noofaq commented 6 years ago

I have just started to review of your bundle to replace nearly dead JMSTranslationBundle (even if I have succeded to make it work in SF4 it does not give any hope for long-term usage).

I have some lines in Symfony4 controller - used to build some breadcrumbs. I have to translate them in controller as they are built with some translateable strings (like shown below "Users" string) and also non-translateable. I am (it seems unfortunately) using 'label' key in breadcrumb array which causes errors in translation:extract Symfony Command.

Code from controller (both lines fail, however 'breadcrumb.users' string is correctly added to the dictionairy):

$params['breadcrumbs'][] = ['label' => $this->get('translator')->trans('breadcrumb.users'), 'route' => 'app_parameters_user_index'];
$params['breadcrumbs'][] = ['label' => $user->getNameAndSurname()];

It seems default extractor looks for "label" field in an array despite the real context (JMSTranslationBundle was smarter in this case).

Nyholm commented 6 years ago

Thank you for this issue.

When you are using $this->get('translator')->trans('breadcrumb.users'), then the extractor will figure that out and extract breadcrumb.users. But is that line also causing an error?

Also, the second line is ignored.. Unless. Is you class named FooControllerType? ref

noofaq commented 6 years ago

I have done a test with very basic controller which easily reproduces the issue:

file named TestController.php (does not contain Type in its name - what a weird idea ;) )

<?php

namespace App\Controller\Test;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class TestController extends Controller
{
    public function foo()
    {
        $foo = [
            ['label' => $this->get('translator')->trans('this.should.be.translated.without.errors')], //ERROR: Form label is not a scalar string
            [/** @Ignore */'label' => $this->get('translator')->trans('this.should.be.translated.without.errors.and.now.it.works')],
            ['label_' => $this->get('translator')->trans('this.works.correctly')],
            ['label' => $this->shouldBeIgnored()], //ERROR: Form label is not a scalar string
            [/** @Ignore */'label' => $this->shouldBeIgnoredAndGoesWithoutError()],
        ];
    }

    private function shouldBeIgnored()
    {
    }

    private function shouldBeIgnoredAndGoesWithoutError()
    {
    }
}

All expected strings are added to the dictionary but mentioned errors appear.

I have done tests by commenting used visitors and I have found that issue seems to be related to FormTypeChoices visitor. Analysing that more I have found that issue can be narrowed down to line 46 in file "/vendor/php-translation/symfony-bundle/DependencyInjection/TranslationExtension.php" which sets SymfonyMajorVersion. Commenting lines 46/47 solves the issue - but I am not sure about other consequences though. I am using Symfony4 and my blind guess is that FormTypeChoices visitor uses hard check === Symfony3 instead of Symfony >=3. That said, it is rather weird that such change introduces review of Controller files so I am not sure it is a real solution.

Nyholm commented 6 years ago

Thank you for this controller. I will add it in a test and make sure it works.

Thanks for the hints.. I'll investigate.