/************************************************************************** /* This class demonstrates the use of the i18n-API. /* /* Copyright (c) 2004 by Bernhard Bablok (mail@bablokb.de) /* /* This program is free software; you can redistribute it and/or modify /* it under the terms of the GNU General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This program is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; see the file COPYING. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ import java.util.*; import java.text.*; /** This class demonstrates the use of the i18n-API. @version $Revision: 1.4 $ @author $Author: bablokb $ */ public class I18nBsp{ private Locale iLocale; private ResourceBundle iBundle; ////////////////////////////////////////////////////////////////////////////// /** Default constructor. Sets the default locale. */ public I18nBsp() { setLocale(Locale.getDefault()); } ////////////////////////////////////////////////////////////////////////////// /** Set the locale for the object. */ private void setLocale(Locale locale) { iLocale = locale; System.out.println("\n--- Locale: " + iLocale.toString() + "---"); iBundle = ResourceBundle.getBundle("I18nBspMsgs",iLocale); } ////////////////////////////////////////////////////////////////////////////// /** Run samples two times: with default and with specific locale. */ public static void main(String[] args) { I18nBsp bsp = new I18nBsp(); bsp.printStrings(); bsp.printDate(); bsp.printMessage(); bsp.printCurrency(); bsp.setLocale(new Locale("en","US")); bsp.printStrings(); bsp.printDate(); bsp.printMessage(); bsp.printCurrency(); } ////////////////////////////////////////////////////////////////////////////// /** Print constant strings. */ private void printStrings() { System.out.println("\tString: " + iBundle.getString("hello")); } ////////////////////////////////////////////////////////////////////////////// /** Print date. */ private void printDate() { Date now = new Date(); DateFormat df = DateFormat. getDateInstance(DateFormat.DEFAULT,iLocale); System.out.println("\tDate: " + df.format(now)); } ////////////////////////////////////////////////////////////////////////////// /** Print a message. */ private void printMessage() { String template = iBundle.getString("dirInfo"); MessageFormat mf = new MessageFormat(""); mf.setLocale(iLocale); mf.applyPattern(template); System.out.println("\tMessage: " + mf.format(new Object[] {new Integer(5)})); } ////////////////////////////////////////////////////////////////////////////// /** Print a currency value. */ private void printCurrency() { Double value = new Double(12345678.99); NumberFormat cf = NumberFormat. getCurrencyInstance(iLocale); System.out.println("\tCurrency: " + cf.format(value)); } }