1 package org.djunits.locale;
2
3 import java.util.Locale;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
6
7
8
9
10
11
12
13
14
15
16
17 public class Localization
18 {
19
20 private final String bundleNamePrefix;
21
22
23 private ResourceBundle resourceBundle;
24
25
26 private Locale currentLocale = null;
27
28
29
30
31
32 public Localization(final String prefix)
33 {
34 this.bundleNamePrefix = prefix;
35 }
36
37
38
39
40
41
42
43 public final String getString(final String key)
44 {
45 if (this.currentLocale == null || !this.currentLocale.equals(DefaultLocale.getLocale()))
46 {
47 if (DefaultLocale.getLocale() == null)
48 {
49 DefaultLocale.setLocale(new Locale("en"));
50 }
51 this.currentLocale = DefaultLocale.getLocale();
52 Locale.setDefault(this.currentLocale);
53 try
54 {
55 this.resourceBundle = ResourceBundle.getBundle(this.bundleNamePrefix, this.currentLocale);
56 }
57 catch (MissingResourceException e)
58 {
59 try
60 {
61 this.resourceBundle = ResourceBundle.getBundle("resources/" + this.bundleNamePrefix, this.currentLocale);
62 }
63 catch (MissingResourceException e2)
64 {
65 return '!' + key.substring(key.indexOf('.') + 1) + '!';
66 }
67 }
68 }
69 if (null == this.resourceBundle)
70 {
71
72 return '!' + key.substring(key.indexOf('.') + 1) + '!';
73 }
74 try
75 {
76 return this.resourceBundle.getString(key);
77 }
78 catch (MissingResourceException e)
79 {
80 return '!' + key.substring(key.indexOf('.') + 1) + '!';
81 }
82 }
83 }