Java Resource Get getResource(String key, String bundleName, Locale locale)

Here you can find the source of getResource(String key, String bundleName, Locale locale)

Description

Returns the localized string with the given key from the given bundle.

License

Apache License

Parameter

Parameter Description
key of the resource
bundleName of the resoure
locale of the user

Return

the localized resource

Declaration

public static String getResource(String key, String bundleName, Locale locale) 

Method Source Code

//package com.java2s;
/**//from w  ww. j av  a2 s  . c o m
 * XOR, empowering Model Driven Architecture in J2EE applications
 *
 * Copyright (c) 2012, Dilip Dalton
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations 
 * under the License.
 */

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

public class Main {
    /**
     * Returns the localized string with the given key from the given bundle.
     * Works with messsage with no parameter.
     * @param key of the resource
     * @param bundleName of the resoure
     * @param locale of the user
     * @return the localized resource
     */
    public static String getResource(String key, String bundleName, Locale locale) {
        try {
            ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);
            return bundle.getString(key);
        } catch (MissingResourceException e) {
            // mimic the way jspc returns when resource is missing
            return "??? " + key + " ???";
        }
    }

    public static String getResource(String key, String bundleName, Locale locale, String[] params) {
        return MessageFormat.format(getResource(key, bundleName, locale), (Object[]) params);
    }

    public static String getResource(String key, String bundleName) {
        return getResource(key, bundleName, Locale.getDefault());
    }

    public static String getResource(String key, String bundleName, String[] params) {
        return getResource(key, bundleName, Locale.getDefault(), params);
    }
}

Related

  1. getResourceString(String key, Object... args)
  2. getResourceAsStream(Class aClass, String name)
  3. getResourceMessage(Locale locale, String resource, String key, Object... params)
  4. getResourceString(String key)
  5. getResourceBundleLocaleString(String sKey, Object[] sParam, String sResourceBundle)