    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/cataloguemanager/control/HelpMessages.java $
    002    /*----------------------------------------------------------------------------
    003     This file is part of deegree, http://deegree.org/
    004     Copyright (C) 2001-2009 by:
    005     - Department of Geography, University of Bonn -
    006     and
    007     - lat/lon GmbH -
    008
    009     This library is free software; you can redistribute it and/or modify it under
    010     the terms of the GNU Lesser General Public License as published by the Free
    011     Software Foundation; either version 2.1 of the License, or (at your option)
    012     any later version.
    013     This library is distributed in the hope that it will be useful, but WITHOUT
    014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    016     details.
    017     You should have received a copy of the GNU Lesser General Public License
    018     along with this library; if not, write to the Free Software Foundation, Inc.,
    019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    020
    021     Contact information:
    022
    023     lat/lon GmbH
    024     Aennchenstr. 19, 53177 Bonn
    025     Germany
    026     http://lat-lon.de/
    027
    028     Department of Geography, University of Bonn
    029     Prof. Dr. Klaus Greve
    030     Postfach 1147, 53001 Bonn
    031     Germany
    032     http://www.geographie.uni-bonn.de/deegree/
    033
    034     e-mail: info@deegree.org
    035    ----------------------------------------------------------------------------*/
    036    package org.deegree.portal.cataloguemanager.control;
    037
    038    import java.io.IOException;
    039    import java.io.InputStream;
    040    import java.util.HashMap;
    041    import java.util.Iterator;
    042    import java.util.Locale;
    043    import java.util.Map;
    044    import java.util.Properties;
    045
    046    import org.deegree.framework.log.ILogger;
    047    import org.deegree.framework.log.LoggerFactory;
    048    import org.deegree.framework.util.BootLogger;
    049
    050    /**
    051     * Responsible for the access to messages that are visible to the user.
    052     * <p>
    053     * Messages are read from the properties file <code>messages_LANG.properties</code> (LANG is
    054     * always a lowercased ISO 639 code), so internationalization is supported. If a certain property
    055     * (or the property file) for the specific default language of the system is not found, the message
    056     * is taken from <code>messages_en.properties</code>.
    057     *
    058     * @see Locale#getLanguage()
    059     *
    060     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    061     * @author last edited by: $Author: jmays $
    062     *
    063     * @version $Revision: 24218 $, $Date: 2010-05-07 09:33:10 +0200 (Fr, 07. Mai 2010) $
    064     */
    065    public class HelpMessages {
    066
    067        private static final ILogger LOG = LoggerFactory.getLogger( HelpMessages.class );
    068
    069        /* This definition allows Eclipse to display the content of referenced message keys. */
    070        @SuppressWarnings("unused")
    071        private static final String BUNDLE_NAME = "org.deegree.portal.cataloguemanager.controller.help_en";
    072
    073        private static Properties defaultProps = new Properties();
    074
    075        private static Map<Locale, Properties> props = new HashMap<Locale, Properties>();
    076
    077        private static String lang;
    078
    079        /**
    080         * Initialization done at class loading time.
    081         */
    082        static {
    083            try {
    084                String fileName = "help_en.properties";
    085                InputStream is = HelpMessages.class.getResourceAsStream( fileName );
    086                if ( is == null ) {
    087                    BootLogger.log( "Error while initializing " + HelpMessages.class.getName() + " : "
    088                                    + " default message file: '" + fileName + " not found." );
    089                }
    090                is = HelpMessages.class.getResourceAsStream( fileName );
    091                defaultProps.load( is );
    092                is.close();
    093
    094                // override messages using file "/message_en.properties"
    095                fileName = "/help_en.properties";
    096                overrideMessages( fileName, defaultProps );
    097
    098                lang = Locale.getDefault().getLanguage();
    099                if ( !"".equals( lang ) && !"en".equals( lang ) ) {
    100                    // override messages using file "org/deegree/i18n/message_LANG.properties"
    101                    fileName = "help_" + lang + ".properties";
    102                    overrideMessages( fileName, defaultProps );
    103                    // override messages using file "/message_LANG.properties"
    104                    fileName = "/help_" + lang + ".properties";
    105                    overrideMessages( fileName, defaultProps );
    106                }
    107            } catch ( IOException e ) {
    108                BootLogger.logError( "Error while initializing " + HelpMessages.class.getName() + " : " + e.getMessage(), e );
    109            }
    110        }
    111
    112        private static void overrideMessages( String propertiesFile, Properties props )
    113                                throws IOException {
    114            InputStream is = HelpMessages.class.getResourceAsStream( propertiesFile );
    115            if ( is != null ) {
    116                // override default messages
    117                Properties overrideProps = new Properties();
    118                overrideProps.load( is );
    119                is.close();
    120                Iterator<?> iter = overrideProps.keySet().iterator();
    121                while ( iter.hasNext() ) {
    122                    String key = (String) iter.next();
    123                    props.put( key, overrideProps.get( key ) );
    124                }
    125            }
    126        }
    127
    128        private static String get( Properties props, String key ) {
    129            String s = (String) props.get( key );
    130            return s;
    131        }
    132
    133        /**
    134         * @param loc
    135         *            the locale to be used
    136         * @param key
    137         *            to get
    138         * @return the localized message
    139         */
    140        public static synchronized String get( Locale loc, String key) {
    141            if ( loc.getLanguage().equals( lang ) ) {
    142                return get( defaultProps, key );
    143            }
    144
    145            if ( !props.containsKey( loc ) ) {
    146                Properties p = new Properties();
    147
    148                String l = loc.getLanguage();
    149
    150                if ( !"".equals( l ) ) {
    151                    try {
    152                        // override messages in this order:
    153                        // help_en.properties
    154                        // /help_en.properties
    155                        // help_lang.properties
    156                        // /help_lang.properties
    157                        String fileName = "help_en.properties";
    158                        overrideMessages( fileName, p );
    159                        fileName = "/help_en.properties";
    160                        overrideMessages( fileName, p );
    161                        fileName = "help_" + l + ".properties";
    162                        overrideMessages( fileName, p );
    163                        fileName = "/help_" + l + ".properties";
    164                        overrideMessages( fileName, p );
    165                    } catch ( IOException e ) {
    166                        LOG.logError( "Error loading language file for language '" + l + "': ", e );
    167                    }
    168                }
    169
    170                props.put( loc, p );
    171            }
    172
    173            return get( props.get( loc ), key );
    174        }
    175
    176    }