View Javadoc

1   package org.opensync.engine.util;
2   
3   import java.util.*;
4   import java.text.*;
5   
6   
7   /***
8    * This class is used to internationalize the application<p>
9    * It use the ressource bundle <code>opensync</code>
10   */
11  
12  public class I18n {
13    static private I18n instance;
14    ResourceBundle messages;
15    HashMap _locales = new HashMap();
16    Locale fr_locale = new Locale("fr","FR");
17    Locale us_locale = new Locale("en","US");
18  
19    /***
20     * Construct a I18n. Use method <code>getInstance()</code> instead
21     *
22     */
23    protected I18n() {
24      _locales.put("fr_FR", fr_locale);
25      _locales.put("en_US", us_locale);
26  
27      //messages = ResourceBundle.getBundle("opensync");
28      messages = ResourceBundle.getBundle("opensync", fr_locale);
29    }
30    /***
31     * Get the singleton instance of the <code>I18n</code> class
32     *
33     */
34    static public I18n getInstance(){
35      if(instance == null){
36        setInstance();
37      }
38      return instance;
39    }
40    /***
41     * Set the the singleton instance
42     *
43     */
44    static protected synchronized void setInstance(){
45      if(instance == null){
46        instance = new I18n();
47      }
48    }
49    /***
50     * Get the message correponding to the key <code>key<code>
51     *
52     * @param	key
53     */
54    public String get(String key){
55      return messages.getString(key);
56    }
57    /***
58     * Get the formatted message correponding to the key <code>key<code>
59     *
60     * @param	key the key of the message
61     * @param	args the paramters of the message
62     *
63     */
64    public String format(String key,Object[] args){
65      MessageFormat formatter = new MessageFormat("");
66      String msg = messages.getString(key);
67      formatter.applyPattern(msg);
68      return formatter.format(args);
69    }
70  
71    /***
72     * Set the Localization
73     *
74     * @param langue_country langue and country
75     *
76     */
77     public void setLocale(String langue_country) throws MissingResourceException {
78       Locale locale = (Locale)_locales.get(langue_country);
79       if (locale != null) {
80         getInstance().messages = ResourceBundle.getBundle("opensync", locale);
81       }
82       else
83         throw new MissingResourceException(langue_country+ " not supported", "I18n", "opensync");
84     }
85  
86     public void setLocale(Locale locale) throws MissingResourceException {
87      if (locale != null) {
88        getInstance().messages = ResourceBundle.getBundle("opensync", locale);
89      }
90      else
91        throw new MissingResourceException(" no Locale", "I18n", "opensync");
92    }
93  }