jblond / twig-trans Goto Github PK
View Code? Open in Web Editor NEWTwig 3 translation extension
Home Page: https://github.com/JBlond/twig-trans
License: MIT License
Twig 3 translation extension
Home Page: https://github.com/JBlond/twig-trans
License: MIT License
Was hoping to use this plugin with twig 3 since the symfony translation thing does not seem to work with xgettext, but came upon a significant issue.
In Translation::replaceContext the code does not account for objects. So if my context has something like:
'reply' => (object) [
'id' => 123,
'body' => 'some text'
]
PHP errors out when you get to the str_replace on line 50.
Also might save considerable effort if the string were first checked for {{
to see if any vars even need replacing.
I'm going to fork and code a fix. Maybe you'll be interested in a PR then if this project is still active.
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
I'm leaving this in case anyone else encounters the same problem
{{ "hello_message"|trans({'username': user.name}) }}
Where hello_message
is hi %username%!
.
This case won't work because the third param of TwigFilter is being ignored.
To make it work i refactored the filter like this:
$filter = new TwigFilter(
'trans',
function ($string, $args) {
return empty($args) ? _($string) : tprintf(_($string), $args);
},
);
But maybe it could be compatible with both scenarios with:
$filter = new TwigFilter(
'trans',
function ($context, $string, $args) {
return \jblond\TwigTrans\Translation::transGetText($string, empty($args) ? $context : $args);
},
['needs_context' => true]
);
Hi there,
is it possible for you, to add trans with {'key': value, ...}
syntax for the trans
tag?
So translation first and then the replace of the keys inside the string.
This optional with
parameter will be very helpful. (for older projects with this syntax)
Have a nice day.
Hello, I have this environment: PHP 8.2, Twig 3.5.1, jblond/twig-trans 1.1.0
Filter was defined as shown into README:
$filter = new TwigFilter(
'trans',
function ($context, $string) {
return Translation::transGetText($string, $context);
},
['needs_context' => true]
);
Templates were generated into cache (by a script that once worked without problems) but:
echo gettext("text-to-be-translated");
)$this->env->getFilter('trans')->getCallable()("text-to-be-translated");
Consequently .po files generated by xgettext did not show text to be translated with the trans filter
After digging into Twig code I came up wit the following solution:
$filter = new TwigFilter(
'trans',
'gettext',
[ 'is_safe_callback' => 'twig_escape_filter_is_safe']
);
Maybe this helps someone else stuck in the same situation
Cheers!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.