Search code examples
javafreemarker

Printing JP chars in Freemarker


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.


Solution

  • 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.