Internationalization and the Java Stack - Part 1
This training will cover the basics of internationalization in the Java Stack, including resource bundles, Spring message sources, locale resolvers, locale change interceptors, ...
- The Java Stack
- Your favorite IDE, ready to import a maven project
Lab 1 Internationalize a Page
- Download the lab code and import the project into your favorite IDE.
- Get your application deployed and running in Tomcat.
- Go to http://localhost: 8080/spring-i18n-labs ( you should get an exception about "No message found under code 'hello'...")
- Configure the MessageSource so that internationalization in the lab.jsp page works.
- Notice the lab.properties file in src/main/bundles and the lab.jsp in src/main/webapp/WEB-INF/views where those resources are utilized.
- Add the appropriate basename property for this bundle to the messageSource definition in src/main/webapp/WEB-INF/spring-i18n-labs-servlet.xml
<property name="basenames"> <list> <value>classpath:messages</value> <value>classpath:lab</value> </list> </property>
- Internationalize the "Internationalization Labs" text on the page.
- Extract the "Internationalization Labs" text into a property file.
- Reference the tag on the page via key (code), using the Spring taglib.
- Verify that you can hit the page (http://localhost: 8080/spring-i18n-labs) and have English text displayed.
Lab 2 Configure Locale Change and Resolution
- Add resources and configuration to allow for changing / resolving a locale.
- Add a new properties file for a locale other than English (for instance lab_es.properties).
- Copy all the English properties into the lab_es.properties file and "translate" them.
- Verify that appending something like ?locale=es to the url does not change the locale.
- Add the locale-resolver and localeChangeInterceptor definitions in your *-servlet.xml file.
<stack-web:locale-resolver /> <!-- this one goes inside the mvc:interceptors tag --> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
- Verify that appending ?locale=es changes the locale now.