1 /*****************************************************************************
5 * Creation date 02.10.2012
6 * @author Author: Maria KRUCHININA
8 *****************************************************************************/
10 package org.splat.i18n;
12 import java.util.Locale;
14 import org.springframework.context.NoSuchMessageException;
15 import org.springframework.context.support.ResourceBundleMessageSource;
18 * Resource bundle utility methods.
20 * @author Maria KRUCHININA
23 public final class I18nUtils {
26 * Contains the list of resources bundle.
28 private static ResourceBundleMessageSource resourceBundleMessageSource;
32 * @param locale Locale
34 * @return message i18n
36 public static String getMessage(final Locale locale, final String code) {
37 return getMessage(locale, code, new Object[0]);
42 * @param locale Locale
45 * @return message i18n
47 public static String getMessage(final Locale locale, final String code, final Object... context) {
48 return resourceBundleMessageSource.getMessage(code, context, code, locale);
53 * Return the translated message of a code with the language.
54 * @param language lowercase two-letter ISO-639 code
56 * @param context parameters
57 * @return Translated message
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);
66 * Return the translated message of a code with the locale default.
68 * @param context parameters
69 * @return Translated message
71 public static String getMessageLocaleDefault(final String code, final Object... context) {
74 res = getMessage(Locale.getDefault(), code, context);
75 } catch (NoSuchMessageException nsme) {
76 res = getMessage(new Locale(""), code, context);
83 * Get the resourceBundleMessageSource.
84 * @return the resourceBundleMessageSource
86 protected ResourceBundleMessageSource getResourceBundleMessageSource() {
87 return resourceBundleMessageSource;
90 * Set the resourceBundleMessageSource.
91 * @param aResourceBundleMessageSource the resourceBundleMessageSource to set
93 public void setResourceBundleMessageSource(
94 final ResourceBundleMessageSource aResourceBundleMessageSource) {
95 resourceBundleMessageSource = aResourceBundleMessageSource;