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-2022 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 = 20200118L;
21  
22      /** filename without .properties, to be found in the /resources folder. */
23      private final String bundleNamePrefix;
24  
25      /** the resource bundle. */
26      private transient ResourceBundle resourceBundle;
27  
28      /** current locale. */
29      private Locale currentLocale = null;
30  
31      /** the default resource bundle. */
32      private transient 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("resources/" + this.bundleNamePrefix, this.currentLocale);
66              }
67              catch (MissingResourceException e)
68              {
69                  return '!' + key.substring(key.indexOf('.') + 1) + '!';
70              }
71          }
72          if (null == this.resourceBundle)
73          {
74              // Failed to find the resourceBundle (on a previous call to getString)
75              return '!' + key.substring(key.indexOf('.') + 1) + '!';
76          }
77          try
78          {
79              return this.resourceBundle.getString(key);
80          }
81          catch (MissingResourceException e)
82          {
83              return '!' + key.substring(key.indexOf('.') + 1) + '!';
84          }
85      }
86  
87      /**
88       * Retrieve a string from the default locale bundle. If retrieval fails the value of key string, surrounded by exclamation
89       * marks is returned.
90       * @param key String; the key for the locale in the properties file
91       * @return String; localized string, or, if a translation could not be found return the key surrounded by exclamation marks
92       */
93      public final String getDefaultString(final String key)
94      {
95          if (this.defaultLocale == null)
96          {
97              this.defaultLocale = new Locale("en");
98              try
99              {
100                 this.defaultResourceBundle = ResourceBundle.getBundle("resources/" + this.bundleNamePrefix, this.defaultLocale);
101             }
102             catch (MissingResourceException e)
103             {
104                 return '!' + key.substring(key.indexOf('.') + 1) + '!';
105             }
106         }
107         if (null == this.defaultResourceBundle)
108         {
109             // Failed to find the resourceBundle (on a previous call to getString)
110             return '!' + key.substring(key.indexOf('.') + 1) + '!';
111         }
112         try
113         {
114             return this.defaultResourceBundle.getString(key);
115         }
116         catch (MissingResourceException e)
117         {
118             return '!' + key.substring(key.indexOf('.') + 1) + '!';
119         }
120     }
121 
122     /**
123      * Return whether the current locale is the default (English) locale.
124      * @return boolean; true if the current locale is the default; false if the current locale is not the default
125      */
126     public boolean isDefault()
127     {
128         if (this.currentLocale == null || this.defaultLocale == null || !this.currentLocale.equals(this.defaultLocale)
129                 || !this.currentLocale.equals(DefaultLocale.getLocale()))
130         {
131             return false;
132         }
133         return true;
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public String toString()
139     {
140         return "Localization [bundleNamePrefix=" + this.bundleNamePrefix + ", resourceBundle=" + this.resourceBundle
141                 + ", currentLocale=" + this.currentLocale + ", defaultResourceBundle=" + this.defaultResourceBundle
142                 + ", defaultLocale=" + this.defaultLocale + "]";
143     }
144 
145 }