Frames | No Frames |
1: /* LocaleHelper.java -- helper routines for localization 2: Copyright (C) 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.java.locale; 40: 41: import java.util.Locale; 42: import java.util.MissingResourceException; 43: import java.util.ResourceBundle; 44: 45: /** 46: * This class provides common helper methods 47: * for handling localized data. 48: * 49: * @author Andrew John Hughes (gnu_andrew@member.fsf.org) 50: * 51: * @see java.util.Locale 52: * @see java.util.ResourceBundle 53: */ 54: public class LocaleHelper 55: { 56: /** 57: * This method is used by the localized name lookup methods to retrieve 58: * the localized name of a particular piece of locale data. 59: * If the display name can not be localized to the supplied 60: * locale, it will fall back on other output in the following order: 61: * 62: * <ul> 63: * <li>the localized name in the default locale</li> 64: * <li>the localized name in English (optional)</li> 65: * <li>the localized name in the root locale bundle (optional)</li> 66: * <li>the localized input string</li> 67: * </ul> 68: * <p> 69: * If the supplied key is merely the empty string, then the empty string is 70: * returned. 71: * </p> 72: * 73: * @param inLocale the locale to use for formatting the display string. 74: * @param key the locale data used as a key to the localized lookup tables. 75: * @param name the name of the hashtable containing the localized data. 76: * @param checkEnglish true if the method should fall back on data 77: * from the English locale. 78: * @param checkRoot true if the method should fall back on data from the 79: * unlocalized root locale. 80: * @return a <code>String</code>, hopefully containing the localized 81: * variant of the input data. 82: * @throws NullPointerException if <code>inLocale</code> is null. 83: */ 84: public static String getLocalizedString(Locale inLocale, String key, 85: String name, boolean checkEnglish, 86: boolean checkRoot) 87: { 88: String localizedString; 89: String property; 90: 91: if (key.equals("")) 92: return ""; 93: property = name + "." + key; 94: /* Localize to inLocale */ 95: try 96: { 97: localizedString = 98: ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", 99: inLocale).getString(property); 100: } 101: catch (MissingResourceException exception) 102: { 103: localizedString = null; 104: } 105: /* Localize to default locale */ 106: if (localizedString == null) 107: { 108: try 109: { 110: ResourceBundle bundle; 111: 112: bundle = 113: ResourceBundle.getBundle("gnu.java.locale.LocaleInformation"); 114: localizedString = bundle.getString(property); 115: } 116: catch (MissingResourceException exception) 117: { 118: localizedString = null; 119: } 120: } 121: /* Localize to English */ 122: if (localizedString == null && checkEnglish) 123: { 124: try 125: { 126: localizedString = 127: ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", 128: Locale.ENGLISH).getString(property); 129: } 130: catch (MissingResourceException exception) 131: { 132: localizedString = null; 133: } 134: } 135: /* Return unlocalized version */ 136: if (localizedString == null && checkRoot) 137: { 138: try 139: { 140: localizedString = 141: ResourceBundle.getBundle("gnu.java.locale.LocaleInformation", 142: new Locale("","","") 143: ).getString(property); 144: } 145: catch (MissingResourceException exception) 146: { 147: localizedString = null; 148: } 149: } 150: /* Return original input string */ 151: if (localizedString == null) 152: { 153: localizedString = key; 154: } 155: return localizedString; 156: } 157: }