001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.util;
019:
020: import java.util.Locale;
021: import java.util.MissingResourceException;
022: import java.util.ResourceBundle;
023:
024: import org.apache.harmony.luni.util.MsgHelp;
025:
026: /**
027: * This class retrieves strings from a resource bundle and returns them,
028: * formatting them with MessageFormat when required.
029: * <p>
030: * It is used by the system classes to provide national language support, by
031: * looking up messages in the <code>
032: * org.apache.harmony.luni.util.ExternalMessages
033: * </code>
034: * resource bundle. Note that if this file is not available, or an invalid key
035: * is looked up, or resource bundle support is not available, the key itself
036: * will be returned as the associated message. This means that the <em>KEY</em>
037: * should a reasonable human-readable (english) string.
038: *
039: */
040: public class Msg {
041:
042: // ResourceBundle holding the system messages.
043: static private ResourceBundle bundle = null;
044:
045: static {
046: // Attempt to load the messages.
047: try {
048: bundle = MsgHelp.setLocale(Locale.getDefault(),
049: "org.apache.harmony.luni.util.ExternalMessages");
050: } catch (Throwable e) {
051: e.printStackTrace();
052: }
053: }
054:
055: /**
056: * Retrieves a message which has no arguments.
057: *
058: * @param msg
059: * String the key to look up.
060: * @return String the message for that key in the system message bundle.
061: */
062: static public String getString(String msg) {
063: if (bundle == null)
064: return msg;
065: try {
066: return bundle.getString(msg);
067: } catch (MissingResourceException e) {
068: return msg;
069: }
070: }
071:
072: /**
073: * Retrieves a message which takes 1 argument.
074: *
075: * @param msg
076: * String the key to look up.
077: * @param arg
078: * Object the object to insert in the formatted output.
079: * @return String the message for that key in the system message bundle.
080: */
081: static public String getString(String msg, Object arg) {
082: return getString(msg, new Object[] { arg });
083: }
084:
085: /**
086: * Retrieves a message which takes 1 integer argument.
087: *
088: * @param msg
089: * String the key to look up.
090: * @param arg
091: * int the integer to insert in the formatted output.
092: * @return String the message for that key in the system message bundle.
093: */
094: static public String getString(String msg, int arg) {
095: return getString(msg, new Object[] { Integer.toString(arg) });
096: }
097:
098: /**
099: * Retrieves a message which takes 1 character argument.
100: *
101: * @param msg
102: * String the key to look up.
103: * @param arg
104: * char the character to insert in the formatted output.
105: * @return String the message for that key in the system message bundle.
106: */
107: static public String getString(String msg, char arg) {
108: return getString(msg, new Object[] { String.valueOf(arg) });
109: }
110:
111: /**
112: * Retrieves a message which takes 2 arguments.
113: *
114: * @param msg
115: * String the key to look up.
116: * @param arg1
117: * Object an object to insert in the formatted output.
118: * @param arg2
119: * Object another object to insert in the formatted output.
120: * @return String the message for that key in the system message bundle.
121: */
122: static public String getString(String msg, Object arg1, Object arg2) {
123: return getString(msg, new Object[] { arg1, arg2 });
124: }
125:
126: /**
127: * Retrieves a message which takes several arguments.
128: *
129: * @param msg
130: * String the key to look up.
131: * @param args
132: * Object[] the objects to insert in the formatted output.
133: * @return String the message for that key in the system message bundle.
134: */
135: static public String getString(String msg, Object[] args) {
136: String format = msg;
137:
138: if (bundle != null) {
139: try {
140: format = bundle.getString(msg);
141: } catch (MissingResourceException e) {
142: }
143: }
144:
145: return MsgHelp.format(format, args);
146: }
147: }
|