001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui;
023:
024: import java.net.URL;
025: import java.util.ArrayList;
026: import java.util.Locale;
027: import java.util.Properties;
028:
029: import org.apache.log4j.Logger;
030:
031: /**
032: * This class provides internationalization support for the XML GUI
033: */
034: public class InternationalizationManager {
035: private static Properties properties = new Properties();
036: private static Logger log = Logger
037: .getLogger(InternationalizationManager.class);
038: private static ArrayList sources = new ArrayList();
039:
040: /**
041: * Implement this interface to provide extensions to the internationalizationManager
042: */
043: public interface InternationalizationSource {
044: public String getString(String identifier);
045: };
046:
047: /**
048: * Add a language file to the internationalization database
049: */
050: public static void addLanguageFile(String prefix)
051: throws GUIException {
052: try {
053: URL url = InternationalizationManager.class.getResource("/"
054: + prefix + "." + Locale.getDefault().toString()
055: + ".properties");
056: properties.load(url.openStream());
057: } catch (Exception e) {
058: throw new GUIException(
059: "Error while loading internationalization property file",
060: e);
061: }
062: }
063:
064: /**
065: * Add an external source for internationalizations
066: */
067: public static void addInternationalizationSource(
068: InternationalizationSource source) {
069: sources.add(source);
070: }
071:
072: /**
073: * Get the internationalized string for a generic identifier
074: */
075: public static String getString(String identifier) {
076: try {
077: String value = properties.getProperty(identifier);
078: if (value == null) {
079: for (int i = 0, size = sources.size(); i < size; i++) {
080: value = ((InternationalizationSource) sources
081: .get(i)).getString(identifier);
082: if (value != null)
083: return value;
084: }
085: if (!identifier.equals(""))
086: log.warn("no internationalization for identifier ["
087: + identifier + "]");
088: return identifier;
089: }
090: return value;
091: } catch (Exception e) {
092: if (identifier == null)
093: return "(null)";
094: else
095: throw new RuntimeException(
096: "Error while looking up internationalization",
097: e);
098: }
099: }
100: }
|