1 package org.djunits.locale;
2
3 import java.io.Serializable;
4 import java.util.Locale;
5 import java.util.MissingResourceException;
6 import java.util.ResourceBundle;
7
8
9
10
11
12
13
14
15
16
17 public class Localization implements Serializable
18 {
19
20 private static final long serialVersionUID = 20200118L;
21
22
23 private final String bundleNamePrefix;
24
25
26 private transient ResourceBundle resourceBundle;
27
28
29 private Locale currentLocale = null;
30
31
32 private transient ResourceBundle defaultResourceBundle;
33
34
35 private Locale defaultLocale = null;
36
37
38
39
40
41 public Localization(final String prefix)
42 {
43 this.bundleNamePrefix = prefix;
44 getString("xyz");
45 }
46
47
48
49
50
51
52
53 public final String getString(final String key)
54 {
55 if (this.currentLocale == null || !this.currentLocale.equals(DefaultLocale.getLocale()))
56 {
57 if (DefaultLocale.getLocale() == null)
58 {
59 DefaultLocale.setLocale(new Locale("en"));
60 }
61 this.currentLocale = DefaultLocale.getLocale();
62 Locale.setDefault(this.currentLocale);
63 try
64 {
65 this.resourceBundle = ResourceBundle.getBundle(this.bundleNamePrefix, this.currentLocale);
66 }
67 catch (MissingResourceException e)
68 {
69 try
70 {
71 this.resourceBundle = ResourceBundle.getBundle("resources/" + this.bundleNamePrefix, this.currentLocale);
72 }
73 catch (MissingResourceException e2)
74 {
75 return '!' + key.substring(key.indexOf('.') + 1) + '!';
76 }
77 }
78 }
79 if (null == this.resourceBundle)
80 {
81
82 return '!' + key.substring(key.indexOf('.') + 1) + '!';
83 }
84 try
85 {
86 return this.resourceBundle.getString(key);
87 }
88 catch (MissingResourceException e)
89 {
90 return '!' + key.substring(key.indexOf('.') + 1) + '!';
91 }
92 }
93
94
95
96
97
98
99
100 public final String getDefaultString(final String key)
101 {
102 if (this.defaultLocale == null)
103 {
104 this.defaultLocale = new Locale("en");
105 try
106 {
107 this.defaultResourceBundle = ResourceBundle.getBundle(this.bundleNamePrefix, this.defaultLocale);
108 }
109 catch (MissingResourceException e)
110 {
111 try
112 {
113 this.defaultResourceBundle =
114 ResourceBundle.getBundle("resources/" + this.bundleNamePrefix, this.defaultLocale);
115 }
116 catch (MissingResourceException e2)
117 {
118 return '!' + key.substring(key.indexOf('.') + 1) + '!';
119 }
120 }
121 }
122 if (null == this.defaultResourceBundle)
123 {
124
125 return '!' + key.substring(key.indexOf('.') + 1) + '!';
126 }
127 try
128 {
129 return this.defaultResourceBundle.getString(key);
130 }
131 catch (MissingResourceException e)
132 {
133 return '!' + key.substring(key.indexOf('.') + 1) + '!';
134 }
135 }
136
137
138
139
140
141 public boolean isDefault()
142 {
143 if (this.currentLocale == null || this.defaultLocale == null || !this.currentLocale.equals(this.defaultLocale)
144 || !this.currentLocale.equals(DefaultLocale.getLocale()))
145 {
146 return false;
147 }
148 return true;
149 }
150
151
152 @Override
153 public String toString()
154 {
155 return "Localization [bundleNamePrefix=" + this.bundleNamePrefix + ", resourceBundle=" + this.resourceBundle
156 + ", currentLocale=" + this.currentLocale + ", defaultResourceBundle=" + this.defaultResourceBundle
157 + ", defaultLocale=" + this.defaultLocale + "]";
158 }
159
160 }