1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.luni.util; 19 20 21import java.util.Locale; 22import java.util.MissingResourceException; 23import java.util.ResourceBundle; 24// BEGIN android-added 25import java.lang.ref.WeakReference; 26import java.util.logging.Logger; 27// END android-added 28 29import org.apache.harmony.luni.util.MsgHelp; 30 31/** 32 * This class retrieves strings from a resource bundle and returns them, 33 * formatting them with MessageFormat when required. 34 * <p> 35 * It is used by the system classes to provide national language support, by 36 * looking up messages in the <code> 37 * org.apache.harmony.luni.util.ExternalMessages 38 * </code> 39 * resource bundle. Note that if this file is not available, or an invalid key 40 * is looked up, or resource bundle support is not available, the key itself 41 * will be returned as the associated message. This means that the <em>KEY</em> 42 * should a reasonable human-readable (English) string. 43 * 44 */ 45public class Msg { 46 // BEGIN android-changed 47 private static final String sResource = 48 "org.apache.harmony.luni.util.ExternalMessages"; 49 // END android-changed 50 51 /** 52 * Retrieves a message which has no arguments. 53 * 54 * @param msg 55 * String the key to look up. 56 * @return String the message for that key in the system message bundle. 57 */ 58 static public String getString(String msg) { 59 // BEGIN android-changed 60 ResourceBundle bundle = MsgHelp.loadBundle(sResource); 61 if (bundle == null) { 62 return msg; 63 } 64 // END android-changed 65 try { 66 return bundle.getString(msg); 67 } catch (MissingResourceException e) { 68 return msg; 69 } 70 } 71 72 /** 73 * Retrieves a message which takes 1 argument. 74 * 75 * @param msg 76 * String the key to look up. 77 * @param arg 78 * Object the object to insert in the formatted output. 79 * @return String the message for that key in the system message bundle. 80 */ 81 static public String getString(String msg, Object arg) { 82 return getString(msg, new Object[] { arg }); 83 } 84 85 /** 86 * Retrieves a message which takes 1 integer argument. 87 * 88 * @param msg 89 * String the key to look up. 90 * @param arg 91 * int the integer to insert in the formatted output. 92 * @return String the message for that key in the system message bundle. 93 */ 94 static public String getString(String msg, int arg) { 95 return getString(msg, new Object[] { Integer.toString(arg) }); 96 } 97 98 /** 99 * 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 // BEGIN android-added 138 ResourceBundle bundle = MsgHelp.loadBundle(sResource); 139 // END android-added 140 if (bundle != null) { 141 try { 142 format = bundle.getString(msg); 143 } catch (MissingResourceException e) { 144 } 145 } 146 147 return MsgHelp.format(format, args); 148 } 149} 150