Internationalizing Roo-generated web applications

Roo supports internationalization of the complete UI by using resource bundles for labels and messages. In this recipe, we will look at the web mvc install language command of Roo and see how it simplifies internationalizing the Roo-generated web user interface.

Getting ready

Delete the contents of ch04-recipe sub-directory inside the C: oo-cookbook directory.

Copy the ch04_web-app.roo script into the ch04-recipe directory.

Execute the ch04_web-app.roo script that creates the flight-app Roo project, sets up Hibernate as the persistence provider, configures MySQL as the database for the application, creates the Flight and FlightDescription JPA entities and defines a many-to-one relationship between the Flight and FlightDescription entities. If you are using a different database than MySQL or your connection settings are different than what is specified in the script, then modify the script accordingly.

Start the Roo shell from the C: oo-cookbookch04-recipe directory.

Execute the controller all command to create controllers and views corresponding to the JPA entities in the flight-app project, as shown here:

.. roo> controller all --package ~.web

Execute the perform eclipse command to update the project's classpath settings, as shown here:

.. roo> perform eclipse

Now, import the project into your Eclipse IDE.

How to do it...

For internationalizing the Roo-generated web user interface execute web mvc install language, as shown here:

.. roo> web mvc install language --code es

Created SRC_MAIN_WEBAPPWEB-INFi18nmessages_es.properties
Created SRC_MAIN_WEBAPPimageses.png
Managed SRC_MAIN_WEBAPPWEB-INFviewsfooter.jspx

How it works...

The web mvc install language command accepts a single argument: code, which identifies the language code for which the support needs to be added to the web application. The code argument accepts a pre-defined language code, depending upon the languages supported by Spring Roo. Spring Roo contains translations for the standard messages and labels for the following language codes: de (German), en (English), es (Espanol), it (Italian), nl (Dutch), and sv (Swedish).

When the web mvc install language command is executed, Roo processes the command by taking the following actions:

  • Creates the messages_es.properties in /WEB-INF/i18n/ folder
  • Copies an image icon (es.png) for the language in the images directory
  • Updates /WEB-INF/views/footer.jspx to show the image icon for the language

To check if the support for the Espanol language is correctly installed, deploy and run the flight-app application. The following screenshot shows the home page of the flight-app application, after the Espanol language support is added:

How it works...

As the given screenshot shows, an additional image icon is displayed to allow users to change the language of the web application to Espanol. When a user clicks the image icon corresponding to a language, the lang request parameter is set in the request, which is used by LocaleChangeInterceptor (configured in the /WEB-INF/spring/webmvc-config.xml) for changing the current locale.

The CookieLocaleResolver configured in the /WEB-INF/spring/webmvc-config.xml stores a cookie named locale in the browser, so that users don't need to change their preferred language every time they access the web application.

There's more...

In the Auto-generating Spring MVC controllers and the JSPX views from the JPA entities recipe, we discussed that the messages.properties contains messages and labels that are common to all Roo-generated web applications and the application.properties contains application-specific messages and labels.

As Roo can't provide translations for application-specific messages and labels, the web mvc install language command doesn't create an application_<language-code>.properties file. It is left up to the developer to create an application_<language-code>.properties file for specific language codes and provide translations.

See also

  • Refer to the Auto-generating Spring MVC controllers and JSPX views from JPA entities recipe for LocaleChangeInterceptor and CookieLocaleResolver configuration
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.101.109