Internationalization Guide

 Previous Next Contents Index View as PDF  

TextFormatter Class Reference for BEA WebLogic Server

The following sections provide reference information for TextFormatter classes:

Note: This information on TextFormatter classes is provided as reference of methods for normal usage. Normally, users will not need to use these interfaces directly.

 


About TextFormatter Classes

TextFormatter classes are generated by i18ngen from simple message catalogs. These classes provide methods for generating localized versions of message text at runtime. The following section shows an example of an application, its simple message catalog, and the TextFormatter class generated for the catalog.

 


Example of an Application Using a TextFormatter Class

Listing 4-3 shows an example of a simple message catalog for the HellowWorld application.

Listing 4-3 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/wls600/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>

Listing 4-4 shows an example of an application using the HelloWorld catalog.

Listing 4-4 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".
*
* Normally, one would use the default constructor to obtain
* formatting in the current locale. In this example we'll use 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, then they can just be passed to the
* method defined for the message.
*/
System.out.println(fmt.nthHello(3));
/*
* If using the manual method then 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()));
}
}

Listing 4-5 shows an example of the generated TextFormatter for the HelloWorld catalog.

Listing 4-5 Example of Generated TextFormatter Class for the HelloWorld Catalog

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE message_catalog PUBLIC "weblogic-message-catalog-dtd" "http://www.bea.com/servers/wls600/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>

 

Back to Top Previous Next