Archive for November 24th, 2007

Java Internationalization Reference

November 24th, 2007 | Category: Programming

ResourceBundle and MessageFormat

try {

	Locale locale = getLocale();
	String rbName = getBundleName();
	ResourceBundle bundle = ResourceBundle.getBundle(rbName, locale);
	String retMessage = bundle.getString( key );

	if ( varargs.length > 0 )
	{
		MessageFormat mf = new MessageFormat( retMessage, locale );
		retMessage = mf.format( varargs );

		//TESTING: Writing Java Unicode to UTF-8

		BufferedWriter bw = new BufferedWriter( new OutputStreamWriter (new FileOutputStream(filename),"UTF-8"));
		bw.write(retMessage);
		bw.close();
	}

}  catch ( Exception e )   {

	e.printStackTrace();

}

References:

I18N Messages and Logging

Provides an API that allows your Java applications to generate and log internationalized messages along with creating localized exceptions. Also provides Java annotations and an ANT task to help automatically generate language resource bundles.

http://sourceforge.net/project/showfiles.php?group_id=169460

Java internationalization with the Properties Pre-Processor

An XML-based utility for handling localized strings

http://www.javaworld.com/javaworld/jw-08-2007/jw-08-i18nproperties.html?page=4#resources

Sun Java Tutorials:

http://java.sun.com/docs/books/tutorial/information/download.html

DateFormat:

import java.util.*;
import java.text.*;
static public void showBothStyles(Locale currentLocale) {

      Date today;
      String result;
      DateFormat formatter;

      int[] styles = {
         DateFormat.DEFAULT,
         DateFormat.SHORT,
         DateFormat.MEDIUM,
         DateFormat.LONG,
         DateFormat.FULL
      };

      System.out.println();
      System.out.println("Locale: " + currentLocale.toString());
      System.out.println();

      today = new Date();

      for (int k = 0; k < styles.length; k++) {
         formatter = DateFormat.getDateTimeInstance(
                     styles[k], styles[k], currentLocale);
         result = formatter.format(today);
         System.out.println(result);
      }
   }

DecimalFormat:


 static public void localizedFormat(String pattern, double value,
                                      Locale loc ) {
      NumberFormat nf = NumberFormat.getNumberInstance(loc);
      DecimalFormat df = (DecimalFormat)nf;
      df.applyPattern(pattern);
      String output = df.format(value);
      System.out.println(pattern + "  " + output + "  " + loc.toString());
   }

NumberFormat:

  static public void displayNumber(Locale currentLocale) {   

      Integer quantity = new Integer(123456);

      Double amount = new Double(345987.246);

      NumberFormat numberFormatter;

      String quantityOut;

      String amountOut;

numberFormatter = NumberFormat.getNumberInstance(currentLocale);

      quantityOut = numberFormatter.format(quantity);

      amountOut = numberFormatter.format(amount);

      System.out.println(quantityOut + "   " + currentLocale.toString());

      System.out.println(amountOut + "   " + currentLocale.toString());

   }

static public void displayCurrency(Locale currentLocale) {

Double currency = new Double(9876543.21);

      NumberFormat currencyFormatter;

      String currencyOut;

currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);

      currencyOut = currencyFormatter.format(currency);

      System.out.println(currencyOut + "   " + currentLocale.toString());

   }

static public void displayPercent(Locale currentLocale) {

Double percent = new Double(0.75);

      NumberFormat percentFormatter;

      String percentOut;

percentFormatter = NumberFormat.getPercentInstance(currentLocale);

      percentOut = percentFormatter.format(percent);

      System.out.println(percentOut + "   " + currentLocale.toString());

   }

Simple MessageFormat:


static  void displayMessage(Locale currentLocale) {

      System.out.println("currentLocale = " + currentLocale.toString());
      System.out.println();

      ResourceBundle messages =
         ResourceBundle.getBundle("MessageBundle",currentLocale);

      Object[] messageArguments = {
         messages.getString("planet"),
         new Integer(7),
         new Date()
      };

      MessageFormat formatter = new MessageFormat("");
      formatter.setLocale(currentLocale);

      formatter.applyPattern(messages.getString("template"));
      String output = formatter.format(messageArguments);

      System.out.println(output);

   }

References:
http://www.jorendorff.com/articles/unicode/index.html
 
 
No comments