KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > util > l10n > Locales


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: Locales.java,v 1.7 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.util.l10n;
21
22 import java.util.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26 import org.apache.log4j.*;
27
28 import org.enhydra.barracuda.core.event.*;
29
30
31 /**
32  * Simple locale utilities. This class makes it easy to determine the target
33  * locale from an event context or a servlet request. You can also set the locale
34  * and ask the class to save the information for you (in a cookie and/or the
35  * session) so that the locale info will persist across muliple requests
36  */

37 public class Locales extends org.enhydra.barracuda.plankton.l10n.Locales {
38
39     protected static final Logger logger = Logger.getLogger(Locales.class.getName());
40
41     /**
42      * Get the client locale from an EventContext using the default
43      * param keys and persist option. This will probably be the method
44      * you use most frequently.
45      *
46      * @param ec the EventContext from which we'd like to determine Locale
47      * @return the target client Locale
48      */

49     public static Locale getClientLocale(EventContext ec) {
50         return getClientLocale(ec, LANGAUGE_KEY, COUNTRY_KEY, VARIANT_KEY, PERSIST_DEFAULT);
51     }
52
53     /**
54      * Get the client locale from an EventContext
55      *
56      * @param ec the EventContext from which we'd like to determine Locale
57      * @param languageKey the key to be used to look in the request for a
58      * language paramter
59      * @param countryKey the key to be used to look in the request for a
60      * country paramter
61      * @param variantKey the key to be used to look in the request for a
62      * variant paramter
63      * @param persistOption how we'd like to persist the Locale (by default, it will be stored
64      * in the SESSION)
65      * @return the target client Locale
66      */

67     public static Locale getClientLocale(EventContext ec, String JavaDoc languageKey, String JavaDoc countryKey, String JavaDoc variantKey, int persistOption) {
68         HttpServletRequest req = (HttpServletRequest) ec.getState(ControlEventContext.HTTP_SERVLET_REQUEST);
69         HttpServletResponse resp = (HttpServletResponse) ec.getState(ViewEventContext.HTTP_SERVLET_RESPONSE);
70         return getClientLocale(req, resp, languageKey, countryKey, variantKey, persistOption);
71     }
72
73     /**
74      * Save the client locale using an EventContext using the default
75      * persist option
76      *
77      * @param ec the EventContext in which we'd like to set Locale
78      * @param loc the target client locale we'd like to set
79      */

80     public static void saveClientLocale(EventContext ec, Locale loc) {
81         saveClientLocale(ec, loc, PERSIST_DEFAULT);
82     }
83
84     /**
85      * Save the client locale using an EventContext
86      *
87      * @param ec the EventContext in which we'd like to set Locale
88      * @param loc the target client locale we'd like to set
89      * @param persistOption the specific persistOption to be used
90      */

91     public static void saveClientLocale(EventContext ec, Locale loc, int persistOption) {
92         HttpServletRequest req = (HttpServletRequest) ec.getState(ControlEventContext.HTTP_SERVLET_REQUEST);
93         HttpServletResponse resp = (HttpServletResponse) ec.getState(ViewEventContext.HTTP_SERVLET_RESPONSE);
94         saveClientLocale(req, resp, loc, persistOption);
95     }
96
97     /**
98      * Release the client locale using an EventContext (this effectively
99      * removes it from whereever it might have been persisted). This means that
100      * on the next request the locale will be determined from scratch again.
101      *
102      * @param ec the EventContext in which we'd like to set Locale (this tells us
103      * where the locale info needs to be removed from)
104      */

105     public static void releaseClientLocale(EventContext ec) {
106         releaseClientLocale(ec, PERSIST_DEFAULT);
107     }
108
109     /**
110      * Release the client locale using an EventContext (this effectively
111      * removes it from whereever it might have been persisted). This means that
112      * on the next request the locale will be determined from scratch again.
113      *
114      * @param ec the EventContext in which we'd like to set Locale
115      * @param persistOption the specific persistOption to be used (this tells us
116      * where the locale info needs to be removed from)
117      */

118     public static void releaseClientLocale(EventContext ec, int persistOption) {
119         HttpServletRequest req = (HttpServletRequest) ec.getState(ControlEventContext.HTTP_SERVLET_REQUEST);
120         HttpServletResponse resp = (HttpServletResponse) ec.getState(ViewEventContext.HTTP_SERVLET_RESPONSE);
121         releaseClientLocale(req, resp);
122     }
123 }
124
Popular Tags