001package edu.pdx.cs410J.datesAndText;
002
003import java.io.*;
004import java.text.*;
005import java.util.*;
006
007/**
008 * This class shows off some of Java's internationalization (i18n)
009 * capabilities using the <code>Locale</code> class.
010 */
011public class AroundTheWorld {
012
013  private static PrintWriter out = new PrintWriter(System.out, true);
014  private static PrintWriter err = new PrintWriter(System.err, true);
015
016  private static void usage() {
017    err.println("usage: java AroundTheWorld [options]");
018    err.println("  -country code      Which country?");
019    err.println("  -language code     Which language?");
020    err.println("  -timeZone code     Which time zone?");
021    err.println("If any code is ??, then all possible values for " +
022                "that code are printed");
023    System.exit(1);
024  }
025
026  /**
027   * Prints the available locales formatted using a given locale.
028   */
029  private static void printAvailableLocales(Locale locale) {
030    Locale[] locales = Locale.getAvailableLocales();
031    out.println("Available locales:");
032    for (int i = 0; i < locales.length; i++) {
033      Locale l = locales[i];
034      out.println("Locale " + i + " of " + locales.length);
035      out.println("  Name: " + l.getDisplayName(locale));
036      out.println("  Country: " + l.getDisplayCountry(locale) + " (" +
037                  l.getCountry() + ")");
038      out.println("  Language: " + l.getDisplayLanguage(locale) + 
039                  " (" + l.getLanguage() + ")");
040      out.println("  Variant: " + l.getDisplayVariant(locale) + " (" +
041                  l.getVariant() + ")");
042    }
043  }
044
045  /**
046   * Prints the codes for all available time zones formatted for a
047   * given locale.
048   */
049  private static void printTimeZones(Locale locale) {
050    String[] codes = TimeZone.getAvailableIDs();
051    out.println("Time zones");
052    for (int i = 0; i < codes.length; i++) {
053      String code = codes[i];
054      TimeZone tz = TimeZone.getTimeZone(code);
055      out.println("  " + code + ": " + tz.getDisplayName(locale));
056    }
057  }
058
059  /**
060   * Prints out today's date and time in a specific locale.
061   */
062  private static void printToday(Locale locale, TimeZone tz) {
063    DateFormat df = 
064      DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,
065                                     locale);
066    df.setTimeZone(tz);
067    out.println("Right now: " + df.format(new Date()));
068  }
069
070  /**
071   * Prints out a number in various locale-specific formats
072   */
073  private static void printNumber(double number, Locale locale) {
074    NumberFormat nf;
075
076    nf = NumberFormat.getNumberInstance(locale);
077    out.println("A number: " + nf.format(number));
078
079    nf = NumberFormat.getCurrencyInstance(locale);
080    out.println("Currency: " + nf.format(number));
081
082    nf = NumberFormat.getPercentInstance(locale);
083    out.println("Percent: " + nf.format(number));
084  }
085
086  /**
087   * Parses the command line to determine which locale to display
088   * information in.  Prints out dates, times, and currencies using
089   * various locales.
090   */
091  public static void main(String[] args) {
092    String countryCode = null;
093    String languageCode = null;
094    String timeZoneCode = null;
095
096    // Parse the command line
097    for (int i = 0; i < args.length; i++) {
098      if (args[i].equals("-country")) {
099        if(++i >= args.length) {
100          err.println("** Missing country code");
101          usage();
102        }
103
104        countryCode = args[i];
105
106      } else if (args[i].equals("-language")) {
107        if(++i >= args.length) {
108          err.println("** Missing language code");
109          usage();
110        }
111
112        languageCode = args[i];
113
114      } else if (args[i].equals("-timeZone")) {
115        if(++i >= args.length) {
116          err.println("** Missing time zone code");
117          usage();
118        }
119
120        timeZoneCode = args[i];
121
122      } else {
123        err.println("** Unknown option: " + args[i]);
124        usage();
125      }
126    }
127
128    Locale locale;
129
130    if (countryCode == null) {
131      // Use default
132      locale = Locale.getDefault();
133
134    } else if (countryCode.equals("??")) {
135      locale = Locale.getDefault();
136      printAvailableLocales(locale);
137
138    } else if (languageCode == null) {
139      // Use default
140      locale = Locale.getDefault();
141
142    } else if (languageCode.equals("??")) {
143      locale = Locale.getDefault();
144      printAvailableLocales(locale);
145
146    } else {
147      locale = new Locale(languageCode, countryCode);
148    }
149
150    TimeZone timeZone;
151    if (timeZoneCode == null) {
152      // Use default
153      timeZone = TimeZone.getDefault();
154
155    } else if (timeZoneCode.equals("??")) {
156      timeZone = TimeZone.getDefault();
157      printTimeZones(locale);
158
159    } else {
160      timeZone = TimeZone.getTimeZone(timeZoneCode);
161    }
162
163    // Print out some interesting info
164    out.println("");
165    printToday(locale, timeZone);
166    printNumber(1234.56, locale);
167    out.println("");
168
169  }
170
171}