Twig dynamic translation

Hi guys, I see in doc that is possible to do this:
$translation = $grav[‘language’]->translate([‘HEADER.MAIN_TEXT’], ‘fr’);
now i try to do the same in twig template, I need it to send a translated e-mail (not all, only table header for example) based on user language preference.
I try to do it with twig function {{ t("ORDER_CODE", lang) }} but it seems not work.
It is possible or I have to do it by php?
PS: I have my own php function to prepare e-mail message so I pass lang variable on twig processTemplate
Thank you

Hi guys, really no one can help me with this?
For clarification, I only need to translate based on user language settings, i try also the “tu” but it not work, and I don’t understand why.

Try with this
and :smile:

@iusvar, Thank you for your reply, I’ve read the documentation but nfortunately don’t help.

If you do $grav[‘language’]->translate([‘HEADER.MAIN_TEXT’], ‘fr’); in php and then pass the result to twig it work but if you try to do it whit twig function {{ t("HEADER.MAIN_TEXT", ‘fr’) }} it don’t do anything.
That’s my problem.
So… am I do something wrong ?

That I know in Twig can use the filter |t, so {{ "HEADER.MAIN_TEXT"|t }} that translates into the current language. There are no other parameters in the file /system/src/Grav/Common/Twig/TwigExtension.php at line 92, to indicate a language of your choice.
However it would be great if it was possible.

I’ ve read the same file end I think the problem is at line 593 , if you look the function
public function translate()
return $this->grav[‘language’]->translate(func_get_args());
It don’t accept parameters but func_get_args() from php manual:

Returns an array comprising a function’s argument list

So, it’s the same function you can use in php.
I think it’s a little bug because in the doc you can se this example {{ "SIMPLE_TEXT"|t(12, "London Zoo") }} but how can it works if the function don’t accept parameters??

Although the example of the “12 monkeys in the London Zoo” works, in fact, making some evidence I think you are right, this must be a limited mistake in choosing the language!

I’m investigating on it, if you look at line 373 of /system/src/Grav/Common/Language.php you can find the function translate

public function translate($args, array $languages = null, $array_support = false, $html_out = false)

It accept languages as array, so we only need to know how to pass that information form twig filter or function.
As you said, it’s a litte mistake!!:exploding_head:

I had read, too, you are right … we just have to wait on Monday :scream:

… update
Well, by doing some tests I resolved my way.
The problem is in /system/src/Grav/Common/Language.php: only $args is considered due to func_get_args. As it is built now only the first argument is considered
Then, after line 376 I entered to take a possible second argument

if (is_array($args)) {
   $languages = array_shift($args);

In Twig, using {{ t("HEADER.MAIN_TEXT", ‘fr’) }} now works :smile:

@iusvar you are great!!!

If you have made an improvement, please consider creating a pull request on the core Grav repo: