1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.web.l10n;
35
36 import fr.paris.lutece.portal.service.datastore.DatastoreService;
37 import fr.paris.lutece.portal.service.util.AppLogService;
38 import fr.paris.lutece.portal.service.util.AppPropertiesService;
39 import java.util.ArrayList;
40 import java.util.List;
41
42 import java.util.Locale;
43
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpSession;
46
47
48
49
50 public final class LocaleService
51 {
52 private static final String ATTRIBUTE_SELECTED_LOCALE = "LUTECE_ATTRIBUTE_USER_SELECTED_LOCALE";
53 private static final String DSKEY_LANGUAGE_DEFAULT = "portal.site.site_property.locale.default";
54 private static final String PROPERTY_LANG_LIST = "lutece.i18n.availableLocales";
55 private static final String PROPERTY_LANG_DEFAULT = "lutece.i18n.defaultLocale";
56 private static Locale _locale;
57 private static List<Locale> _supportedLocales;
58
59
60
61
62 private LocaleService( )
63 {
64 }
65
66
67
68
69
70
71
72
73
74 public static void setUserSelectedLocale( HttpServletRequest request, Locale locale )
75 {
76 HttpSession session = request.getSession( true );
77 session.setAttribute( ATTRIBUTE_SELECTED_LOCALE, locale );
78 }
79
80
81
82
83
84
85
86
87 public static Locale getUserSelectedLocale( HttpServletRequest request )
88 {
89 Locale locale = LocaleService.getDefault( );
90 HttpSession session = request.getSession( );
91
92 if ( session != null )
93 {
94 Locale localeSession = (Locale) session.getAttribute( ATTRIBUTE_SELECTED_LOCALE );
95
96 if ( localeSession != null )
97 {
98 locale = localeSession;
99 }
100 }
101
102 return locale;
103 }
104
105
106
107
108
109
110 public static synchronized Locale getDefault( )
111 {
112 if ( _locale != null )
113 {
114 return _locale;
115 }
116 else
117 {
118 String dsLang = DatastoreService.getInstanceDataValue( DSKEY_LANGUAGE_DEFAULT, null );
119
120 if ( dsLang != null )
121 {
122 for ( String strISOLang : Locale.getISOLanguages( ) )
123 {
124 if ( strISOLang.equals( dsLang ) )
125 {
126 _locale = new Locale( dsLang );
127 AppLogService.info( "LocaleService : default locale set to : {}", dsLang );
128
129 return _locale;
130 }
131 }
132 }
133
134
135 _locale = new Locale( AppPropertiesService.getProperty( PROPERTY_LANG_DEFAULT ) );
136 AppLogService.error( "LocaleService : invalid defined locale {} - default set to {}", dsLang, _locale.getLanguage( ) );
137
138 return _locale;
139 }
140 }
141
142
143
144
145
146
147
148
149
150 public static Locale getContextUserLocale( HttpServletRequest request )
151 {
152
153 if ( request != null )
154 {
155 HttpSession session = request.getSession( false );
156 if ( session != null )
157 {
158 Locale userSessionLocale = (Locale) session.getAttribute( ATTRIBUTE_SELECTED_LOCALE );
159 if ( userSessionLocale != null )
160 {
161 if ( isSupported( userSessionLocale ) )
162 {
163 return userSessionLocale;
164 }
165 }
166 }
167 }
168
169
170 Locale locale = new Locale( request.getLocale( ).getLanguage( ).substring( 0, 2 ) );
171 if ( isSupported( locale ) )
172 {
173 return locale;
174 }
175
176
177 return getDefault( );
178 }
179
180
181
182
183
184
185
186 public static boolean isSupported( Locale locale )
187 {
188 if ( _supportedLocales == null )
189 {
190 getSupportedLangList( );
191 }
192
193
194 for ( Locale supportedLocale : _supportedLocales )
195 {
196 if ( supportedLocale.equals( locale ) )
197 {
198 return true;
199 }
200 }
201
202 return false;
203 }
204
205
206
207
208
209
210 public static synchronized List<Locale> getSupportedLangList( )
211 {
212 if ( _supportedLocales == null )
213 {
214 String [ ] strLangList = AppPropertiesService.getProperty( PROPERTY_LANG_LIST ).split( "," );
215 _supportedLocales = new ArrayList<>( );
216 for ( String lang : strLangList )
217 {
218 _supportedLocales.add( new Locale( lang ) );
219 }
220 }
221
222 return _supportedLocales;
223 }
224 }