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