Skip navigation.
Downloads | Product Documentation | Support

Using WebLogic Logging Services for Application Logging

# TextFormatter Class Reference for WebLogic Server

The following sections provide reference information for `TextFormatter` classes:

- About TextFormatter Classes
- Example of an Application Using a TextFormatter Class

# About TextFormatter Classes

`TextFormatter` classes are generated by `weblogic.i18ngen` from simple message catalogs. These classes provide methods for generating localized versions of message text at runtime.

# Example of an Application Using a TextFormatter Class

The following is an example of a `Hello_World` application, its simple message catalog, and the `TextFormatter` class generated for the catalog.

Listing C-1 Example of a Simple Message Catalog

>     <?xml version="1.0" encoding="UTF-8"?>
>     <!DOCTYPE message_catalog PUBLIC "weblogic-message-catalog-dtd" "http://www.bea.com/servers/wls90/dtd/msgcat.dtd">
>     <message_catalog
>        l10n_package="examples.i18n.simple"
>        subsystem="I18N"
>        version="1.0"
>        >
>        <message
>           messageid="HELLO_WORLD"
>           datelastchanged="967575717875"
>           method="helloWorld()"
>           >
>           <messagebody>
>              Hello World!
>           </messagebody>
>        </message>
>        <!--  -->
>        <message
>           messageid="HELLO_AGAIN"
>           datelastchanged="967575717804"
>           method="helloAgain()"
>           >
>           <messagebody>
>              Hello again
>           </messagebody>
>        </message>
>        <!--  -->
>        <message
>           messageid="NTH_HELLO"
>           datelastchanged="967575770971"
>           method="nthHello(int count)"
>           >
>           <messagebody>
>              This is hello number {0,number}.
>           </messagebody>
>        </message>
>        <!--  -->
>        <message
>           messageid="VERSION"
>           datelastchanged="967578656214"
>           method="version(String version)"
>           >
>           <messagebody>
>              Catalog version: {0}
>           </messagebody>
>        </message>
>        <!--  -->
>        <message
>           messageid="I18N_PACKAGE"
>           datelastchanged="967578691394"
>           method="i18nPackage(String pkg)"
>           >
>           <messagebody>
>              I18n Package: {0}
>           </messagebody>
>        </message>
>        <!--  -->
>        <message
>           messageid="L10N_PACKAGE"
>           datelastchanged="967578720156"
>           method="l10nPackage(String pkg)"
>           >
>           <messagebody>
>              L10n Package: {0}
>           </messagebody>
>        </message>
>        <!--  -->
>        <message
>           messageid="SUBSYSTEM"
>           datelastchanged="967578755587"
>           method="subSystem(String sub)"
>           >
>           <messagebody>
>              Catalog subsystem: {0}
>           </messagebody>
>        </message>
>     </message_catalog>

The following is an example of an application using the HelloWorld catalog. The example shows various ways of internationalizing an application using simple message catalogs.

Listing C-2 Example of an Application Using the HelloWorld Catalog

>     package examples.i18n.simple;
>     import java.util.Locale;
>     import java.text.MessageFormat;
>     import weblogic.i18n.Localizer;
>     import weblogic.i18ntools.L10nLookup;

>     /**
>      * @author Copyright (c) 2000 by BEA Systems, Inc. All Rights Reserved.
>      */

>     /**
>      * This example shows various ways of internationalizing an application
>      * using simple message catalogs.
>      * <p>
>      * Usage: java examples.i18n.simple.HelloWorld [lang [country]]
>      * <p>
>      * lang is a 2 character ISO language code. e.g. "en"
>      * country is a 2 character ISO country code. e.g. "US"
>      * <p>
>      * Usage of any of the languages supported by this example presumes
>      * the existence of the appropriate OS localization software and character
>      * encodings.
>      * <p>
>      * The example comes with catalogs for English (the default) and  French.
>      * The catalog source is in the following files, and were built
>      * using the catalog editing utility, weblogic.i18ntools.gui.MessageEditor.
>      * <p>
>      * <pre>
>      * English(base language)       ../msgcat/Helloworld.xml
>      * French                       ../msgcat/fr/FR/HelloWorld.xml
>      * </pre>
>      * <p>
>      * To build this example run the bld.sh(UNIX) or bld.cmd (NT) scripts from
>      * the examples/i18n/simple directory. CLIENT_CLASSES must be set up and
>      * needs to be in the classpath when running the example.
>      */

>     public final class HelloWorld {

>       public static void main(String[] argv) {
>         /*
>          * The easiest method for displaying localized text is to
>          * instantiate the generated formatter class for the HelloWorld catalog.
>          * This class contains convenience methods that return localized text for
>          * each message defined in the catalog.  The class name is
>          * the catalog name followed by "TextFormatter".
>          *
>          * Typically, you would use the default constructor to obtain
>          * formatting in the current locale. This example uses a locale
>          * based on arguments to construct the TextFormatter.
>          */
>         Locale lcl;
>         if (argv.length == 0) { // default is default locale for JVM
>           lcl = Locale.getDefault();
>         }
>         else {
>           String lang = null;
>           String country = null;
>           //get  the language code
>           lang = argv[0];
>           if (argv.length >= 2) { // get the country code
>             country = argv[1];
>           }
>           lcl = new Locale(lang,country);
>         }
>         /*
>          * Get formatter in appropriate locale.
>          */
>         HelloWorldTextFormatter fmt = new HelloWorldTextFormatter(lcl);
>         fmt.setExtendedFormat(true);
>         /*
>          * Print the text in the current locale.
>          */
>         System.out.println(fmt.helloWorld());

>         /*
>          * Alternatively,  text can be accessed and formatted manually. In this
>          * case you must obtain the Localizer class for the catalog. The
>          * Localizer class is formed from the l10n_package attribute in the
>          * catalog, the catalog name, and the string "TextLocalizer".
>          */
>         Localizer l10n = L10nLookup.getLocalizer
>           (lcl,"examples.i18n.simple.HelloWorldTextLocalizer");
>         System.out.println(l10n.get("HELLO_AGAIN"));
>         /*
>          * If the message accepts arguments, they can be passed to the
>          * method defined for the message.
>          */
>         System.out.println(fmt.nthHello(3));
>         /*
>          * If using the manual method, you must manually apply the argument to
>          * the text using the MessageFormat class.
>          */
>         String text = l10n.get("NTH_HELLO");
>         Object[] args = {new Integer(4)};
>         System.out.println(MessageFormat.format(text,args));
>         /*
>          * The Localizer class also provides methods for accessing catalog
>          * information.
>          */
>         System.out.println(fmt.version(l10n.getVersion()));
>         System.out.println(fmt.l10nPackage(l10n.getL10nPackage()));
>         System.out.println(fmt.i18nPackage(l10n.getI18nPackage()));
>         System.out.println(fmt.subSystem(l10n.getSubSystem()));
>       }
>     }

The following listing shows an example of the generated `TextFormatter` for the HelloWorld catalog.

Listing C-3 Example of Generated TextFormatter Class for the HelloWorld Catalog

>     package examples.i18n.simple;import java.text.MessageFormat;
>     import java.text.DateFormat;
>     import java.util.Date;
>     import java.util.Locale;
>     import weblogic.i18n.Localizer;
>     import weblogic.i18ntools.L10nLookup;

>     /**
>      * Copyright (c) 2002 by BEA Inc. All Rights Reserved.
>      * @exclude
>      */

>     public class HelloWorldTextFormatter {
>        private Localizer l10n;
>        private boolean format=false;
>        // constructors
>        public HelloWorldTextFormatter() {
>        l10n = L10nLookup.getLocalizer(Locale.getDefault(),     "examples.i18n.simple.HelloWorldTextLocalizer");
>        }
>        public HelloWorldTextFormatter(Locale l) {
>        l10n =    L10nLookup.getLocalizer(l,"examples.i18n.simple.HelloWorldTextLocalizer");
>        }
>        public static HelloWorldTextFormatter getInstance() {
>           return new HelloWorldTextFormatter();
>        }
>        public static HelloWorldTextFormatter getInstance(Locale l) {
>           return new HelloWorldTextFormatter(l);
>        }
>        public void setExtendedFormat(boolean fmt) {
>           format = fmt;
>        }
>        public boolean getExtendedFormat() { return format;

>       /**
>        * Hello World!
>        */

>        public String helloWorld()  {
>           String fmt  = "";
>           String id = "HELLO_WORLD" ;
>           String subsystem = "I18N" ;
>           Object [] args = {  };
>           String output =  MessageFormat.format(l10n.get(id) , args);
>           if (getExtendedFormat()) {
>           DateFormat dformat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,       DateFormat.LONG);
>           fmt = "<"+dformat.format(new Date())+"><"+subsystem+"><"+id+"> ";
>           }
>           return fmt+output;
>           }

>       /**
>        * Hello again
>        */

>        public String helloAgain()  {
>           String fmt  = "";
>           String id = "HELLO_AGAIN" ;
>           String subsystem = "I18N" ;
>           Object [] args = {  };
>           String output =  MessageFormat.format(l10n.get(id) , args);
>           if (getExtendedFormat()) {
>              DateFormat dformat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,         DateFormat.LONG);
>              fmt = "<"+dformat.format(new Date())+"><"+subsystem+"><"+id+">";
>           }
>           return fmt+output;
>           }

>      /**
>       * This is hello number {0,number}.
>       */

>        public String nthHello(int arg0)  {
>           String fmt  = "";
>           String id = "NTH_HELLO" ;
>           String subsystem = "I18N" ;
>           Object [] args = { new Integer(arg0) };
>           String output =  MessageFormat.format(l10n.get(id) , args);
>           if (getExtendedFormat()) {
>              DateFormat dformat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,          DateFormat.LONG);
>              fmt = "<"+dformat.format(new Date())+"><"+subsystem+"><"+id+">";
>           }
>           return fmt+output;
>           }

>      /**
>       * Catalog version: {0}
>       */

>        public String version(String arg0)  {
>           String fmt  = "";
>           String id = "VERSION" ;
>           String subsystem = "I18N" ;
>           Object [] args = { arg0 };
>           String output =  MessageFormat.format(l10n.get(id) , args);
>           if (getExtendedFormat()) {
>              DateFormat dformat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,          DateFormat.LONG);
>              fmt = "<"+dformat.format(new Date())+"><"+subsystem+"><"+id+">";
>           }
>           return fmt+output;
>           }

>      /**
>       * I18n Package: {0}
>       */

>        public String i18nPackage(String arg0)  {
>           String fmt  = "";
>           String id = "I18N_PACKAGE" ;
>           String subsystem = "I18N" ;
>           Object [] args = { arg0 };
>           String output =  MessageFormat.format(l10n.get(id) , args);
>           if (getExtendedFormat()) {
>              DateFormat dformat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,          DateFormat.LONG);
>              fmt = "<"+dformat.format(new Date())+"><"+subsystem+"><"+id+">";
>           }
>           return fmt+output;
>           }

>      /**
>       * L10n Package: {0}
>       */

>        public String l10nPackage(String arg0)  {
>           String fmt  = "";
>           String id = "L10N_PACKAGE" ;
>           String subsystem = "I18N" ;
>           Object [] args = { arg0 };
>           String output =  MessageFormat.format(l10n.get(id) , args);
>           if (getExtendedFormat()) {
>              DateFormat dformat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,          DateFormat.LONG);
>              fmt = "<"+dformat.format(new Date())+"><"+subsystem+"><"+id+">";
>           }
>           return fmt+output;
>           }

>      /**
>       * Catalog subsystem: {0}
>       */

>        public String subSystem(String arg0)  {
>           String fmt  = "";
>           String id = "SUBSYSTEM" ;
>           String subsystem = "I18N" ;
>           Object [] args = { arg0 };
>           String output =  MessageFormat.format(l10n.get(id) , args);
>           if (getExtendedFormat()) {
>              DateFormat dformat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,          DateFormat.LONG);
>               fmt = "<"+dformat.format(new Date())+"><"+subsystem+"><"+id+">";
>           }
>           return fmt+output;
>           }
>     }