1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet.filters.language;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.language.UnicodeLanguageUtil;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.servlet.StringServletResponse;
22  import com.liferay.portal.kernel.util.HttpUtil;
23  import com.liferay.portal.kernel.util.LocaleUtil;
24  import com.liferay.portal.kernel.util.StringBundler;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.servlet.filters.BasePortalFilter;
27  import com.liferay.util.servlet.ServletResponseUtil;
28  
29  import java.util.Locale;
30  import java.util.regex.Matcher;
31  import java.util.regex.Pattern;
32  
33  import javax.servlet.FilterChain;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  /**
38   * <a href="LanguageFilter.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Eduardo Lundgren
41   * @author Shuyang Zhou
42   */
43  public class LanguageFilter extends BasePortalFilter {
44  
45      protected void processFilter(
46              HttpServletRequest request, HttpServletResponse response,
47              FilterChain filterChain)
48          throws Exception {
49  
50          StringServletResponse stringResponse = new StringServletResponse(
51              response);
52  
53          processFilter(
54              LanguageFilter.class, request, stringResponse, filterChain);
55  
56          if (_log.isDebugEnabled()) {
57              String completeURL = HttpUtil.getCompleteURL(request);
58  
59              _log.debug("Translating response " + completeURL);
60          }
61  
62          String content = translateResponse(request, stringResponse);
63  
64          ServletResponseUtil.write(response, content);
65      }
66  
67      protected String translateResponse(
68          HttpServletRequest request, StringServletResponse stringResponse) {
69  
70          String languageId = LanguageUtil.getLanguageId(request);
71          Locale locale = LocaleUtil.fromLanguageId(languageId);
72  
73          String content = stringResponse.getString();
74  
75          StringBundler sb = new StringBundler();
76  
77          Matcher matcher = _pattern.matcher(content);
78  
79          int x = 0;
80  
81          while (matcher.find()) {
82              int y = matcher.start(0);
83  
84              String key = matcher.group(1);
85  
86              sb.append(content.substring(x, y));
87              sb.append(StringPool.APOSTROPHE);
88              sb.append(UnicodeLanguageUtil.get(locale, key));
89              sb.append(StringPool.APOSTROPHE);
90  
91              x = matcher.end(0);
92          }
93  
94          sb.append(content.substring(x));
95  
96          return sb.toString();
97      }
98  
99      private static Log _log = LogFactoryUtil.getLog(LanguageFilter.class);
100 
101     private static Pattern _pattern = Pattern.compile(
102         "Liferay\\.Language\\.get\\([\"']([^)]+)[\"']\\)");
103 
104 }