Printing JP chars in Freemarker

Viewed 109

I am trying to print JP chars. I am using java 8 and freemarker 2.3.31. I have following code,

output starts
${(("こんにちは世界")?right_pad(12,"-"))[0..*12]}
${(("こんにちは")?right_pad(12,"-"))[0..*12]}
output ends
output starts
${(("こんにちは世界")?right_pad(12))[0..*12]}*
${(("こんにちは")?right_pad(12))[0..*12]}*
output ends

and below is the output. Why is it in not ending in same place.

output starts
こんにちは世界-----
こんにちは-------
output ends
output starts
こんにちは世界     *
こんにちは       *
output ends

what am i missing? Same thing when i do in english works fine and both the lines starts and ends at same place. I tried adding <#ftl encoding="Shift_JIS"> to my ftl file.But no luck. Can someone help. TIA.

2 Answers

It is not ending at the same place because the length of a Japanese character is different from the length of the -.

The solution would be to use a Japanese character with the same width as a kanji, and the same meaning as the dash. I don't know if such a character exists (maybe U+30FB: )

For Japanese you should use the wider ideographic space (U+3000) instead normal space. That is, ?right_pad(12, '\x3000').

If you need a similarly wide dash, that's U+30FC, so like ?right_pad(12, '\x30FC').

All these can of course be typed directly as well, like ?right_pad(12, 'ー'), but in that case be careful with the template file encoding.

Related