1 28 29 package com.caucho.util; 30 31 import com.caucho.log.Log; 32 33 import java.io.InputStream ; 34 import java.util.HashMap ; 35 import java.util.Locale ; 36 import java.util.logging.Level ; 37 import java.util.logging.Logger ; 38 39 42 public class L10N { 43 private static Logger _log; 44 45 static HashMap <String ,HashMap <String ,String >> l10nMap 46 = new HashMap <String ,HashMap <String ,String >>(); 47 48 HashMap <String ,String > messages; 49 50 public L10N(Class cl) 51 { 52 String name = cl.getName().replace('.', '/'); 53 int p = name.lastIndexOf('/'); 54 55 if (p > 0) 56 init(name.substring(0, p) + "/messages"); 57 else 58 init("/messages"); 59 } 60 66 67 private void init(String path) 68 { 69 if (! path.startsWith("/")) 70 path = "/" + path; 71 72 messages = l10nMap.get(path); 73 74 InputStream is = null; 75 Locale locale = Locale.getDefault(); 76 77 try { 78 String language = locale.getLanguage(); 79 80 String xmlName = path + "_" + language + ".xml"; 81 82 116 } catch (Exception e) { 117 log().log(Level.FINE, e.toString(), e); 118 } 119 } 120 121 public String l(String msg) 122 { 123 msg = getTranslated(msg); 124 125 return msg; 126 } 127 128 public String l(String msg, long l) 129 { 130 return l(msg, String.valueOf(l)); 131 } 132 133 public String l(String msg, Object o) 134 { 135 msg = getTranslated(msg); 136 137 CharBuffer cb = CharBuffer.allocate(); 138 139 int length = msg.length(); 140 int i = 0; 141 142 while (i < length) { 143 char ch = msg.charAt(i); 144 145 if (ch != '{' || i + 2 >= length) { 146 cb.append(ch); 147 i++; 148 } 149 else { 150 ch = msg.charAt(i + 1); 151 152 if (ch == '{') { 153 cb.append('{'); 154 i += 2; 155 } 156 else if (msg.charAt(i + 2) != '}') { 157 cb.append('{'); 158 i++; 159 } 160 else if (ch == '0') { 161 cb.append(o); 162 i += 3; 163 } 164 else { 165 cb.append('{'); 166 i++; 167 } 168 } 169 } 170 171 return cb.close(); 172 } 173 174 public String l(String msg, Object o1, Object o2) 175 { 176 msg = getTranslated(msg); 177 178 CharBuffer cb = CharBuffer.allocate(); 179 180 int length = msg.length(); 181 int i = 0; 182 183 while (i < length) { 184 char ch = msg.charAt(i); 185 186 if (ch != '{' || i + 2 >= length) { 187 cb.append(ch); 188 i++; 189 } 190 else { 191 ch = msg.charAt(i + 1); 192 193 if (ch == '{') { 194 cb.append('{'); 195 i += 2; 196 } 197 else if (msg.charAt(i + 2) != '}') { 198 cb.append('{'); 199 i++; 200 } 201 else if (ch == '0') { 202 cb.append(o1); 203 i += 3; 204 } 205 else if (ch == '1') { 206 cb.append(o2); 207 i += 3; 208 } 209 else { 210 cb.append('{'); 211 i++; 212 } 213 } 214 } 215 216 return cb.close(); 217 } 218 219 public String l(String msg, Object o1, int i2) 220 { 221 return l(msg, o1, String.valueOf(i2)); 222 } 223 224 public String l(String msg, int i1, int i2) 225 { 226 return l(msg, String.valueOf(i1), String.valueOf(i2)); 227 } 228 229 public String l(String msg, Object o1, Object o2, Object o3) 230 { 231 msg = getTranslated(msg); 232 233 CharBuffer cb = CharBuffer.allocate(); 234 235 int length = msg.length(); 236 int i = 0; 237 238 while (i < length) { 239 char ch = msg.charAt(i); 240 241 if (ch != '{' || i + 2 >= length) { 242 cb.append(ch); 243 i++; 244 } 245 else { 246 ch = msg.charAt(i + 1); 247 248 if (ch == '{') { 249 cb.append('{'); 250 i += 2; 251 } 252 else if (msg.charAt(i + 2) != '}') { 253 cb.append('{'); 254 i++; 255 } 256 else if (ch == '0') { 257 cb.append(o1); 258 i += 3; 259 } 260 else if (ch == '1') { 261 cb.append(o2); 262 i += 3; 263 } 264 else if (ch == '2') { 265 cb.append(o3); 266 i += 3; 267 } 268 else { 269 cb.append('{'); 270 i++; 271 } 272 } 273 } 274 275 return cb.close(); 276 } 277 278 public String l(String msg, Object o1, Object o2, Object o3, Object o4) 279 { 280 msg = getTranslated(msg); 281 282 CharBuffer cb = CharBuffer.allocate(); 283 284 int length = msg.length(); 285 int i = 0; 286 287 while (i < length) { 288 char ch = msg.charAt(i); 289 290 if (ch != '{' || i + 2 >= length) { 291 cb.append(ch); 292 i++; 293 } 294 else { 295 ch = msg.charAt(i + 1); 296 297 if (ch == '{') { 298 cb.append('{'); 299 i += 2; 300 } 301 else if (msg.charAt(i + 2) != '}') { 302 cb.append('{'); 303 i++; 304 } 305 else if (ch == '0') { 306 cb.append(o1); 307 i += 3; 308 } 309 else if (ch == '1') { 310 cb.append(o2); 311 i += 3; 312 } 313 else if (ch == '2') { 314 cb.append(o3); 315 i += 3; 316 } 317 else if (ch == '3') { 318 cb.append(o4); 319 i += 3; 320 } 321 else { 322 cb.append('{'); 323 i++; 324 } 325 } 326 } 327 328 return cb.close(); 329 } 330 331 public String l(String msg, Object o1, Object o2, 332 Object o3, Object o4, Object o5) 333 { 334 msg = getTranslated(msg); 335 336 CharBuffer cb = CharBuffer.allocate(); 337 338 int length = msg.length(); 339 int i = 0; 340 341 while (i < length) { 342 char ch = msg.charAt(i); 343 344 if (ch != '{' || i + 2 >= length) { 345 cb.append(ch); 346 i++; 347 } 348 else { 349 ch = msg.charAt(i + 1); 350 351 if (ch == '{') { 352 cb.append('{'); 353 i += 2; 354 } 355 else if (msg.charAt(i + 2) != '}') { 356 cb.append('{'); 357 i++; 358 } 359 else if (ch == '0') { 360 cb.append(o1); 361 i += 3; 362 } 363 else if (ch == '1') { 364 cb.append(o2); 365 i += 3; 366 } 367 else if (ch == '2') { 368 cb.append(o3); 369 i += 3; 370 } 371 else if (ch == '3') { 372 cb.append(o4); 373 i += 3; 374 } 375 else if (ch == '4') { 376 cb.append(o5); 377 i += 3; 378 } 379 else { 380 cb.append('{'); 381 i++; 382 } 383 } 384 } 385 386 return cb.close(); 387 } 388 389 private String getTranslated(String msg) 390 { 391 if (messages == null) 392 return msg; 393 394 String translated = (String ) messages.get(msg); 395 396 if (translated == null) { 397 return msg; 398 } 399 else 400 return translated; 401 } 402 403 private Logger log() 404 { 405 if (_log == null) 406 _log = Log.open(L10N.class); 407 408 return _log; 409 } 410 } 411 | Popular Tags |