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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值