i18n - Newlines in ResourceBundle messages

R

Rhino

I'm posting this in case anyone is trying to figure out how to put newline
characters in messages that they put in ResourceBundle message files in
their i18n (internationalization) efforts. I had the same
question the other day but couldn't find anything in Google or the Java
Tutorial that discussed this.

Two approaches that didn't work:
- embedding \n (or \\n) directly in the message text e.g.
msg014 = Failed to get database connection. \n\nError: {1}
- embedding the Unicode character for newline directly in the message text
e.g.
msg014 = Failed to get database connection. \u2424 Error: {1}

The approach I found which does work is to pass a newline to the message
just the way you would any other variable in the message. Here is the
relevant code fragment:

Connection dbConnection = null;

try {

dbConnection = DriverManager.getConnection(strURL, strUserName,
strPassword);

}

catch (SQLException sql_excp) {

Object[] msgArgs = { "\n", sql_excp }; //first variable is a newline
character, second variable is an exception message

msgFmt.applyPattern(locMsg.getString("msg016"));

String msg016 = msgFmt.format(msgArgs);

JOptionPane.showMessageDialog(this, msg016, null,
JOptionPane.ERROR_MESSAGE);

return(Result);

}

This is the message itself:

msg016 = Failed to get database connection. {0}{0}Error: {1}

Note that you can repeat the newline variable as many times as you like
within the message.

I assume that you could put other special characters, like tabs, into a
message in the same way but I haven't actually tried that yet.
 
S

Sudsy

Rhino said:
I'm posting this in case anyone is trying to figure out how to put newline
characters in messages that they put in ResourceBundle message files in
their i18n (internationalization) efforts. I had the same
question the other day but couldn't find anything in Google or the Java
Tutorial that discussed this.

You've made your life unnecessarily complex. In fact it IS possible to
include the newline character in a message. If you select "Show source"
in your browser then you'll see the line break. Unfortunately, that
won't actually result in a line break on display since it's just white
space according to HTML.
Two options:
- wrap the message in the <pre> (preformatted text) tag
- insert <br> tags in the message where you want the breaks

HTH
 
F

Filip Larsen

Rhino skrev
The approach I found which does work is to pass a newline to the message
just the way you would any other variable in the message.

If you are going to make your own substitution anyway, you might as well
use \n in the resource file and replace them with String.replaceAll.


Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top