LocaleService.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.portal.web.l10n;

  35. import fr.paris.lutece.portal.service.datastore.DatastoreService;
  36. import fr.paris.lutece.portal.service.util.AppLogService;
  37. import fr.paris.lutece.portal.service.util.AppPropertiesService;
  38. import java.util.ArrayList;
  39. import java.util.List;

  40. import java.util.Locale;

  41. import javax.servlet.http.HttpServletRequest;
  42. import javax.servlet.http.HttpSession;

  43. /**
  44.  * LocaleService
  45.  */
  46. public final class LocaleService
  47. {
  48.     private static final String ATTRIBUTE_SELECTED_LOCALE = "LUTECE_ATTRIBUTE_USER_SELECTED_LOCALE";
  49.     private static final String DSKEY_LANGUAGE_DEFAULT = "portal.site.site_property.locale.default";
  50.     private static final String PROPERTY_LANG_LIST = "lutece.i18n.availableLocales";
  51.     private static final String PROPERTY_LANG_DEFAULT = "lutece.i18n.defaultLocale";
  52.     private static Locale _locale;
  53.     private static List<Locale> _supportedLocales;

  54.     /**
  55.      * Private constructor
  56.      */
  57.     private LocaleService( )
  58.     {
  59.     }

  60.     /**
  61.      * Set the locale selected by the user in the front office. The user may select a language without been authenticated.
  62.      *
  63.      * @param request
  64.      *            The request
  65.      * @param locale
  66.      *            The locale
  67.      */
  68.     public static void setUserSelectedLocale( HttpServletRequest request, Locale locale )
  69.     {
  70.         HttpSession session = request.getSession( true );
  71.         session.setAttribute( ATTRIBUTE_SELECTED_LOCALE, locale );
  72.     }

  73.     /**
  74.      * Get a Locale selected by the user in front office
  75.      *
  76.      * @param request
  77.      *            The HTTP request
  78.      * @return The locale selected, or the default jvm locale if no locale has been selected
  79.      */
  80.     public static Locale getUserSelectedLocale( HttpServletRequest request )
  81.     {
  82.         Locale locale = LocaleService.getDefault( );
  83.         HttpSession session = request.getSession( );

  84.         if ( session != null )
  85.         {
  86.             Locale localeSession = (Locale) session.getAttribute( ATTRIBUTE_SELECTED_LOCALE );

  87.             if ( localeSession != null )
  88.             {
  89.                 locale = localeSession;
  90.             }
  91.         }

  92.         return locale;
  93.     }

  94.     /**
  95.      * Return a Lutece defined default Locale
  96.      *
  97.      * @return The locale
  98.      */
  99.     public static synchronized Locale getDefault( )
  100.     {
  101.         if ( _locale != null )
  102.         {
  103.             return _locale;
  104.         }
  105.         else
  106.         {
  107.             String dsLang = DatastoreService.getInstanceDataValue( DSKEY_LANGUAGE_DEFAULT, null );

  108.             if ( dsLang != null )
  109.             {
  110.                 for ( String strISOLang : Locale.getISOLanguages( ) )
  111.                 {
  112.                     if ( strISOLang.equals( dsLang ) )
  113.                     {
  114.                         _locale = new Locale( dsLang );
  115.                         AppLogService.info( "LocaleService : default locale set to : {}", dsLang );

  116.                         return _locale;
  117.                     }
  118.                 }
  119.             }

  120.             // otherwise, get the default locale from properties
  121.             _locale = new Locale( AppPropertiesService.getProperty( PROPERTY_LANG_DEFAULT ) );
  122.             AppLogService.error( "LocaleService : invalid defined locale {} - default set to {}", dsLang, _locale.getLanguage( ) );

  123.             return _locale;
  124.         }
  125.     }

  126.     /**
  127.      * Retrieve the best supported locale for the user's session Priority order : 1- selected locale session attribute 2- browser locale (if supported) 3-
  128.      * default server locale
  129.      *
  130.      * @param request
  131.      *            The HTTP request
  132.      * @return The locale
  133.      */
  134.     public static Locale getContextUserLocale( HttpServletRequest request )
  135.     {
  136.         // consider the current session locale
  137.         if ( request != null )
  138.         {
  139.             HttpSession session = request.getSession( false );
  140.             if ( session != null )
  141.             {
  142.                 Locale userSessionLocale = (Locale) session.getAttribute( ATTRIBUTE_SELECTED_LOCALE );
  143.                 if ( userSessionLocale != null )
  144.                 {
  145.                     if ( isSupported( userSessionLocale ) )
  146.                     {
  147.                         return userSessionLocale;
  148.                     }
  149.                 }
  150.             }
  151.         }

  152.         // consider the browser language
  153.         Locale locale = new Locale( request.getLocale( ).getLanguage( ).substring( 0, 2 ) );
  154.         if ( isSupported( locale ) )
  155.         {
  156.             return locale;
  157.         }

  158.         // otherwise consider the server default
  159.         return getDefault( );
  160.     }

  161.     /**
  162.      * check if Locale is supported according to locale list in lutece properties
  163.      *
  164.      * @param locale
  165.      * @return true if supported
  166.      */
  167.     public static boolean isSupported( Locale locale )
  168.     {
  169.         if ( _supportedLocales == null )
  170.         {
  171.             getSupportedLangList( );
  172.         }

  173.         // check if the mandatory language is supported
  174.         for ( Locale supportedLocale : _supportedLocales )
  175.         {
  176.             if ( supportedLocale.equals( locale ) )
  177.             {
  178.                 return true;
  179.             }
  180.         }

  181.         return false;
  182.     }

  183.     /**
  184.      * get Supported Lang List
  185.      *
  186.      * @return the Supported Lang List
  187.      */
  188.     public static synchronized List<Locale> getSupportedLangList( )
  189.     {
  190.         if ( _supportedLocales == null )
  191.         {
  192.             String [ ] strLangList = AppPropertiesService.getProperty( PROPERTY_LANG_LIST ).split( "," );
  193.             _supportedLocales = new ArrayList<>( );
  194.             for ( String lang : strLangList )
  195.             {
  196.                 _supportedLocales.add( new Locale( lang ) );
  197.             }
  198.         }

  199.         return _supportedLocales;
  200.     }
  201. }