HelloWorld.java
001 package examples.i18n.simple;
002 
003 import java.util.Locale;
004 import java.text.MessageFormat;
005 
006 import weblogic.i18n.Localizer;
007 import weblogic.i18ntools.L10nLookup;
008 
009 /**
010  * This example shows various ways of internationalizing an application
011  * using simple message catalogs.
012  <p>
013  * Usage: java examples.i18n.simple.HelloWorld [lang [country]]
014  <p>
015  * lang is a 2 character ISO language code. e.g. "en"
016  * country is a 2 character ISO country code. e.g. "US"
017  <p>
018  * Usage of any of the languages supported by this example presumes
019  * the existence of the appropriate OS localization software and character
020  * encodings.
021  <p>
022  * The example comes with catalogs for English (the default) and  French. 
023  * The catalog source is in the following files, and were built 
024  * using the catalog editing utility, weblogic.i18ntools.gui.MessageEditor.
025  <p>
026  <pre>
027  * English(base language)       ..\msgcat\Helloworld.xml
028  * French                       ..\msgcat\fr\FR\HelloWorld.xml
029  </pre>
030  <p>
031  * To build this example run the build.sh(UNIX) or build.cmd (NT) scripts from
032  * the examples/i18n/simple directory. CLIENT_CLASSES must be set up and
033  * needs to be in the classpath when running the example.
034  *
035  @author Copyright (c) 2000,2009, Oracle and/or its affiliates. All Rights Reserved.
036  */
037 public final class HelloWorld
038 {
039   public static void main(String[] argsthrows Exception {
040     /*
041      * The easiest method for displaying localized text is to 
042      * instantiate the generated formatter class for the HelloWorld catalog.
043      * This class contains convenience methods that return localized text for
044      * each message defined in the catalog.  The class name is
045      * the catalog name followed by "TextFormatter". 
046      *
047      * Normally, one would use the default constructor to obtain 
048      * formatting in the current locale. In this example we'll use a locale
049      * based on  arguments to construct the TextFormatter. 
050      */
051     Locale lcl;
052     if (args.length == 0) { // default is default locale for JVM
053       lcl = Locale.getDefault();
054     }
055     else {
056       // get the language code
057       String lang = args[0];
058       String country = "";
059       if (args.length >= 2) {
060         // get the country code
061         country = args[1];
062       }
063       lcl = new Locale(lang,country);
064     }
065     /*
066      * get formatter in appropriate locale
067      */  
068     HelloWorldTextFormatter fmt = new HelloWorldTextFormatter(lcl);
069     /*
070      * print the text in the current locale
071      */
072     System.out.println(fmt.helloWorld());
073     /*
074      * Alternatively,  text can be accessed and formatted manually. In this
075      * case you must obtain the Localizer class for the catalog. The Localizer
076      * class is  formed from the l10n_package attribute in the catalog, the
077      * catalog name, and the string "TextLocalizer".
078      */
079     Localizer l10n = L10nLookup.getLocalizer
080       (lcl,"examples.i18n.simple.HelloWorldTextLocalizer");
081     System.out.println(l10n.get("HELLO_AGAIN"));
082     /*
083      * If the message accepts arguments, then they can just be passed to the
084      * method defined for the message.
085      */
086     System.out.println(fmt.nthHello(3));
087     /*
088      * If using the manual method then you must manually apply the argument to
089      * the text using the MessageFormat class.
090      */
091     String text = l10n.get("NTH_HELLO");
092     Object[] argary = {new Integer(4)};
093     System.out.println(MessageFormat.format(text,argary));
094     /*
095      * The Localizer class also provides methods for accessing catalog information.
096      */
097     System.out.println(fmt.version(l10n.getVersion()));
098     System.out.println(fmt.l10nPackage(l10n.getL10nPackage()));
099     System.out.println(fmt.i18nPackage(l10n.getI18nPackage()));
100     System.out.println(fmt.subSystem(l10n.getSubSystem()));
101   }
102 }