1 /* 2 Copyright 2002-2006 Martin van den Bemt 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package org.xulux.utils; 17 18 import java.util.Iterator; 19 import java.util.List; 20 import java.util.MissingResourceException; 21 import java.util.ResourceBundle; 22 23 /** 24 * Retrievs the specified internationalized text 25 * @todo add translation fix method, so translations not 26 * yet present will get added to the property file 27 * 28 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a> 29 * @version $Id: Translator.java,v 1.1 2005/12/18 12:58:15 mvdb Exp $ 30 */ 31 public class Translator { 32 33 /** 34 * the translator instance 35 */ 36 private static Translator instance; 37 38 /** 39 * 40 */ 41 protected Translator() { 42 } 43 44 /** 45 * @return the instance of the translator 46 */ 47 protected static Translator getInstance() { 48 if (instance == null) { 49 instance = new Translator(); 50 } 51 return instance; 52 } 53 54 /** 55 * For now only support for full i18n (so the first 56 * entry in the key must be the percent sign 57 * to get translated. 58 * @todo Make it more flexible 59 * @todo Make it read comma delimited entries.. 60 * @param list the list of urls 61 * @param key the key to translate 62 * @return the key or the found value if 63 */ 64 public static String translate(List list, String key) { 65 boolean problem = false; 66 if (list != null && key != null && key.startsWith("%")) { 67 if (key.startsWith("%%")) { 68 // return the key with one less percent sign 69 return key.substring(1); 70 } 71 for (Iterator it = list.iterator(); it.hasNext();) { 72 try { 73 Translation translation = (Translation) it.next(); 74 ResourceBundle bundle = ResourceBundle.getBundle(translation.getUrl()); 75 return bundle.getString(key.substring(1)); 76 } catch (MissingResourceException mre) { 77 problem = true; 78 } 79 } 80 } 81 // if (log.isWarnEnabled() && problem) { 82 // log.warn("Resource " + key + " not found in " + list); 83 // } 84 return key; 85 } 86 87 }