In a global environment, your application may have to accept users with different locale preferences. Determine the preferred locale of the user. Once that is known, the application should construct HTML content in the language of the locale, and follow the cultural conventions implied by the locale.
One of the most common methods in determining the user locale, is based on the default ISO locale setting of the browser of the user. Usually a browser sends locale preference settings to the HTTP server with the Accept-Language
HTTP header. If this header is set to NULL
, then there is no locale preference information available and the application should ideally fall back to a predefined application default locale.
Both JSP pages and Java Servlets can use calls to the Servlet API to retrieve the Accept-Language
HTTP header as shown in Example 9-2.
This code gets the Accept-Language
header from the HTTP request, extracts the first ISO locale, and uses it as the user-desired locale.
Example 9-2 Determining User Locale in Java Using the Accept-Language Header
String lang = request.getHeader("Accept-Language") StringTokenizer st = new StringTokenizer(lang, ",") if (st.hasMoreTokens()) userLocale = st.nextToken();