Skip to content
Advertisement

How to prevent twig from auto escaping Japanese characters?

I am trying to display product’s arrival date. I’m using moment.js to format YYYY/MM/DDdate string to Japanese local string. My javascript code is included in a twig file:

JavaScript

As you can see, I used raw filter to prevent twig from escaping Japanese characters. Nevertheless, twig escapes special characters anyway and the text is garbled:

garbled Japanese characters due to twig auto-escaping

Of course, it would be solved if I moved the above snippet to an external file. But seriously, is there not a way to stop twig escaping Japanese characters? Why is raw filter not working?

Advertisement

Answer

Import moment ja locale from the cdn (or from npm/yarm installed package) as seen in https://momentjs.com/docs/#/i18n/

If you use a cdn hosted js then use the following code:

JavaScript

Then instead of:

JavaScript

Use:

JavaScript

And remove the:

JavaScript

line

So your script will be:

JavaScript

Consult the example bellow containing only the formating regarding the language:

JavaScript
JavaScript

So you let moment to format the date and avoid any complexity with js/twig. Just keep it simple and let the js to do its job instead of mixing them all together.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement