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

Multi-Language. TWIG in JS

Started by Dima 2 years ago · 4 replies · 107 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 Thomas, 1 week ago
2 56 13 hours ago
Support · by Anna, 3 days ago
2 61 16 hours ago
Support · by Justin Young, 17 hours ago
1 32 17 hours ago
Support · by Duc , 1 week ago
2 66 5 days ago
Support · by Colin Hume, 1 week ago
2 59 5 days ago