Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Can I put a twig variable inside a translated string?

Solved by phi ARCHITECT View solution

Started by Ptarmic 6 years ago · 2 replies · 2127 views
6 years ago

I've sucessfully created a languages.yaml file with text strings which is working great but I'm wondering if its possible to take a step further and insert twig variables into a string. For example I have string like "Follow XX on Twitter" where I would like to do something like "Follow {{ page.header.post_author }} on Twitter". Is there anyway other than string concatenation to do this?

6 years ago Solution

Hi @Ptarmic -

This is the way to insert variables into your language string:

Translations with Variables

In your language file:

TXT
FOLLOW: Follow %s on %s.

then in your template:

TWIG
{{ 'FOLLOW'|t( page.header.post_author,  'Twitter' )  }}
  • phi
👍 1
6 years ago

@phi:
{{ 'FOLLOW'|t( page.header.post_author, 'Twitter' ) }}

Thanks @phi that worked perfectly!

Suggested topics

Topic Participants Replies Views Activity
General · by Hanns Mattes, 6 days ago
0 93 6 days ago
General · by Dima, 2 weeks ago
1 121 2 weeks ago
General · by pamtbaau, 2 weeks ago
0 192 2 weeks ago
General · by lynbor, 2 months ago
5 701 4 weeks ago
General · by Andy Miller, 1 month ago
0 425 1 month ago