01: /* *****************************************************************************
02: * LaszloMessages.java
03: * ****************************************************************************/
04:
05: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
06: * Copyright 2001-2006 Laszlo Systems, Inc. All Rights Reserved. *
07: * Use is subject to license terms. *
08: * J_LZ_COPYRIGHT_END *********************************************************/
09:
10: package org.openlaszlo.i18n;
11:
12: import java.text.MessageFormat;
13: import java.util.Locale;
14: import java.util.MissingResourceException;
15: import java.util.ResourceBundle;
16: import org.apache.log4j.Logger;
17: import org.openlaszlo.server.LPS;
18:
19: public class LaszloMessages {
20: private static Locale LOCALE;
21: private static ResourceBundle RESOURCE_BUNDLE;
22: private static final String BUNDLE_NAME = "org.openlaszlo.i18n.laszlomessages";
23: private static Logger mLogger = Logger
24: .getLogger(LaszloMessages.class);
25:
26: /* initialize static variables */
27: static {
28: LaszloMessages.setLocale(LPS.getLocale()); /* call added new LPS method */
29: mLogger.debug("LPS mLocale=" + LOCALE.getDisplayName());
30: mLogger.debug("Resource locale="
31: + RESOURCE_BUNDLE.getLocale().getDisplayName());
32: }
33:
34: private LaszloMessages() {
35: }
36:
37: public static void setLocale(Locale locale) {
38: LOCALE = locale;
39: RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME, LOCALE);
40: }
41:
42: public static String getMessage(String packageName, String key) {
43: return getMessage(packageName, key, new Object[] {});
44: }
45:
46: public static String getMessage(String packageName, String key,
47: Object[] args) {
48: return getValue(packageName + "." + key, args);
49: }
50:
51: public static String getValue(String fullKey) {
52: return getValue(fullKey, new Object[] {});
53: }
54:
55: public static String getValue(String fullKey, Object[] args) {
56: try {
57: //mLogger.debug(BUNDLE_NAME+".key="+fullKey);
58: if (args.length == 0) {
59: //mLogger.debug("value="+RESOURCE_BUNDLE.getString(fullKey));
60: return RESOURCE_BUNDLE.getString(fullKey);
61: } else {
62: String format = RESOURCE_BUNDLE.getString(fullKey);
63: //mLogger.debug(MessageFormat.format(format, args));
64: return MessageFormat.format(format, args);
65: }
66: } catch (MissingResourceException e) {
67: String mes = "can't find key=" + fullKey + " of '"
68: + BUNDLE_NAME + " " + getLocaleString()
69: + ".properties'";
70: System.err.println(mes);
71: return mes;
72: }
73: }
74:
75: public static String getLocaleString() {
76: String lang = RESOURCE_BUNDLE.getLocale().getLanguage();
77: if (lang == "")
78: return "";
79: else
80: return "_" + lang;
81: }
82: }
|