KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > client > util > Translation


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.client.util;
21
22 import java.io.*;
23 import java.net.*;
24 import java.util.*;
25
26 import javax.swing.UIManager JavaDoc;
27
28 import org.lucane.client.Client;
29 import org.lucane.common.Logging;
30
31 /**
32  * Handles translations of messages
33  */

34 public class Translation
35 {
36     private static ResourceBundle bundle;
37     private static ResourceBundle defaultBundle;
38     
39     /**
40      * Places the correct Locale.
41      * Load the properties file
42      */

43     public static void setLocale(String JavaDoc lang)
44     {
45         Locale.setDefault(new Locale(lang));
46         try {
47             InputStream is = new URL(getDirectory() + "messages/messages.properties").openStream();
48             Translation.bundle = new PropertyResourceBundle(is);
49             Translation.defaultBundle = Translation.bundle;
50         } catch(Exception JavaDoc e) {
51             Translation.bundle = null;
52             Translation.defaultBundle = null;
53         }
54         
55         try {
56             InputStream is = new URL(getDirectory() + "messages/messages_" + lang + ".properties").openStream();
57             Translation.bundle = new PropertyResourceBundle(is);
58         } catch(Exception JavaDoc e) {
59             if(Translation.bundle == null)
60                 Logging.getLogger().info("unable to set language");
61         }
62         
63         try {
64             if(Translation.bundle != null)
65                 Translation.changeUIMessages();
66         } catch(UnsatisfiedLinkError JavaDoc ule) {
67             //awt is not available
68
}
69     }
70     
71     /**
72      * Message translaion
73      *
74      * @param origin string to fetch
75      * @return the correct string
76      */

77     public static String JavaDoc tr(String JavaDoc origin)
78     {
79         try {
80             return bundle.getString(origin);
81         } catch(Exception JavaDoc e) {
82             try {
83                 return defaultBundle.getString(origin);
84             } catch(Exception JavaDoc e2) {
85                 return origin;
86             }
87         }
88     }
89     
90     /**
91      * Get the directory of the content inside the jar file
92      *
93      * @return the String containing the directory formed like an url
94      */

95     private static String JavaDoc getDirectory()
96     {
97         String JavaDoc url = "jar:file:///"
98             + System.getProperty("user.dir")
99             + "/lib/lucane-client-" + Client.VERSION +".jar!/";
100         
101         return url.replace('\\', '/');
102     }
103     
104     /**
105      * Change the messages for Swing objects
106      */

107     private static void changeUIMessages()
108     throws UnsatisfiedLinkError JavaDoc
109     {
110         String JavaDoc[] messages = {
111                 "FileChooser.acceptAllFileFilterText",
112                 "FileChooser.cancelButtonText",
113                 "FileChooser.cancelButtonToolTipText",
114                 "FileChooser.detailsViewButtonToolTipText",
115                 "FileChooser.directoryDescriptionText",
116                 "FileChooser.fileDescriptionText",
117                 "FileChooser.fileNameLabelText",
118                 "FileChooser.filesOfTypeLabelText",
119                 "FileChooser.helpButtonText",
120                 "FileChooser.helpButtonToolTipText",
121                 "FileChooser.homeFolderToolTipText",
122                 "FileChooser.listViewButtonToolTipText",
123                 "FileChooser.lookInLabelText",
124                 "FileChooser.newFolderErrorText",
125                 "FileChooser.newFolderToolTipText",
126                 "FileChooser.openButtonText",
127                 "FileChooser.openButtonToolTipText",
128                 "FileChooser.saveButtonText",
129                 "FileChooser.saveButtonToolTipText",
130                 "FileChooser.updateButtonText",
131                 "FileChooser.updateButtonToolTipText",
132         "FileChooser.upFolderToolTipText"};
133         
134         for(int i=0;i<messages.length;i++)
135             UIManager.put(messages[i], Translation.tr(messages[i]));
136     }
137 }
138
Popular Tags