001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package org.lucane.webconnector.util;
021:
022: import java.io.*;
023: import java.net.*;
024: import java.util.*;
025:
026: import org.lucane.common.Logging;
027: import org.lucane.common.concepts.UserConcept;
028: import org.lucane.webconnector.WebApp;
029: import org.lucane.webconnector.security.LucanePrincipal;
030:
031: /**
032: * Handles translations of messages
033: */
034: public class Translation {
035: private static HashMap translations = new HashMap();
036:
037: public static Translation get(WebApp app, LucanePrincipal principal) {
038: try {
039: return get(app, principal.getUser());
040: } catch (Exception e) {
041: return get(app, "en");
042: }
043: }
044:
045: public static Translation get(WebApp app, UserConcept user) {
046: return get(app, user.getLanguage());
047: }
048:
049: public static Translation get(WebApp app, String lang) {
050: Translation t = (Translation) translations.get(app.getName()
051: + "-" + lang);
052: if (t == null) {
053: t = new Translation();
054: t.load(app, lang);
055: translations.put(app.getName() + "-" + lang, t);
056: }
057:
058: return t;
059: }
060:
061: //-- instance specific
062:
063: private ResourceBundle bundle;
064: private ResourceBundle defaultBundle;
065:
066: /**
067: * Places the correct Locale.
068: * Load the properties file
069: */
070: private void load(WebApp app, String lang) {
071: String appdir = app.getDirectory();
072: try {
073: InputStream is = new URL(appdir
074: + "messages/messages.properties").openStream();
075: bundle = new PropertyResourceBundle(is);
076: defaultBundle = bundle;
077: } catch (Exception e) {
078: bundle = null;
079: defaultBundle = null;
080: }
081:
082: try {
083: InputStream is = new URL(appdir + "messages/messages_"
084: + lang + ".properties").openStream();
085: bundle = new PropertyResourceBundle(is);
086: } catch (Exception e) {
087: if (bundle == null)
088: Logging.getLogger().info("unable to set language");
089: }
090: }
091:
092: /**
093: * Message translaion
094: *
095: * @param origin string to fetch
096: * @return the correct string
097: */
098: public String tr(String origin) {
099: try {
100: return bundle.getString(origin);
101: } catch (Exception e) {
102: try {
103: return defaultBundle.getString(origin);
104: } catch (Exception e2) {
105: return origin;
106: }
107: }
108: }
109: }
|