BrightSide Workbench Full Report + Source Code
LocaleProvider.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.zkoss.locale;
19 
20 import java.util.Locale;
21 import javax.servlet.http.Cookie;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24 import org.turro.elephant.zkoss.ZkossElephant;
25 import org.zkoss.web.Attributes;
26 import org.zkoss.zk.ui.Session;
27 import org.zkoss.zk.ui.util.RequestInterceptor;
28 
33 public class LocaleProvider implements RequestInterceptor {
34 
35  @Override
36  public void request(Session sess, Object request, Object response) {
37  final Cookie[] cookies = ((HttpServletRequest) request).getCookies();
38  if (cookies != null) {
39  for (int j = cookies.length; --j >= 0;) {
40  if (cookies[j].getName().equals("lang")) {
41  String val = cookies[j].getValue();
42  if(val != null) {
43  //sess.removeAttribute(Attributes.PREFERRED_LOCALE);
44  if(val.startsWith("_")) val = val.substring(1);
45  Locale locale = org.zkoss.util.Locales.getLocale(val);
46  sess.setAttribute(Attributes.PREFERRED_LOCALE, locale);
47  }
48  break;
49  }
50  }
51  }
52  ZkossElephant.instance((HttpServletRequest) request, (HttpServletResponse) response);
53  }
54 
55 }
static ZkossElephant instance(HttpServletRequest request, HttpServletResponse response)
void request(Session sess, Object request, Object response)