import java.util.*; public class Polyglot implements PolyglotI18NStrings { protected static Locale partOfTheWorld; protected static Hashtable localeMap; protected static int localeIndex; protected static final int AFTERNOON_START = 12; // hours (24 based) protected static final int EVENING_START = 18; //------------------------------------------------------------------- // Initialize the locale-awareness of the program. //------------------------------------------------------------------- static { // find out where we got transported to partOfTheWorld = Locale.getDefault(); // create a little database of locales that we support localeMap = new Hashtable(); localeMap.put(Locale.ENGLISH , new Integer(0)); localeMap.put(Locale.FRANCE , new Integer(1)); localeMap.put(new Locale("nl","NL") , new Integer(2)); // DUTCH Integer locIndex = (Integer) localeMap.get(partOfTheWorld); if ( locIndex == null ) { locIndex = new Integer(0); } // all user-visible strings can be found at array index N localeIndex = locIndex.intValue(); } //------------------------------------------------------------------- // Program main() entry point //------------------------------------------------------------------- public static void main (String[] args) { GregorianCalendar now = new GregorianCalendar(); now.setTime(new Date()); // output localized "Dear User, Good Morning/Afternoon/Evening" System.out.print(visible(dearUser) + ", "); int hour = now.get(Calendar.HOUR_OF_DAY); if ( hour < AFTERNOON_START ) { System.out.print(visible(goodMorning)); } else if ( hour < EVENING_START ) { System.out.print(visible(goodAfternoon)); } else { System.out.print(visible(goodEvening)); } System.out.println("!"); } //------------------------------------------------------------------- // Utility function to return localized string. //------------------------------------------------------------------- protected static String visible(String[] stringSet) { String localized; try { // select correct string for current locale. localized = stringSet[localeIndex]; } catch (IndexOutOfBoundsException unknownLocale) { // use ENGLISH string as fallback. localized = stringSet[0]; // but print error message so that we know there's a problem. System.out.println("ERROR: visible(" + localized + ") not found!"); System.out.println("(index was " + localeIndex +")"); } return localized; } } // End of Class Polyglot