Giter Site home page Giter Site logo

jblond / twig-trans Goto Github PK

View Code? Open in Web Editor NEW
12.0 6.0 1.0 38 KB

Twig 3 translation extension

Home Page: https://github.com/JBlond/twig-trans

License: MIT License

PHP 97.82% Twig 2.18%
twig-extension twig-templates translation gettext i18n intl multitype twig twig-trans i18nextension

twig-trans's Issues

Bug: Objects in $context cause error

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.

Generating po files from cached twig

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

trans arguments are ignored

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]
    );

Add trans with syntax

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.

trans filter definition

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:

  • trans tags worked correctly (rendered as echo gettext("text-to-be-translated");)
  • trans filters were instead rendered into cache template as $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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.