001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.util.i18n;
006:
007: import java.text.MessageFormat;
008: import java.util.HashMap;
009: import java.util.Locale;
010: import java.util.Map;
011: import java.util.PropertyResourceBundle;
012: import java.util.ResourceBundle;
013: import java.util.StringTokenizer;
014:
015: public final class LocaleHelper {
016: private static final Map localeHelperList = new HashMap();
017:
018: private final String RESOURCE_BASE;
019: private ResourceBundle resourceBundle;
020:
021: public LocaleHelper(final String aBase) {
022: this (aBase, Locale.getDefault());
023: }//constructor
024:
025: public LocaleHelper(final String aBase, final Locale aLocale) {
026: RESOURCE_BASE = aBase;
027: setLocale(aLocale);
028: }//constructor
029:
030: public void setLocale(final Locale locale) {
031: resourceBundle = PropertyResourceBundle.getBundle(
032: RESOURCE_BASE, locale);
033: }//setLocale()
034:
035: public String getResourceBase() {
036: return RESOURCE_BASE;
037: }//getResourceBase()
038:
039: public String getLocalizedString(final String key) {
040: return resourceBundle.getString(key);
041: }//getLocalizedString()
042:
043: public String getLocalizedString(final String key,
044: final Object[] objs) {
045: if (objs != null && objs.length > 0) {
046: MessageFormat mf = new MessageFormat("");
047: mf.setLocale(resourceBundle.getLocale());
048: mf.applyPattern(resourceBundle.getString(key));
049: return mf.format(objs);
050: } else {
051: return resourceBundle.getString(key);
052: }
053: }//getLocalizedString()
054:
055: public void debug(final String key) {
056: debug(key, null);
057: }//debug()
058:
059: public void debug(final String key, final Object[] tokens) {
060: final String msg = getLocalizedString(key, tokens);
061: final Object[] toks = { msg };
062: System.out.println(getLocalizedString("msgDebug", toks));
063: }//debug()
064:
065: public void warning(final String key) {
066: warning(key, null);
067: }//warning()
068:
069: public void warning(final String key, final Object[] tokens) {
070: final String msg = getLocalizedString(key, tokens);
071: final Object[] toks = { msg };
072: System.out.println(getLocalizedString("msgWarning", toks));
073: }//warning()
074:
075: /**
076: * given a string representation of the locale (i.e. en_US)
077: * create a Locale obj.
078: */
079: public static Locale getLocale(final String stringformat) {
080: if (stringformat == null) {
081: return Locale.getDefault();
082: }
083:
084: StringTokenizer tk = new StringTokenizer(stringformat, "_");
085: String lang = "";
086: String country = "";
087: String variant = "";
088: if (tk.hasMoreTokens()) {
089: lang = tk.nextToken();
090: }
091:
092: if (tk.hasMoreTokens()) {
093: country = tk.nextToken();
094: }
095: if (tk.hasMoreTokens()) {
096: variant = tk.nextToken();
097: }
098:
099: return new Locale(lang, country, variant);
100: }//getLocale()
101:
102: public static void store(final String aLocaleBase,
103: final Locale aLocale) {
104: localeHelperList.put(aLocaleBase, new LocaleHelper(aLocaleBase,
105: aLocale));
106: }//storeHelper()
107:
108: public static LocaleHelper getLocaleHelper(final String aLocaleBase) {
109: return (LocaleHelper) localeHelperList.get(aLocaleBase);
110: }//getLocaleHelper()
111:
112: }//class LocaleHelper
|