3

Using the Grails internationalization messages.properties I'm trying to create a multi-line message, but cannot seem to find a way to create a new line without using the <br> element, and I'd prefer to keep presentation logic out of the message. I've tried using "\n" but that doesn't get rendered.

I know I can use multiple messages "message.1=...", "message.2=...", but that doesn't seem as clean either.

Here's what I'd like to be able to do:

messages.properties

helptext=First Line\nSecond Line\nThird Line

page.gsp

<g.message code="helptext"/>

result:

First Line
Second Line
Third Line

Everything I've found either says to use <br> element, or do a replaceAll on \n, but I was hoping to not have to use extra processing to handle this.

mnd
  • 2,709
  • 3
  • 27
  • 48
  • 1
    Since it's a properties file, I think you should break line in [this way](http://stackoverflow.com/questions/5576164/java-possible-to-line-break-in-a-properties-file) –  Aug 21 '13 at 15:02
  • Thanks @SérgioMichels, I have also tried the back slash, but it doesn't seem to work, I'm not sure if it is Java specific and not properly transferred for grails. – mnd Aug 21 '13 at 15:08

3 Answers3

7

I think you have to use <br> in the message directly.

//messages.properties
helptext=First Line<br>Second Line<br>Third Line

//Gsp
<p><g:message code="helptext"/><p>

\ gives the ability to break the line in the properties file but renders as a single line in view.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • 1
    Selecting this answer because it fits my use case best, because there are only a couple places where I need to do this. – mnd Aug 26 '13 at 17:38
3

For me (i18n message properties in Grails 2.0 project) worked following line:

property = Line1\\nLine2\\nLine3

HTML tag BR worked also fine if displayed on HTML page, but was not any good for me, because I in my case this text needed to be a text string not HTML.

Serpent
  • 31
  • 2
  • It worked for me too. I was creating a message for my onclick event which calls a JS alert(). `default.somebutton.message=bla bla bla1\\n\\nbla bla bla2` – victorf Apr 30 '15 at 18:09
2

You could write a custom tag that converts \n into br tags as well. It would just need to call the messageSource bean and parse the results. Thus your messages would not have to be HTML-specific

Jim Sosa
  • 578
  • 3
  • 21
  • This answer requires a bit more overhead, but I really like it as an alternative solution, and will keep this in mind for the future, as it might require more of a paradigm shift to use throughout the app. Thanks! – mnd Aug 26 '13 at 17:40