Java Resource Get getResourceString(String key)

Here you can find the source of getResourceString(String key)

Description

Returns a string from the resource bundle.

License

Open Source License

Declaration

public static String getResourceString(String key) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2016 IBM Corporation 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://from  www .  j a v  a2s.  c om
 *     IBM Corporation - initial API and implementation
 *     Diamond Light Source - Custom modifications for Diamond's needs
 *******************************************************************************/

import java.text.MessageFormat;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class Main {
    private static ResourceBundle resourceBundle = ResourceBundle.getBundle("file_viewer");

    /**
     * Returns a string from the resource bundle. We don't want to crash because
     * of a missing String. Returns the key if not found.
     */
    public static String getResourceString(String key) {
        try {
            return resourceBundle.getString(key);
        } catch (MissingResourceException e) {
            return key;
        } catch (NullPointerException e) {
            return "!" + key + "!";
        }
    }

    /**
     * Returns a string from the resource bundle and binds it with the given
     * arguments. If the key is not found, return the key.
     */
    public static String getResourceString(String key, Object[] args) {
        try {
            return MessageFormat.format(getResourceString(key), args);
        } catch (MissingResourceException e) {
            return key;
        } catch (NullPointerException e) {
            return "!" + key + "!";
        }
    }
}

Related

  1. getResourceReader(Class clazz, String name, String charset)
  2. getResources(String archiveName)
  3. getResources(String pkgName)
  4. getResourcesFromDirectory(File resource, Pattern pattern)
  5. getResourceString(ResourceBundle rb, String key, Object param1)
  6. getResourceString(String key, Object... args)
  7. loadResource(Class contextClass, String resourceName)
  8. loadResource(final String name)
  9. loadResource(final String s)