MUlti language support in JSP / Servlet

How to provide support for multiple languages ​​through JSP / Servlet? How to include static data of different languages ​​at runtime based on the selected language?

+7
java java-ee jsp servlets
source share
3 answers

In the plain vanilla JSP / Servlet application, the best solution is the JSTL fmt taglib . (just jstl-1.2.jar in /WEB-INF/lib ). How to use it in Oracle Java EE 5, Part II of Chapter 7 and in this answer: How to internationalize a Java web application? .

If you are using an MVC framework such as Oracle JSF or Apache Struts, you need to consult your specific documentation using the keywords “internationalization” (i18n) or “localization” (l10n). In most cases, they also provide specific tags for this, such as <f:loadBundle> in the case of JSF, which in turn is covered in the Oracle Java EE 5 tutorial part II chapter 15 .

Those i18n tags already check the default language / locale on ServletRequest#getLocale() (you don't need to do this “low level” by checking the header as suggested earlier — which will require more header parsing in accordance with the HTTP specification ). You can let the user select the language itself (drop-down list?) And save it in the session area and specify those tag lists to use it. Here is an example with JSTL fmt taglib:

 <fmt:setLocale value="${someSessionBean.locale}" /> 

.. where ${someSessionBean.locale} can return en , en_US , en_UK , etc. This, in turn, is used by the java.util.ResourceBundle API to load localized text (you do not need to create / load the ResourceBundle itself, taglib tags already do this, just read the related javadoc to find out a little more about how it works).

If you want the language to be available as the first part of the path URL (e.g. http://example.com/en/ , which is best for SEO), then you can best use Filter for this, which listens for /* , checks pathinfo, separates part of the language from it, saves / compares it as / with the session value and forwards the request without the language part to pathinfo further to the desired front-controller.

+10
source share

There are several important aspects to this problem. The first part defines each request locale. You can use something like this:

 HttpServletRequest req ...; String browserLocale = req.getHeader("Accept-Language"); // typically something like 'en' 

Next, you need to decide how to manage the localized content of the site. The most Java-like approach (not necessarily the best) is to externalize all messages with a ResourceBundle. You can learn about the Java core tools for I18N, G13N in your Isolate Language Specific Data .

Using only this approach, in my opinion, is pretty bad. The size of the content of different languages ​​is different, it is better combined with different layouts, etc. Thus, you can completely exclude resource packages (if you do not have a lot of data from several locales) or increase the approach using XSLT or another template that is language specific.

One very effective but highly paid overhead approach is to use a servlet filter to redirect traffic to individual subtitles related to the language (or language). In this case, any click of http://my.domain.fake/xyz will be redirected to http://my.domain.fake/ en /xyz

Finally, it is worth noting that most serious web frameworks have their own I18N support. Their approaches vary depending on the philosophy of structure.

-one
source share

We can create the properties message.properties, messages _ ????. and place these files in the / scr / java directory. (where ???? - en_US, ru_RU, etc.)

Example lines in messages.properties:

 About = About Buy = Buy Company = Company ContactUs = Contact Us 

Then insert into the jsp file, for example, the lines:

  Locale locale = Locale.getDefault(); String lng = locale.getCountry(); session.setAttribute( "language", lng); if (lng.equals( "UA")) locale = new Locale( "uk", "UA"); else if (lng.equals( "RU")) locale = new Locale( "ru", "RU"); else locale = Locale.US; ResourceBundle boundle = ResourceBundle.getBundle( "messages", locale); for (Enumeration e = boundle.getKeys(); e.hasMoreElements(); ) { String key = (String) e.nextElement(); String s = boundle.getString(key); session.setAttribute( key, s); } 

Now you can insert $ {name} in the following jsp code ($ {About}, $ {Buy}, ...).

-one
source share

All Articles