1 18 package org.apache.batik.i18n; 19 20 import java.text.MessageFormat ; 21 import java.util.Locale ; 22 import java.util.ResourceBundle ; 23 24 77 public class LocalizableSupport implements Localizable { 78 81 protected LocaleGroup localeGroup = LocaleGroup.DEFAULT; 82 83 86 protected String bundleName; 87 88 91 protected ClassLoader classLoader; 92 93 96 protected Locale locale; 97 98 101 protected Locale usedLocale; 102 103 106 protected ResourceBundle resourceBundle; 107 108 111 public LocalizableSupport(String s) { 112 this(s, null); 113 } 114 115 125 public LocalizableSupport(String s, ClassLoader cl) { 126 bundleName = s; 127 classLoader = cl; 128 } 129 130 133 public void setLocale(Locale l) { 134 if (locale != l) { 135 locale = l; 136 resourceBundle = null; 137 } 138 } 139 140 143 public Locale getLocale() { 144 return locale; 145 } 146 147 151 public void setLocaleGroup(LocaleGroup lg) { 152 localeGroup = lg; 153 } 154 155 159 public LocaleGroup getLocaleGroup() { 160 return localeGroup; 161 } 162 163 169 public void setDefaultLocale(Locale l) { 170 localeGroup.setLocale(l); 171 } 172 173 177 public Locale getDefaultLocale() { 178 return localeGroup.getLocale(); 179 } 180 181 185 public String formatMessage(String key, Object [] args) { 186 getResourceBundle(); 187 return MessageFormat.format(resourceBundle.getString(key), args); 188 } 189 190 194 public ResourceBundle getResourceBundle() { 195 Locale l; 196 197 if (resourceBundle == null) { 198 if (locale == null) { 199 if ((l = localeGroup.getLocale()) == null) { 200 usedLocale = Locale.getDefault(); 201 } else { 202 usedLocale = l; 203 } 204 } else { 205 usedLocale = locale; 206 } 207 if (classLoader == null) { 208 resourceBundle = ResourceBundle.getBundle(bundleName, 209 usedLocale); 210 } else { 211 resourceBundle = ResourceBundle.getBundle(bundleName, 212 usedLocale, 213 classLoader); 214 } 215 } else if (locale == null) { 216 if ((l = localeGroup.getLocale()) == null) { 218 if (usedLocale != (l = Locale.getDefault())) { 219 usedLocale = l; 220 if (classLoader == null) { 221 resourceBundle = ResourceBundle.getBundle(bundleName, 222 usedLocale); 223 } else { 224 resourceBundle = ResourceBundle.getBundle(bundleName, 225 usedLocale, 226 classLoader); 227 } 228 } 229 } else if (usedLocale != l) { 230 usedLocale = l; 231 if (classLoader == null) { 232 resourceBundle = ResourceBundle.getBundle(bundleName, 233 usedLocale); 234 } else { 235 resourceBundle = ResourceBundle.getBundle(bundleName, 236 usedLocale, 237 classLoader); 238 } 239 } 240 } 241 242 return resourceBundle; 243 } 244 } 245 | Popular Tags |