Java Resource Message getMessageString(String bundleName, String key, Object[] params, Locale locale)

Here you can find the source of getMessageString(String bundleName, String key, Object[] params, Locale locale)

Description

get Message String

License

Open Source License

Declaration

public static String getMessageString(String bundleName, String key, Object[] params, Locale locale) 

Method Source Code


//package com.java2s;
/*/*from  w w  w  .j  ava2 s.  c o  m*/
 * Copyright (c) 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Nuxeo - initial API and implementation
 *
 * $Id: I18NUtils.java 19046 2007-05-21 13:03:50Z sfermigier $
 */

import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class Main {
    public static String getMessageString(String bundleName, String key, Object[] params, Locale locale) {

        String text;

        ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale, getCurrentClassLoader(params));

        try {
            text = bundle.getString(key);
        } catch (MissingResourceException e) {
            text = key;
        }

        if (params != null) {
            MessageFormat mf = new MessageFormat(text, locale);
            text = mf.format(params, new StringBuffer(), null).toString();
        }

        return text;
    }

    static ClassLoader getCurrentClassLoader(Object defaultObject) {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader == null) {
            loader = defaultObject.getClass().getClassLoader();
        }
        return loader;
    }
}

Related

  1. getMessageCreateTime()
  2. getMessageFormat(final String bundleKey, final String messageKey, final Locale locale)
  3. getMessageFormattedTime(Date date)
  4. getMessageNoKey(String messageName)
  5. getMessages(final Class cls)
  6. getMessageString(String key, Locale locale)
  7. getMessageString(String key, Locale locale)
  8. getMessageWithArgs(String pTemplate, Object pArg0)