001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2002 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.client.util;
021:
022: import java.io.*;
023: import java.net.*;
024: import java.util.*;
025:
026: import javax.swing.UIManager;
027:
028: import org.lucane.client.Client;
029: import org.lucane.common.Logging;
030:
031: /**
032: * Handles translations of messages
033: */
034: public class Translation {
035: private static ResourceBundle bundle;
036: private static ResourceBundle defaultBundle;
037:
038: /**
039: * Places the correct Locale.
040: * Load the properties file
041: */
042: public static void setLocale(String lang) {
043: Locale.setDefault(new Locale(lang));
044: try {
045: InputStream is = new URL(getDirectory()
046: + "messages/messages.properties").openStream();
047: Translation.bundle = new PropertyResourceBundle(is);
048: Translation.defaultBundle = Translation.bundle;
049: } catch (Exception e) {
050: Translation.bundle = null;
051: Translation.defaultBundle = null;
052: }
053:
054: try {
055: InputStream is = new URL(getDirectory()
056: + "messages/messages_" + lang + ".properties")
057: .openStream();
058: Translation.bundle = new PropertyResourceBundle(is);
059: } catch (Exception e) {
060: if (Translation.bundle == null)
061: Logging.getLogger().info("unable to set language");
062: }
063:
064: try {
065: if (Translation.bundle != null)
066: Translation.changeUIMessages();
067: } catch (UnsatisfiedLinkError ule) {
068: //awt is not available
069: }
070: }
071:
072: /**
073: * Message translaion
074: *
075: * @param origin string to fetch
076: * @return the correct string
077: */
078: public static String tr(String origin) {
079: try {
080: return bundle.getString(origin);
081: } catch (Exception e) {
082: try {
083: return defaultBundle.getString(origin);
084: } catch (Exception e2) {
085: return origin;
086: }
087: }
088: }
089:
090: /**
091: * Get the directory of the content inside the jar file
092: *
093: * @return the String containing the directory formed like an url
094: */
095: private static String getDirectory() {
096: String url = "jar:file:///" + System.getProperty("user.dir")
097: + "/lib/lucane-client-" + Client.VERSION + ".jar!/";
098:
099: return url.replace('\\', '/');
100: }
101:
102: /**
103: * Change the messages for Swing objects
104: */
105: private static void changeUIMessages() throws UnsatisfiedLinkError {
106: String[] messages = { "FileChooser.acceptAllFileFilterText",
107: "FileChooser.cancelButtonText",
108: "FileChooser.cancelButtonToolTipText",
109: "FileChooser.detailsViewButtonToolTipText",
110: "FileChooser.directoryDescriptionText",
111: "FileChooser.fileDescriptionText",
112: "FileChooser.fileNameLabelText",
113: "FileChooser.filesOfTypeLabelText",
114: "FileChooser.helpButtonText",
115: "FileChooser.helpButtonToolTipText",
116: "FileChooser.homeFolderToolTipText",
117: "FileChooser.listViewButtonToolTipText",
118: "FileChooser.lookInLabelText",
119: "FileChooser.newFolderErrorText",
120: "FileChooser.newFolderToolTipText",
121: "FileChooser.openButtonText",
122: "FileChooser.openButtonToolTipText",
123: "FileChooser.saveButtonText",
124: "FileChooser.saveButtonToolTipText",
125: "FileChooser.updateButtonText",
126: "FileChooser.updateButtonToolTipText",
127: "FileChooser.upFolderToolTipText" };
128:
129: for (int i = 0; i < messages.length; i++)
130: UIManager.put(messages[i], Translation.tr(messages[i]));
131: }
132: }
|