Salome HOME
Study validation cycle operations are implemented according to the specification.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / i18n / I18nUtils.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            Id: 
5  * Creation date   02.10.2012
6  * @author         Author: Maria KRUCHININA
7  * @version        Revision: 
8  *****************************************************************************/
9
10 package org.splat.i18n; 
11
12 import java.util.Locale;
13
14 import org.springframework.context.NoSuchMessageException;
15 import org.springframework.context.support.ResourceBundleMessageSource;
16
17 /**
18  * Resource bundle utility methods.
19  * 
20  * @author Maria KRUCHININA
21  *
22  */
23 public final class I18nUtils {
24
25         /**
26          * Contains the list of resources bundle. 
27          */
28         private static ResourceBundleMessageSource resourceBundleMessageSource;
29         
30         /**
31          * .
32          * @param locale Locale
33          * @param code key
34          * @return message i18n
35          */
36         public static String getMessage(final Locale locale, final String code) {
37                 return getMessage(locale, code, new Object[0]);
38         }
39         
40         /**
41          * .
42          * @param locale Locale
43          * @param code key
44          * @param context args
45          * @return message i18n
46          */
47         public static String getMessage(final Locale locale, final String code, final Object... context) {
48                 return resourceBundleMessageSource.getMessage(code, context, code, locale);
49         }
50         
51         
52         /**
53          * Return the translated message of a code with the language.
54          * @param language lowercase two-letter ISO-639 code
55          * @param code code
56          * @param context parameters
57          * @return Translated message
58          */
59         public static String getMessage(final String language, final String code, final Object... context) {
60                 Locale locale = new Locale(language);
61                 return getMessage(locale, code, context);
62         }       
63         
64         
65         /**
66          * Return the translated message of a code with the locale default.
67          * @param code code
68          * @param context parameters
69          * @return Translated message
70          */
71         public static String getMessageLocaleDefault(final String code, final Object... context) {
72                 String res = "";
73                 try {
74                         res = getMessage(Locale.getDefault(), code, context);
75                 } catch (NoSuchMessageException nsme) {
76                         res = getMessage(new Locale(""), code, context);
77                 }
78                 return res;
79         }       
80         
81         
82         /**
83          * Get the resourceBundleMessageSource.
84          * @return the resourceBundleMessageSource
85          */
86         protected ResourceBundleMessageSource getResourceBundleMessageSource() {
87                 return resourceBundleMessageSource;
88         }
89         /**
90          * Set the resourceBundleMessageSource.
91          * @param aResourceBundleMessageSource the resourceBundleMessageSource to set
92          */
93         public void setResourceBundleMessageSource(
94                         final ResourceBundleMessageSource aResourceBundleMessageSource) {
95                 resourceBundleMessageSource = aResourceBundleMessageSource;
96         }       
97         
98 }