Example usage for java.text MessageFormat setLocale

List of usage examples for java.text MessageFormat setLocale

Introduction

In this page you can find the example usage for java.text MessageFormat setLocale.

Prototype

public void setLocale(Locale locale) 

Source Link

Document

Sets the locale to be used when creating or comparing subformats.

Usage

From source file:org.apache.myfaces.shared_impl.util.MessageUtils.java

/**
 * Uses <code>MessageFormat</code> and the supplied parameters to fill in the param placeholders in the String.
 *
 * @param locale The <code>Locale</code> to use when performing the substitution.
 * @param msgtext The original parameterized String.
 * @param params The params to fill in the String with.
 * @return The updated String./*from   w  w w. j  a  v a 2 s .co  m*/
 */
public static String substituteParams(Locale locale, String msgtext, Object params[]) {
    String localizedStr = null;
    if (params == null || msgtext == null)
        return msgtext;
    StringBuffer b = new StringBuffer(100);
    MessageFormat mf = new MessageFormat(msgtext);
    if (locale != null) {
        mf.setLocale(locale);
        b.append(mf.format(((Object) (params))));
        localizedStr = b.toString();
    }
    return localizedStr;
}

From source file:com.hypersocket.i18n.I18N.java

public static String getResource(Locale locale, String resourceBundle, String key, Object... arguments) {

    if (key == null) {
        throw new IllegalArgumentException("You must specify a key!");
    }/*w ww. ja va  2 s  . com*/
    if (resourceBundle == null) {
        throw new IllegalArgumentException("You must specify a resource bundle for key " + key);
    }

    File overideFile = getOverrideFile(locale, resourceBundle);

    if (overideFile.exists()) {

        if (!overideProperties.containsKey(overideFile)) {

            Properties properties = new Properties();
            try {
                InputStream in = new FileInputStream(overideFile);
                try {
                    properties.load(in);
                } catch (IOException ex) {
                } finally {
                    FileUtils.closeQuietly(in);
                }

                overideProperties.put(overideFile, properties);
            } catch (FileNotFoundException e) {

            }
        }

        if (overideProperties.containsKey(overideFile)) {
            Properties properties = overideProperties.get(overideFile);

            if (properties.containsKey(key)) {
                String localizedString = properties.getProperty(key);
                if (arguments == null || arguments.length == 0) {
                    return localizedString;
                }

                MessageFormat messageFormat = new MessageFormat(localizedString);
                messageFormat.setLocale(locale);
                return messageFormat.format(formatParameters(arguments));
            }
        }
    }

    String bundlePath = resourceBundle;
    if (!bundlePath.startsWith("i18n/")) {
        bundlePath = "i18n/" + resourceBundle;
    }

    try {
        ResourceBundle resource = ResourceBundle.getBundle(bundlePath, locale, I18N.class.getClassLoader());
        String localizedString = resource.getString(key);
        if (arguments == null || arguments.length == 0) {
            return localizedString;
        }

        MessageFormat messageFormat = new MessageFormat(localizedString);
        messageFormat.setLocale(locale);
        return messageFormat.format(formatParameters(arguments));
    } catch (MissingResourceException mre) {
        return "Missing resource key [i18n/" + resourceBundle + "/" + key + "]";
    }
}

From source file:org.jamwiki.utils.Utilities.java

/**
 * Given a message key, locale, and formatting parameters, return a
 * locale-specific message./*from  ww w  .  j  a v a  2s .  c  o m*/
 *
 * @param key The message key that corresponds to the formatted message
 *  being retrieved.
 * @param locale The locale for the message that is to be retrieved.
 * @param params An array of formatting parameters to use in the message
 *  being returned.
 * @return A formatted message string that is specific to the locale.
 */
public static String formatMessage(String key, Locale locale, Object[] params) {
    MessageFormat formatter = new MessageFormat("");
    formatter.setLocale(locale);
    String message = Utilities.formatMessage(key, locale);
    formatter.applyPattern(message);
    return formatter.format(params);
}

From source file:com.opensymphony.xwork2.util.LocalizedTextUtil.java

private static MessageFormat buildMessageFormat(String pattern, Locale locale) {
    MessageFormatKey key = new MessageFormatKey(pattern, locale);
    MessageFormat format = messageFormats.get(key);
    if (format == null) {
        format = new MessageFormat(pattern);
        format.setLocale(locale);
        format.applyPattern(pattern);/*from www.  j  av  a  2 s  .  com*/
        messageFormats.put(key, format);
    }

    return format;
}

From source file:com.safetys.framework.jmesa.core.message.ResourceBundleMessages.java

public String getMessage(String code, Object[] args) {
    String result = findResource(customResourceBundle, code);

    if (result == null) {
        result = findResource(defaultResourceBundle, code);
    }//from  w  w w  . jav  a  2  s  .  c om

    if (result != null && args != null) {
        MessageFormat formatter = new MessageFormat("");
        formatter.setLocale(locale);
        formatter.applyPattern(result);
        result = formatter.format(args);
    }

    return result;
}

From source file:org.jmesa.core.message.ResourceBundleMessages.java

@Override
public String getMessage(String code, Object[] args) {

    String result = findResource(customResourceBundle, code);

    if (result == null) {
        result = findResource(defaultResourceBundle, code);
    }//from   w w w  . ja va 2 s.  c  o m

    if (result != null && args != null) {
        MessageFormat formatter = new MessageFormat("");
        formatter.setLocale(locale);
        formatter.applyPattern(result);
        result = formatter.format(args);
    }

    return result;
}

From source file:org.extremecomponents.table.resource.TableResourceBundle.java

/**
 * Get the resource property./*ww  w.  j  a va  2s  .c om*/
 */
public String getMessage(String code, Object[] args) {
    String result = findResource(customResourceBundle, code);

    if (result == null) {
        result = findResource(defaultResourceBundle, code);
    }

    if (result != null && args != null) {
        MessageFormat formatter = new MessageFormat("");
        formatter.setLocale(locale);
        formatter.applyPattern(result);
        result = formatter.format(args);
    }

    return result;
}

From source file:com.googlecode.jtiger.modules.ecside.resource.TableResourceBundle.java

/**
* Get the resource property./*  www.  ja  v a2s. c om*/
*/
public String getMessage(String code, Object[] args) {
    String result = null;
    ResourceBundle customResourceBundle;
    for (int i = 0; i < customResourceBundleList.size(); i++) {
        customResourceBundle = (ResourceBundle) customResourceBundleList.get(i);
        result = findResource(customResourceBundle, code);
        if (result != null) {
            break;
        }
    }

    if (result == null) {
        result = findResource(defaultResourceBundle, code);
    }

    if (result != null && args != null) {
        MessageFormat formatter = new MessageFormat("");
        formatter.setLocale(locale);
        formatter.applyPattern(result);
        result = formatter.format(args);
    }

    return result;
}

From source file:com.ultrapower.eoms.common.plugin.ecside.resource.TableResourceBundle.java

 /**
 * Get the resource property./* w  w  w.ja va  2  s . c  o  m*/
 */
 public String getMessage(String code, Object[] args) {
   String result = null;
   ResourceBundle customResourceBundle;
   for (int i = 0; i < customResourceBundleList.size(); i++) {
      customResourceBundle = (ResourceBundle) customResourceBundleList
            .get(i);
      result = findResource(customResourceBundle, code);
      if (result != null) {
         break;
      }
   }

   if (result == null) {
      result = findResource(defaultResourceBundle, code);
   }

   if (result != null && args != null) {
      MessageFormat formatter = new MessageFormat("");
      formatter.setLocale(locale);
      formatter.applyPattern(result);
      result = formatter.format(args);
   }

   return result;
}

From source file:de.kaiserpfalzEdv.vaadin.i18n.impl.I18NHandlerImpl.java

@Override
public String get(final String key, final Object[] parameters) {
    MessageFormat message = new MessageFormat(get(key));
    message.setLocale(locale);

    LOG.trace("Translating {}='{}' with parameters: {} ...", key, get(key), parameters);

    return message.format(parameters);
}