JBlond / twig-trans

Twig 3 translation extension
https://github.com/JBlond/twig-trans
MIT License
12 stars 1 forks source link

Generating po files from cached twig #4

Closed NicolasCARPi closed 2 years ago

NicolasCARPi commented 2 years ago

Hello,

First, thank you for providing this, it is very helpful in my project and works as expected!

Previously, for generating translation files (.mo/.po) I used to force generation of twig cache files from templates, so that xgettext could grab the string from gettext() call and generate the .po (script link). This was possible because in v2 the cache generated code would use the gettext() function.

Now with the plugin it is not the case anymore. Example output:

echo twig_escape_filter($this->env, call_user_func_array($this->env->getFilter('trans')->getCallable(), [$context, "go back to login page"]), "html", null, true);

So do you think it would be possible to somehow be able to use this workflow again?

Please consider this issue more like a question rather than a feature request. I was able to workaround this limitation by buying the PRO version of Poedit which can extract from twig templates directly, thereby rendering the previous workflow caducous. But this might prove useful to other users I guess, so maybe it is worth investigating, I don't know.

Best, ~Nico

JBlond commented 2 years ago

Hi Nico, the code still uses gettext

See https://github.com/JBlond/twig-trans/blob/master/src/jblond/TwigTrans/TransNode.php#L184 and https://github.com/JBlond/twig-trans/blob/master/src/jblond/TwigTrans/TransNode.php#L67

Also like in the example you can call it even without the getCallable call

like

Translation::transGetText($string, $context);

let me know if that works.

NicolasCARPi commented 2 years ago

Yes but xgettext doesn't recognize this, and the code is generated by twig so I don't have the possibility to modify it (unless of course I search and replace). Anyway, extracting the strings from templates is a better approach! Thanks for your input on this.

JBlond commented 2 years ago

@NicolasCARPi Can you please post your solution? I'm interested.

NicolasCARPi commented 2 years ago

Well, my solution was to buy the PRO version of Poedit, which supports extracting strings from twig templates ;)

JBlond commented 2 years ago

Well, can you please test https://github.com/JBlond/twig-trans/tree/extract I think I managed to get it working.

NicolasCARPi commented 2 years ago

After fiddling around a bit with it, it seems it only works with terms that have plural form, as the twig generated cache code uses the ngettext function call, which is recognized by xgettext, but not the rest. So the problem stays the same as before.

Furthermore, I'm not sure what the Extract class brings to the table as it's basically only the first part of my script, but in php.

I believe a useful addition would be a console script that would parse the twig html templates and generate some php code that can then be recognized by xgettext. Without even the need to call Twig at all. Just to extract the strings.

JBlond commented 2 years ago

Do you have only {{ }} as a placeholder or also |trans?

With the plural nor singular. I tried and the script did the job. The generated file is https://gist.github.com/JBlond/54d374d800f601ffc0d52b42a3c154f0 done from the https://github.com/JBlond/twig-trans/blob/extract/example/tpl/default.twig example and the generated po file.

NicolasCARPi commented 2 years ago

In my code, the plural will use {% trans %} wheras the rest will use |trans. The problem is that line 15 won't get a gettext() call here: https://gist.github.com/JBlond/54d374d800f601ffc0d52b42a3c154f0#file-cachedfile-php-L56. So it looks like the |trans filter is when it fails.

JBlond commented 2 years ago

On my dev env with PHP 8.1.3 I don't have that problem.

JBlond commented 2 years ago

I wonder what is so different between |trans and {% trans %}They use the same funktion. Sure the parser is different. Maybe I have to make the |trans work more like the other class.

JBlond commented 2 years ago

No matter what. Even the internal twig 3 filter will cause an echo twig_escape_filter() function call in the cache file. I don't see there a chance to prevent that, except to override the compiler $twig->setCompiler($compiler);