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 · 228 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 Anna, 22 hours ago
2 70 1 hour ago
Support · by gtx, 1 week ago
4 233 2 days ago
Support · by Paul Hodges, 2 weeks ago
13 283 5 days ago
Support · by Anna, 3 weeks ago
9 340 1 week ago
Support · by TomW, 2 weeks ago
4 192 2 weeks ago