001/*
002 * #%L
003 * Netarchivesuite - common
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.common.utils;
024
025import java.text.MessageFormat;
026import java.util.Locale;
027import java.util.MissingResourceException;
028import java.util.ResourceBundle;
029
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033import dk.netarkivet.common.exceptions.ArgumentNotValid;
034
035/**
036 * Internationalization class.
037 */
038public class I18n {
039
040    /** Logger for this class. */
041    private static final Logger log = LoggerFactory.getLogger(I18n.class);
042
043    /** Name of the resource bundle. */
044    private final String bundleName;
045
046    /** Make an internationalisation object with the given bundle. */
047    public I18n(String translationsBundle) {
048        ArgumentNotValid.checkNotNullOrEmpty(translationsBundle, "String translationsBundle");
049        bundleName = translationsBundle;
050    }
051
052    /**
053     * Get a localized message for a given locale and label, and optionally arguments.
054     * <p>
055     * E.g.
056     * <p>
057     * I18N.getString(Locale.default, "job.unknown.id", 17)
058     * <p>
059     * In contrast to {@link java.util.ResourceBundle#getString}, this method is forgiving on errors
060     *
061     * @param locale The locale to get the string for
062     * @param label The label of the string in the resource bundle
063     * @param args Any args required for formatting the label
064     * @return The localised string, or the label if the string could not be found or the format is invalid or does not
065     * match the args.
066     * @throws ArgumentNotValid on null or empty local or label.
067     */
068    public String getString(Locale locale, String label, Object... args) {
069        // Arguments checked in helper method.
070        return getString(bundleName, locale, label, args);
071    }
072
073    /**
074     * Get a localized message for a given resource bundle, locale and label.
075     * <p>
076     * In contrast to {@link java.util.ResourceBundle#getString}, this method is forgiving on errors
077     * <p>
078     * I18n.getString("dk.netarkivet.common.Translations", Locale.default, "job.unknown.id", 17)
079     *
080     * @param bundleName The name of the resource bundle, fully qualified, but without the properties. See
081     * {@link java.util.ResourceBundle#getBundle(String)}
082     * @param locale The locale to get the string for
083     * @param label The label of the string in the resource bundle
084     * @param args Any args required for formatting the label
085     * @return The localised string, or the label if the string could not be found or the format is invalid or does not
086     * match the args.
087     * @throws ArgumentNotValid on null bundleName, locale or label.
088     */
089    public static String getString(String bundleName, Locale locale, String label, Object... args) {
090        ArgumentNotValid.checkNotNullOrEmpty(bundleName, "String bundleName");
091        ArgumentNotValid.checkNotNull(locale, "Locale locale");
092        ArgumentNotValid.checkNotNullOrEmpty(label, "String label");
093        try {
094            ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);
095            String message = bundle.getString(label);
096            try {
097                return new MessageFormat(message, locale).format(args);
098            } catch (IllegalArgumentException e) {
099                log.warn("I18n bundle '{}' has wrong format '{}' for label '{}'", bundleName, message, label, e);
100                return label;
101            }
102        } catch (MissingResourceException e) {
103            log.warn("I18n bundle '{}' is missing label '{}'", bundleName, label, e);
104            return label;
105        }
106    }
107
108}