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

Multi-Language. TWIG in JS

Started by Dima 2 years ago · 4 replies · 270 views
2 years ago

I use Grav and Gantry Helium.

I have a function

// Handle a click on the hint button
hintButton.addEventListener('click', function () {
if (score >= 3) {
score -= 3;
scoreElement.textContent = score;
hintImage.style.display = 'block';
setTimeout(() => {
hintImage.style.display = 'none';
}, 12000);
} else {
showNotification('Text Text Text Text', 'error');
}
});

How to showNotification 'Text Text Text Text' in different languages?

In user/languages/ I have de.yaml en.yaml.

Me and claude.ai haven't found a solution yet(.

2 years ago

I think you're trying to solve it the other way around. Try JS in Twig. Set JS variables for your strings in twig and then use them in JS. At least that's how I'd do

2 years ago

Hi, you can add translations to languages.yaml file in your theme folder

example:

YAML
en:
  THEME_NAME:
    TEXT: 'Text EN!’

de:
  THEME_NAME:
    TEXT: 'Text DE!’

then use it in your templates .html.twig

TWIG
{{ 'THEME_NAME.TEXT’ | t }}

live example - press copy/kopiuj button en/pl:

https://step-bud.pl/en/contact

2 years ago

This doesn't answer the question. You can't use twig in JS

2 years ago

thats why I propose possible solution and use translations in .html.twig

Suggested topics

Topic Participants Replies Views Activity
Support · by Paul Hodges, 4 weeks ago
19 483 5 days ago
Support · by Lauw, 7 days ago
2 99 6 days ago
Support · by Anna, 3 weeks ago
4 426 1 week ago
Support · by gtx, 4 weeks ago
4 377 3 weeks ago
Support · by Anna, 1 month ago
9 484 3 weeks ago