WORK IN PROGRESS, NEEDS FORMATTING.
Why internationalise?
Internationalisation is about users being presented with interfaces that are displayed in their preferred language. If your portlet provides options for a user to configure the portlet, or displays headings and other strings on screen, this is an essential step. And it is simple!
...
In order to fix this we need to first move this string into a properties file where it can be translated, then add some additional setup to our pages to read the correct language version of the text into our page.
It's easy, lets do it!
First, create a properties file. Conventionally this is called messages.properties but it can be called anything.
Code Block | ||
---|---|---|
| ||
my.internationalised.string = This is some text |
...
If you have a string that accepts dynamic data, perhaps from a variable, such as "Hello my name is USERNAME, how are you today?" where 'USERNAME' is a variable, you must NOT concatenate strings together within your code. The properties file system contains the facility for placeholders to be set. This is because the way you structure a sentence may not be the same way a user from a different locale structures the same sentence. Using placeholders allows the correct sentence structure to be set for each locale.Do
Don't do this:
Code Block |
---|
greeting.text=Hello {0}.
configure.link.text=Click {0} to configure this item or {1} to go back.
configure.link.here=here |
...
.1=Hello
greeting.text.2=, how are you today
<fmt:message key="greeting.text.1"/> ${username}<fmt:message key="greeting.text.2"/> |
Do this instead:
Code Block |
---|
greeting.text=Hello {0}, how are you today?
|
Then in your JSP you then provide the fmt:message tag a list of parameters that will be substituted for the placeholders:
Code Block |
---|
<fmt:message key="greeting.text.hello"> <fmt:param value="${username}" /> </fmt:message> <fmt:message key="configure.link.text"> <fmt:param value="${username}" /> </fmt:message> |
TODO
...