All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.api.locale.Resources Maven / Gradle / Ivy

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.api.locale;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Helper class for localization.
 * 
 * @author André Schenk
 */
public final class Resources {

    private static final Logger LOGGER = LoggerFactory.getLogger(Resources.class);

    private static Resources resources;

    private ResourceBundle properties;

    private static final String BUNDLE_BASEDIR_NAME = "catalogs/";

    private static final String BUNDLE_BASE_NAME = "BiDiBWizard_locale";

    private static String userLocation;

    private Resources(String bidibConfigDir) {

        String userCountry = System.getProperty("user.country");
        LOGGER.info("Current user country: {}", userCountry);

        String userHome = System.getProperty("user.home");
        LOGGER.info("Current user.home: {}, userLocation: {}", userHome, userLocation);

        final File userDefinedFile;
        if (StringUtils.isBlank(userLocation)) {
            userDefinedFile = new File(userHome, ".bidib/catalogs/");
        }
        else {
            userDefinedFile = new File(userLocation, "catalogs/");
        }

        if (userDefinedFile.isDirectory()) {
            LOGGER.info("Found user-defined catalogs in directory under: {}", userDefinedFile.getPath());
            Locale locale = Locale.getDefault();
            String language = locale.getLanguage();
            String country = locale.getCountry();
            LOGGER.info("Search for user-defined catalogs, language: {}, country: {}", language, country);

            // @formatter:off
            String[] candidates =
                new String[] { 
                    BUNDLE_BASE_NAME + "_" + language + "_" + country + ".properties",
                    BUNDLE_BASE_NAME + "_" + language + ".properties", 
                    BUNDLE_BASE_NAME + ".properties" };
            // @formatter:on

            for (String candidate : candidates) {
                File candidateFile = new File(userDefinedFile, candidate);
                try {
                    LOGGER.info("Try to load file from: {}", candidateFile.getPath());
                    properties = fromFile(candidateFile);
                    LOGGER.info("Found valid catalog: {}", candidateFile.getPath());
                    // return;
                }
                catch (Exception ex) {
                    LOGGER
                        .info("Load resource bundle from user-defined file failed: {}, reason: {}",
                            candidateFile.getPath(), ex.getMessage());
                }
            }
        }
        else {
            LOGGER.info("No user-defined catalogs found in directory under: {}", userDefinedFile.getPath());
        }

        if (properties != null) {
            LOGGER.info("Set the parent.");
            ResourceBundle parent = ResourceBundle.getBundle(BUNDLE_BASEDIR_NAME + BUNDLE_BASE_NAME);

            try {
                MethodUtils.invokeMethod(properties, true, "setParent", parent);
            }
            catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
                LOGGER.warn("Set the parent failed.", ex);
            }

        }
        else {
            properties = ResourceBundle.getBundle(BUNDLE_BASEDIR_NAME + BUNDLE_BASE_NAME);
        }
    }

    public static void setUserLocation(String userLocation) {
        Resources.userLocation = userLocation;
    }

    // This method doesn't use localization
    private static ResourceBundle fromFile(File userDefinedFile) throws IOException {
        FileInputStream fis = new FileInputStream(userDefinedFile);
        try {
            return new PropertyResourceBundle(fis);
        }
        finally {
            fis.close();
        }
    }

    private static synchronized Resources getInstance() {
        if (resources == null) {
            resources = new Resources(userLocation);
        }
        return resources;
    }

    private static ResourceBundle getResources() {
        return getInstance().properties;
    }

    public static String getString(Class clazz, String key) {

        String resourceName = null;
        if (clazz != null) {
            resourceName = clazz.getName() + "." + key;
        }
        else {
            resourceName = key;
        }
        return getString(resourceName);
    }

    public static String trimWizardPackage(String clazz) {
        String resource = clazz;
        return resource;
    }

    /**
     * Prepare the resource key.
     * 
     * @param resourceName
     *            the resource name
     * @param key
     *            the key
     * @return the resource key
     */
    public static String prepareResourceKey(String resourceName, String key) {
        return resourceName + "." + key;
    }

    /**
     * Prepare the resource key.
     * 
     * @param clazz
     *            the resource class name
     * @param key
     *            the key
     * @return the resource key
     */
    public static String prepareResourceKey(Class clazz, String key) {

        String resourceName = null;
        if (clazz != null) {
            resourceName = clazz.getName() + "." + key;
        }
        else {
            resourceName = key;
        }
        return resourceName;
    }

    public static String getString(String resourceName, String key) {
        return getString(resourceName + "." + key);
    }

    protected static String getString(String resourceName) {
        String result = null;
        ResourceBundle properties = getResources();

        if (properties != null) {
            try {
                result = properties.getString(resourceName);
            }
            catch (MissingResourceException e) {
                result = "<" + resourceName + ">";
            }
        }
        else {
            result = resourceName;
        }
        return result;
    }

    public static String getString(Class clazz, String key, Object... arguments) {
        String result = null;
        String template = getString(clazz, key);

        if (template != null) {
            template = template.replaceAll("(?) null, resourceName);

        if (template != null) {
            template = template.replaceAll("(?




© 2015 - 2024 Weber Informatics LLC | Privacy Policy