Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Twig dynamic translation

Started by 12k 9 years ago · 10 replies · 2611 views
9 years ago

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

9 years ago

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.

9 years ago

@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 ?
Thank's

9 years ago

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.

9 years ago

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:

Blockquote
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??

9 years ago

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!

9 years ago

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

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!!🤯

9 years ago

I had read, too, you are right ... we just have to wait on Monday 😱

... 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

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

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

last edited 08/20/17 by Severo Iuliano
9 years ago

@iusvar you are great!!!

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 50 6 hours ago
Support · by Anna, 2 days ago
2 58 9 hours ago
Support · by Justin Young, 10 hours ago
1 28 10 hours ago
Support · by Duc , 1 week ago
2 63 5 days ago
Support · by Colin Hume, 1 week ago
2 53 5 days ago