View Javadoc
1   package org.apache.turbine.services.localization;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Locale;
23  
24  import org.apache.fulcrum.localization.DefaultLocalizationService;
25  import org.apache.fulcrum.localization.LocalizationService;
26  import org.apache.logging.log4j.LogManager;
27  import org.apache.logging.log4j.Logger;
28  import org.apache.turbine.om.security.User;
29  import org.apache.turbine.util.RunData;
30  
31  /**
32   * 
33   * Instead of reading first the accept-language header in a http request,
34   * instead this method read the user.getTemp("locale")
35   * from the RunData to obtain the language choice by the user
36   * without the browser language rule.
37   * If user.getPerm("language") is not set,
38   *  the "Accept-Language" header is read.
39   * 
40   * Adapted from the Jetspeed-1 implementation of CustomLocalizationService.
41   * 
42   */
43  public class RundataLocalizationService extends DefaultLocalizationService implements RundataLocalizationInterface {
44  
45      private static final Logger log = LogManager.getLogger(RundataLocalizationService.class); 
46      
47      @Override
48      public Locale getLocale(RunData data) {
49          User user = data.getUser();
50          log.debug( "retrieving lang from req header :{}",
51                  (user == null || user.getTemp("locale") == null )  );
52  
53          if (user == null)
54          {
55              return getLocale(data.getRequest().getHeader(LocalizationService.ACCEPT_LANGUAGE));
56          }
57          else
58          {
59              try
60              {
61                  Locale locale = (Locale) data.getUser().getTemp("locale");
62                  if (locale == null)
63                  {
64                      return getLocale(data.getRequest().getHeader(LocalizationService.ACCEPT_LANGUAGE));
65                  }
66                  else
67                  {
68                      log.debug( "retrieved lang from temp(locale):{}", ()-> locale.getLanguage() );
69                      return locale;
70                  }
71              }
72              catch (Exception use)
73              {
74                  return getLocale(data.getRequest().getHeader(LocalizationService.ACCEPT_LANGUAGE));
75              }
76          }
77      }
78  
79  }