Java Q&A by Jared Jackson Listing One package com.myCompany.myProject; import java.io.*; import java.util.*; import java.text.MessageFormat; public class Translator { // The Resource Bundle super class private static ResourceBundle resource_bundle; // The current Locale from which to access translations private static Locale locale; // Initializes the values of the resource bundle and locale, by default // the locale is defined by the virtual machine. The resource bundle is // looked up by using a file name, in this case looking in the // 'Resources'directory using the base class name 'weather' private static void initBundle() { try { if (Translator.locale == null) Translator.locale = Locale.getDefault(); Translator.resource_bundle = ResourceBundle.getBundle("Resources" + File.separator + "sample", locale); } catch(MissingResourceException mre) { } } // Also provided is a method for changing locale from within a program. // The resource bundle location is unchanged. public static void setLocale(Locale locale) { try { Translator.locale = locale; Translator.resource_bundle = ResourceBundle.getBundle("Resources" + File.separator + "sample", locale); } catch (MissingResourceException mre) { } } // A method for retrieving translations from the resource bundle public static String getTranslation(String key) { if (key == null ) return ""; if (Translator.resource_bundle == null) initBundle(); try { return Translator.resource_bundle.getString(key); } catch (Exception e) { return key; } } // Returns contextually specific translations public static String getTranslation(String key, String[] lookups) { if (key == null ) return ""; if (resource_bundle == null) initBundle(); try { String retStr = resource_bundle.getString(key); return MessageFormat.format(retStr, lookups); } catch (Exception e) { return key; } } } Listing Two Public java.util.Locale getLocaleFromBrowser(javax.servlet.http.HttpServletRequest request) { // Define the default locale Locale DEFAULT_LOCALE = Locale.getLocale(); // Error Check if (request == null) return DEFAULT_LOCALE; // Retrieve the 'Accept-Language' HTTP header item String str_loc = request.getHeader("Accept-Language"); if (str_loc == null) retrun DEFAULT_LOCALE; String new_loc = null; // We are interested in the first language accepted int index = str_loc.indexOf(","); // Put the first locale encoding into new_loc if (index >= 0) { new_loc = str_loc.substring(0, index); } else { // Languages can be separated by either commas or semi-colons index = str_loc.indexOf(";"); if (index >= 0) { new_loc = str_loc.substring(0, index); } else { new_loc = str_loc; } } String language = new String(); String country = new String(); index = new_loc.indexOf("_"); if (index >= 0) { language = new_loc.substring(0, index); country = new_loc.substring(index+1); } else { language = new_loc; } // Create and return the locale return new Locale(language, country); } Listing Three