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

The correct way to extract translations from Twig #1

Open
sabat24 opened this issue Nov 25, 2020 · 7 comments
Open

The correct way to extract translations from Twig #1

sabat24 opened this issue Nov 25, 2020 · 7 comments
Assignees

Comments

@sabat24
Copy link

sabat24 commented Nov 25, 2020

I tried to decorate service Coffreo\JMSTranslationJsExtractorBundle\Extractor\JsFileExtractor but Symfony gives me information that not such service exists.
Then I overrided service declaration in config to point out my custom service

coffreo.jms_translation.js_extractor.js:
        class:  App\Service\Decorators\JsFileExtractor  <---- my class
        arguments:
            - '@jms_translation.file_source_factory'
            - ['js', 'jsx']
        tags:
            - { name: 'jms_translation.file_visitor' }

I tried to extend your class to only override visitTwigFile method, but I'm not able to extend final class.

Finally I created a following constructor:

public function __construct(FileSourceFactory $fileSourceFactory, $extensions = null, JsTranslationExtractorInterface $extractor = null)
    {
        $this->fileSourceFactory = $fileSourceFactory;
        $this->jsFileExtractor = new \Coffreo\JMSTranslationJsExtractorBundle\Extractor\JsFileExtractor($fileSourceFactory, $extensions, $extractor);
    }

and "extended" your methods like that

public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue)
    {
        $this->jsFileExtractor->visitFile($file, $catalogue);
    }

   public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, Node $ast)
    {
        $translations = $this->jsFileExtractor->findTranslations($file);

        foreach ($translations as $translation) {
            /** @var $translation TranslationString */
            $message = new Message($translation->getMessage(), $translation->getContext('domain') ?: 'messages');
            $message->setDesc($translation->getContext('desc'));
            $message->setDesc($translation->getContext('meaning'));
            $message->addSource($this->fileSourceFactory->create($file, $translation->getLine()));
            $catalogue->add($message);
        }
        $this->jsFileExtractor->visitTwigFile($file, $catalogue, $ast);

    }

However it's not the best option. How it should be done?

@emri99
Copy link
Member

emri99 commented Nov 25, 2020

Hello

The purpose of this bundle is to extract translations from js file using bazinga js bundle syntax.

Is there any reason you don't use the trans twig fonction to extract translations strings from twig files ?

@emri99 emri99 self-assigned this Nov 25, 2020
@sabat24
Copy link
Author

sabat24 commented Nov 25, 2020

Sometimes I prefer/need to add some JS code directly inside twig block with some params for example:

file.rejectDimensions = function () {
    done(Translator.trans('dropzone_min_width_restriction', {'minImageWidth': that.options.minImageWidth}, 'admin_js'));
};

I can't use trans because of minImageWidth param.

@emri99
Copy link
Member

emri99 commented Nov 25, 2020

Ok, it's a good reason !
That wasn't something I thought while creating this bundle.
I'll take a look more deeply in the next few hours

@emri99
Copy link
Member

emri99 commented Dec 7, 2020

Sorry for delay, I haven't look really yet, but given classes are final, I don't see a better way for now. can you share a bit more your code so I can integrate this use case in this bundle ?

@sabat24
Copy link
Author

sabat24 commented Dec 13, 2020

My use case

  1. Users are allowed to upload some kind of files.
  2. Each user can have got some restrictions like image dimension, file size oraz file type.
  3. Restrictions are generated based on data set in DB for current logged user.
  4. In template I dynamically render following part of code to add validation messages to Dropzone.
accept: function (file, done) {
    let that = this;
    {% if user.dimensionRestriction %}                    
      file.rejectDimensions = function () {
          done(Translator.trans('dropzone_min_width_restriction', {'minImageWidth': that.options.minImageWidth}, 'admin_js'));
      };
      file.acceptDimensions = done;
      {% endif %}
},

There is a little bit more logic in accept and init functions which are generated in twig and parameters like fileSize are taken to messages from Dropzone options.

Of course I could rewrite that code and put everything into some generic js method, put my dynamic params into html data and then read them from that generic method.

I think it's a rare case, but I was wonder if your bundle can handle it. All in all I made some hack, so it's generally working fine.

If you want to keep your classes final the only thing I came up with is it to make JsFileExtractor class as service and then decorate it.

@emri99
Copy link
Member

emri99 commented Oct 22, 2021

Hey ! As you may have understood, I didn't do any changes to integrate your use case. Sorry for that.

With arrival of PHP8, a contributor has added today the support of PHP8 on others libs related to this one js-translation-extractor and php-translation-js-extractor-bundle.

I plan to add PHP8 support on this lib as well but it needs to remove PHP 5.x / 7.0 / 7.1 support. (#2)
Do you still uses theses PHP versions ?

If you tell me no, I would probably add your use case only in the next upcoming version covering PHP ≥ 7.2
If you tell me yes, I will try to add your use case on the current version before adding PHP8 support.
Or maybe your use case is not useful any more, so we can close this issue without updating code.

Please let me know your needs, so I can try to adapt update strategy to the simplest needed one ;)

@sabat24
Copy link
Author

sabat24 commented Oct 22, 2021

Thanks for clarification. I think that my case is quite rare so I do not think that resolving it is worth your work. What's more there are some solutions to write a few good workarounds. For me we can close this issue.

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

No branches or pull requests

2 participants