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
18 public class Localization
19 {
20
21 private final String bundleNamePrefix;
22
23
24 private ResourceBundle resourceBundle;
25
26
27 private Locale currentLocale = null;
28
29
30 private ResourceBundle defaultResourceBundle;
31
32
33 private Locale defaultLocale = null;
34
35
36
37
38
39 public Localization(final String prefix)
40 {
41 this.bundleNamePrefix = prefix;
42 getString("xyz");
43 }
44
45
46
47
48
49
50
51 public final String getString(final String key)
52 {
53 if (this.currentLocale == null || !this.currentLocale.equals(DefaultLocale.getLocale()))
54 {
55 if (DefaultLocale.getLocale() == null)
56 {
57 DefaultLocale.setLocale(new Locale("en"));
58 }
59 this.currentLocale = DefaultLocale.getLocale();
60 Locale.setDefault(this.currentLocale);
61 try
62 {
63 this.resourceBundle = ResourceBundle.getBundle(this.bundleNamePrefix, this.currentLocale);
64 }
65 catch (MissingResourceException e)
66 {
67 try
68 {
69 this.resourceBundle = ResourceBundle.getBundle("resources/" + this.bundleNamePrefix, this.currentLocale);
70 }
71 catch (MissingResourceException e2)
72 {
73 return '!' + key.substring(key.indexOf('.') + 1) + '!';
74 }
75 }
76 }
77 if (null == this.resourceBundle)
78 {
79
80 return '!' + key.substring(key.indexOf('.') + 1) + '!';
81 }
82 try
83 {
84 return this.resourceBundle.getString(key);
85 }
86 catch (MissingResourceException e)
87 {
88 return '!' + key.substring(key.indexOf('.') + 1) + '!';
89 }
90 }
91
92
93
94
95
96
97
98 public final String getDefaultString(final String key)
99 {
100 if (this.defaultLocale == null)
101 {
102 this.defaultLocale = new Locale("en");
103 try
104 {
105 this.defaultResourceBundle = ResourceBundle.getBundle(this.bundleNamePrefix, this.defaultLocale);
106 }
107 catch (MissingResourceException e)
108 {
109 try
110 {
111 this.defaultResourceBundle =
112 ResourceBundle.getBundle("resources/" + this.bundleNamePrefix, this.defaultLocale);
113 }
114 catch (MissingResourceException e2)
115 {
116 return '!' + key.substring(key.indexOf('.') + 1) + '!';
117 }
118 }
119 }
120 if (null == this.defaultResourceBundle)
121 {
122
123 return '!' + key.substring(key.indexOf('.') + 1) + '!';
124 }
125 try
126 {
127 return this.defaultResourceBundle.getString(key);
128 }
129 catch (MissingResourceException e)
130 {
131 return '!' + key.substring(key.indexOf('.') + 1) + '!';
132 }
133 }
134
135
136
137
138 public boolean isDefault()
139 {
140 if (this.currentLocale == null || this.defaultLocale == null | !this.currentLocale.equals(this.defaultLocale)
141 || !this.currentLocale.equals(DefaultLocale.getLocale()))
142 {
143 return false;
144 }
145 return true;
146 }
147 }