View Javadoc
1   package org.djunits.locale;
2   
3   import java.util.Locale;
4   import java.util.MissingResourceException;
5   import java.util.ResourceBundle;
6   
7   /**
8    * Localization object for language sprecific reporting of units.
9    * <p>
10   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
12   * <p>
13   * $LastChangedDate: 2019-03-03 00:53:50 +0100 (Sun, 03 Mar 2019) $, @version $Revision: 349 $, by $Author: averbraeck $,
14   * initial version Jun 12, 2014 <br>
15   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="http://tudelft.nl/pknoppers">Peter Knoppers</a>
17   */
18  public class Localization
19  {
20      /** filename without .properties, to be found in src/main/resources folder. */
21      private final String bundleNamePrefix;
22  
23      /** the resource bundle. */
24      private ResourceBundle resourceBundle;
25  
26      /** current locale. */
27      private Locale currentLocale = null;
28  
29      /** the default resource bundle. */
30      private ResourceBundle defaultResourceBundle;
31  
32      /** default locale. */
33      private Locale defaultLocale = null;
34  
35      /**
36       * Create a Localization object.
37       * @param prefix String; the prefix of the properties files to use.
38       */
39      public Localization(final String prefix)
40      {
41          this.bundleNamePrefix = prefix;
42          getString("xyz"); // initialize the default locale
43      }
44  
45      /**
46       * Retrieve a string from a locale bundle. If retrieval fails the value of key string, surrounded by exclamation marks is
47       * returned.
48       * @param key String; the key for the locale in the properties file
49       * @return localized string, or, if a translation could not be found return the key surrounded by exclamation marks
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              // Failed to find the resourceBundle (on a previous call to getString)
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       * Retrieve a string from the default locale bundle. If retrieval fails the value of key string, surrounded by exclamation
94       * marks is returned.
95       * @param key String; the key for the locale in the properties file
96       * @return localized string, or, if a translation could not be found return the key surrounded by exclamation marks
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             // Failed to find the resourceBundle (on a previous call to getString)
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      * @return whether the current locale is the default (English) locale
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 }