Thibstars

LanguageManager

Nov 16th, 2016
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. import java.util.Locale;
  2. import java.util.MissingResourceException;
  3. import java.util.ResourceBundle;
  4.  
  5. /**
  6.  * Class that has the responsibility to provide correct application text based on the set locale.
  7.  *
  8.  * @author Thibault Helsmoortel
  9.  */
  10.  
  11. public final class LanguageManager {
  12.     private static String base = "StringResources";
  13.     private static LanguageManager classInstance = new LanguageManager();
  14.     private static Locale locale = new Locale(System.getProperty("user.language"));
  15.     private static ResourceBundle bundle = updateBundle(locale);
  16.  
  17.     /**
  18.      * Class constructor. Publicly unavailable.
  19.      */
  20.     private LanguageManager() {
  21.         //Constructor not publicly available
  22.     }
  23.  
  24.     /**
  25.      * Updates the resource bundle according to the given locale.
  26.      *
  27.      * @param switchLocale the locale for which to update the resource bundle
  28.      * @return the updated resource bundle
  29.      */
  30.     private static ResourceBundle updateBundle(Locale switchLocale) {
  31.         LanguageManager.locale = switchLocale;
  32.         try {
  33.             bundle = ResourceBundle.getBundle(base, LanguageManager.locale);
  34.         } catch (MissingResourceException e) {
  35.             LanguageManager.getInstance().setLocale(Locale.ENGLISH);
  36.         } finally {
  37.             bundle = ResourceBundle.getBundle(base, LanguageManager.locale);
  38.         }
  39.         return bundle;
  40.     }
  41.  
  42.     /**
  43.      * Returns an instance of the manager.
  44.      *
  45.      * @return an instance of the manager
  46.      */
  47.     public static synchronized LanguageManager getInstance() {
  48.         if (bundle == null) {
  49.             updateBundle(locale);
  50.         }
  51.         return classInstance;
  52.     }
  53.  
  54.     /**
  55.      * Returns a translated String based on a given key.
  56.      *
  57.      * @param key the key for which to find a translated String
  58.      * @return the key's respective translated String
  59.      */
  60.     public String translate(String key) {
  61.         if (!bundle.containsKey(key)) throw new IllegalArgumentException("Key not found: " + key);
  62.         return bundle.getString(key);
  63.     }
  64.  
  65.     /**
  66.      * Returns the manager's locale.
  67.      *
  68.      * @return the manager's locale
  69.      */
  70.     public Locale getLocale() {
  71.         return locale;
  72.     }
  73.  
  74.     /**
  75.      * Sets the manager's locale and updates the resource bundle accordingly.
  76.      *
  77.      * @param locale the manager's locale
  78.      */
  79.     public void setLocale(Locale locale) {
  80.         updateBundle(locale);
  81.     }
  82.  
  83.     /**
  84.      * Returns the base name of the ResourceBundle to use.
  85.      *
  86.      * @return the base name of the ResourceBundle to use
  87.      */
  88.     public static String getBase() {
  89.         return base;
  90.     }
  91.  
  92.     /**
  93.      * Sets the new base name of the ResourceBundle to use.
  94.      *
  95.      * @param base the new base name of the ResourceBundle to use
  96.      */
  97.     public static void setBase(String base) {
  98.         LanguageManager.base = base;
  99.     }
  100.  
  101.     @Override
  102.     protected Object clone() throws CloneNotSupportedException {
  103.         throw new UnsupportedOperationException();
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment