| 1 30 31 35 package com.nightlabs.i18n; 36 37 import java.io.Serializable ; 38 import java.util.Collection ; 39 import java.util.Locale ; 40 import java.util.Map ; 41 import java.util.Set ; 42 43 51 public abstract class I18nText 52 implements Serializable , Localizable 53 { 54 55 public static final String DEFAULT_LANGUAGEID = Locale.ENGLISH.getLanguage(); 56 61 protected abstract Map getI18nMap(); 62 67 protected abstract void setText(String localizedValue); 68 73 public abstract String getText(); 74 80 protected abstract String getFallBackValue(String languageID); 81 82 86 private Map getEnsuredMap() { 87 Map result = getI18nMap(); 88 if (result == null) 89 throw new IllegalStateException ("Subclasses of I18nText must not return null in implementations of getI18nMap()!"); 90 return result; 91 } 92 93 101 public void localize(String languageID) 102 { 103 localize(languageID, this); 104 } 105 106 114 public void localize(String languageID, I18nText livingInstance) 115 { 116 Map map = livingInstance.getEnsuredMap(); 117 String text = (String ) map.get(languageID); 118 if (text == null) 119 text = (String ) map.get(DEFAULT_LANGUAGEID); 120 if (text == null && !map.isEmpty()) 121 text = (String ) map.values().iterator().next(); 122 if (text == null) 123 text = livingInstance.getFallBackValue(languageID); 124 setText(text); 125 } 126 127 137 public String getText(String languageID) 138 { 139 Map map = getEnsuredMap(); 140 String result = (String )map.get(languageID); 141 if (result == null) 142 result = (String )map.get(DEFAULT_LANGUAGEID); 143 if (result == null && !map.isEmpty()) 144 result = (String )map.values().iterator().next(); 145 if (result == null) 146 result = getFallBackValue(languageID); 147 return result; 148 } 149 150 155 public void setText(String languageID, String text) { 156 if ("".equals(text) || text == null) 157 getEnsuredMap().remove(languageID); 158 else 159 getEnsuredMap().put(languageID, text); 160 } 161 162 165 public boolean isEmpty() { 166 return getEnsuredMap().isEmpty(); 167 } 168 169 173 public Set getTexts() 174 { 175 return getEnsuredMap().entrySet(); 176 } 177 178 182 public Set getLanguageIDs() { 183 return getEnsuredMap().keySet(); 184 } 185 } 186 | Popular Tags |