View Javadoc
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    * Localization object for language specific reporting of units.
10   * <p>
11   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>
13   * </p>
14   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="https://tudelft.nl/pknoppers">Peter Knoppers</a>
16   */
17  public class Localization implements Serializable
18  {
19      /** */
20      private static final long serialVersionUID = 1L;
21  
22      /** filename without .properties, to be found in src/main/resources folder. */
23      private final String bundleNamePrefix;
24  
25      /** the resource bundle. */
26      private ResourceBundle resourceBundle;
27  
28      /** current locale. */
29      private Locale currentLocale = null;
30  
31      /** the default resource bundle. */
32      private ResourceBundle defaultResourceBundle;
33  
34      /** default locale. */
35      private Locale defaultLocale = null;
36  
37      /**
38       * Create a Localization object.
39       * @param prefix String; the prefix of the properties files to use.
40       */
41      public Localization(final String prefix)
42      {
43          this.bundleNamePrefix = prefix;
44          getString("xyz"); // initialize the default locale
45      }
46  
47      /**
48       * Retrieve a string from a locale bundle. If retrieval fails the value of key string, surrounded by exclamation marks is
49       * returned.
50       * @param key String; the key for the locale in the properties file
51       * @return String; localized string, or, if a translation could not be found return the key surrounded by exclamation marks
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              // Failed to find the resourceBundle (on a previous call to getString)
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       * Retrieve a string from the default locale bundle. If retrieval fails the value of key string, surrounded by exclamation
96       * marks is returned.
97       * @param key String; the key for the locale in the properties file
98       * @return String; localized string, or, if a translation could not be found return the key surrounded by exclamation marks
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             // Failed to find the resourceBundle (on a previous call to getString)
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      * Return whether the current locale is the default (English) locale.
139      * @return boolean; true if the current locale is the default; false if the current locale is not the default
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     /** {@inheritDoc} */
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 }