Java Resource Message getMessage(Locale locale, String key, String... args)

Here you can find the source of getMessage(Locale locale, String key, String... args)

Description

get Message

License

Open Source License

Declaration

public static String getMessage(Locale locale, String key, String... args) 

Method Source Code


//package com.java2s;
/*/*from w ww.j  a  v a2  s .c  o  m*/
 * Copyright (C) 2010 Viettel Telecom. All rights reserved.
 * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

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

public class Main {
    /**
     * RESOURCE.
     */
    //private static final String RESOURCE = "com/viettel/config/ ";
    private static final String RESOURCE = "com/viettel/config/message/Language";
    private static final String RESOURCEMESSAGE = "com/viettel/config/message/Message";
    /**
     * local.
     */
    private static Locale local = null;
    /**
     * languageRb.
     */
    private static ResourceBundle languageRb = null;
    private static ResourceBundle messageRb = null;

    public static String getMessage(Locale locale, String key, String... args) {
        if (locale != null) {
            messageRb = ResourceBundle.getBundle(RESOURCEMESSAGE, locale);
        } else {
            messageRb = ResourceBundle.getBundle(RESOURCEMESSAGE);
        }
        MessageFormat msgFormat = new MessageFormat(messageRb.getString(key));
        Object[] arguments = new Object[args.length];
        int i = 0;
        for (Object obj : args) {
            String arg = (String) obj;
            try {
                arg = messageRb.getString((String) obj);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                arguments[i++] = arg;
            }

        }
        String output = msgFormat.format(arguments);
        return output;
    }

    /**
     * getString
     *
     * @param key String
     * @return value
     */
    public static String getString(String key) {
        if (local != null) {
            languageRb = ResourceBundle.getBundle(RESOURCE, local);
        } else {
            languageRb = ResourceBundle.getBundle(RESOURCE);
        }
        try {
            return languageRb.getString(key);
        } catch (Exception ex) {
            return key;
        }
    }

    public static String getString(Locale locale, String key) {
        if (locale != null) {
            languageRb = ResourceBundle.getBundle(RESOURCE, locale);
        } else {
            languageRb = ResourceBundle.getBundle(RESOURCE);
        }
        return languageRb.getString(key);
    }

    public static String getString(String key, String... args) {
        if (local != null) {
            languageRb = ResourceBundle.getBundle(RESOURCE, local);
        } else {
            languageRb = ResourceBundle.getBundle(RESOURCE);
        }
        MessageFormat msgFormat = new MessageFormat(languageRb.getString(key));
        Object[] arguments = new Object[args.length];
        int i = 0;
        for (Object obj : args) {
            String arg = (String) obj;
            try {
                arg = languageRb.getString((String) obj);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                arguments[i++] = arg;
            }

        }
        String output = msgFormat.format(arguments);
        return output;
    }

    public static String getString(Locale locale, String key, String... args) {
        if (locale != null) {
            languageRb = ResourceBundle.getBundle(RESOURCE, locale);
        } else {
            languageRb = ResourceBundle.getBundle(RESOURCE);
        }
        MessageFormat msgFormat = new MessageFormat(languageRb.getString(key));
        Object[] arguments = new Object[args.length];
        int i = 0;
        for (Object obj : args) {
            String arg = (String) obj;
            try {
                arg = languageRb.getString((String) obj);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                arguments[i++] = arg;
            }

        }
        String output = msgFormat.format(arguments);
        return output;
    }
}

Related

  1. getMessage(final Locale locale, final String messageKey)
  2. getMessage(final String bundleName, final Locale locale, final String key, final Object... params)
  3. getMessage(Locale locale, String key, Object... args)
  4. getMessage(Properties prop, String key, Object... args)
  5. getMessage(Properties props, String key, Object param)
  6. getMessage(ResourceBundle bundle, Object key, Object[] params)
  7. getMessage(ResourceBundle bundle, String key, Object... params)