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.
Balusc
source share