KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > LocaleAction


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.frontend;
17
18 import org.apache.cocoon.acting.Action;
19 import org.apache.cocoon.environment.*;
20 import org.apache.cocoon.i18n.I18nUtils;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22 import org.apache.avalon.framework.parameters.Parameters;
23
24 import java.util.Map JavaDoc;
25 import java.util.Locale JavaDoc;
26
27 /**
28  * Action that puts the locale (the java.util.Locale, not a string)
29  * as an attribute called "locale" on the request object.
30  *
31  * <p>It determines the locale as follows:
32  * <ul>
33  * <li>First look in the session
34  * <li>Then look for a cookie
35  * <li>Then take the one specified in the default param.
36  * </ul>
37  *
38  */

39 public class LocaleAction implements Action, ThreadSafe {
40
41     public Map JavaDoc act(Redirector redirector, SourceResolver sourceResolver, Map JavaDoc objectModel, String JavaDoc s, Parameters parameters) throws Exception JavaDoc {
42         Request request = ObjectModelHelper.getRequest(objectModel);
43         Session session = request.getSession(false);
44
45         Locale JavaDoc locale = session != null ? (Locale JavaDoc)session.getAttribute("locale") : null;
46         String JavaDoc localeParam = request.getParameter("locale");
47         if (localeParam != null) {
48             // when a locale request parameter is specified, this only changes the language
49
// for the current request, thus no session or cookie is updated
50
locale = I18nUtils.parseLocale(localeParam);
51         } else if (locale == null) {
52             Cookie[] cookies = request.getCookies();
53             if (cookies != null) {
54                 for (int i = 0; i < cookies.length; i++) {
55                     if (cookies[i].getName().equals("locale")) {
56                         String JavaDoc value = cookies[i].getValue();
57                         if (value != null) {
58                             locale = I18nUtils.parseLocale(value);
59                         }
60                         break;
61                     }
62                 }
63             }
64
65             // If no cookie was found, use locale specified in "default" parameter
66
if (locale == null) {
67                 locale = I18nUtils.parseLocale(parameters.getParameter("default"));
68             }
69
70             // Set locale in session for quick future retrieval
71
if (session != null)
72                 session.setAttribute("locale", locale);
73         }
74
75         WikiHelper.changeLocale(locale, request);
76         return null;
77     }
78 }
79
Popular Tags