KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 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.webconnector.util;
21
22 import java.io.*;
23 import java.net.*;
24 import java.util.*;
25
26 import org.lucane.common.Logging;
27 import org.lucane.common.concepts.UserConcept;
28 import org.lucane.webconnector.WebApp;
29 import org.lucane.webconnector.security.LucanePrincipal;
30
31
32 /**
33  * Handles translations of messages
34  */

35 public class Translation
36 {
37     private static HashMap translations = new HashMap();
38     
39     public static Translation get(WebApp app, LucanePrincipal principal)
40     {
41         try {
42             return get(app, principal.getUser());
43         } catch (Exception JavaDoc e) {
44             return get(app, "en");
45         }
46     }
47     
48     public static Translation get(WebApp app, UserConcept user)
49     {
50         return get(app, user.getLanguage());
51     }
52     
53     public static Translation get(WebApp app, String JavaDoc lang)
54     {
55         Translation t = (Translation)translations.get(app.getName() + "-" + lang);
56         if(t == null)
57         {
58             t = new Translation();
59             t.load(app, lang);
60             translations.put(app.getName() + "-" + lang, t);
61         }
62             
63         return t;
64     }
65     
66     //-- instance specific
67

68     private ResourceBundle bundle;
69     private ResourceBundle defaultBundle;
70     
71     /**
72      * Places the correct Locale.
73      * Load the properties file
74      */

75     private void load(WebApp app, String JavaDoc lang)
76     {
77         String JavaDoc appdir = app.getDirectory();
78         try {
79             InputStream is = new URL(appdir + "messages/messages.properties").openStream();
80             bundle = new PropertyResourceBundle(is);
81             defaultBundle = bundle;
82         } catch(Exception JavaDoc e) {
83             bundle = null;
84             defaultBundle = null;
85         }
86         
87         try {
88             InputStream is = new URL(appdir + "messages/messages_" + lang + ".properties").openStream();
89             bundle = new PropertyResourceBundle(is);
90         } catch(Exception JavaDoc e) {
91             if(bundle == null)
92                 Logging.getLogger().info("unable to set language");
93         }
94     }
95     
96     /**
97      * Message translaion
98      *
99      * @param origin string to fetch
100      * @return the correct string
101      */

102     public String JavaDoc tr(String JavaDoc origin)
103     {
104         try {
105             return bundle.getString(origin);
106         } catch(Exception JavaDoc e) {
107             try {
108                 return defaultBundle.getString(origin);
109             } catch(Exception JavaDoc e2) {
110                 return origin;
111             }
112         }
113     }
114 }
115
Popular Tags