Spring MVC Internationalization

Spring MVC Internationalization


Text labels and headings can be externalized into resource bundles, allowing Spring's internationalization features to automatically display web pages in the appropriate language.

Tip

Tip - as an alternative, check out the Google website translator gadget , which can dynamically translate web pages into 51 languages (note that, in Google's own words, "it's not a perfect substitute for the art of professional translation").

Resource Bundles

We set up three resource bundle files called messages_en.properties (English), messages_fr.properties (French) and messages.properties (the default if no other file is found).

The files contain key-value pairs representing text for the web pages in different languages.

messages_en.properties

01. application.name=hello world
02.  
03. #navigation.back=Back
04. #falls back to messages.properties
05.  
06. country.plural=Countries
07. country.details=Country Details
08. country.name=Name
09. country.area=Area
10. country.population=Population
11. country.updatedOn=Last Updated
12. country.currency=Currency


messages_fr.properties

01. application.name=bonjour monde
02.  
03. navigation.back=Retournez
04.  
05. country.plural=Pays
06. country.details=Détails de pays
07. country.name=Nom
08. country.area=Taille
09. country.population=Population
10. country.updatedOn=Dernier mis à jour
11. country.currency=Devise


messages.properties

01. application.name=hello world
02.  
03. navigation.back=Back
04.  
05. country.plural=Countries
06. country.details=Country Details
07. country.name=Name
08. country.area=Area
09. country.population=Population
10. country.updatedOn=Last Updated
11. country.currency=Currency

We tell Spring how to locate these resource bundles in the configuration

section.

JSP Pages

Next, we convert our JSP pages to use the <spring:message> tag from the Spring tag library to display static text, and to use the JSTL <fmt:formatNumber> and <fmt:formatDate> tags to display numbers and dates.

01. < table >
02.    < tr >
03.      < td >< spring:message code = "country.name" /></ td >
04.      < td >${country.name}</ td >
05.    </ tr >
06.    < tr >
07.      < td >< spring:message code = "country.area" /></ td >
08.      < td >< fmt:formatNumber type = "number" value = "${country.area}" /></ td >
09.    </ tr >
10.    < tr >
11.      < td >< spring:message code = "country.population" /></ td >
12.      < td >< fmt:formatNumber type = "number" value = "${country.population}" /></ td >
13.    </ tr >
14.    < tr >
15.      < td >< spring:message code = "country.updatedOn" /></ td >
16.      < td >< fmt:formatDate value = "${country.populationLastUpdated}" /></ td >
17.    </ tr >
18.    < tr >
19.      < td >< spring:message code = "country.currency" /></ td >
20.      < td >${country.currency}</ td >
21.    </ tr >
22. </ table >

Locale

The appropriate resource bundle (and therefore, the appropriate set of messages to be displayed) is automatically selected by Spring, based on the end user's locale. If the display language setting is English, then the messages_en.properties file will be used. If the language is French then the messages_fr.properties file will be used, and so on.

If a file is not available for a language, or a message within a file cannot be found (for example, navigation.back in messages_en.properties has been commented out), then Spring will fall back to using the entries in messages.properties .

The same JSP page can now be displayed in Engish or French.

English
Hello World Application in English

French
Hello World Application in French


Tip

Tip - change the language preferences in your browser to access the different resource bundles for the application.

Formatting

In addition to Spring's <spring:message> tag, we also use the JSTL <fmt:formatNumber> and <fmt:formatDate> tags to display numbers and dates.

A number that is displayed as 999,999,999 in English is displayed as 999 999 999 in French, and dates displayed as Mon dd, yyyy in English are displayed as dd mon. yyyy in French. Month names and their abbreviations are also converted to the appropriate language.

 

http://levelup.lishman.com/spring/getting-started/internationalization.php

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章