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
39 import java.util.Locale;
40
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpSession;
43
44
45
46
47
48 public final class LocaleService
49 {
50 private static final String ATTRIBUTE_SELECTED_LOCALE = "LUTECE_ATTRIBUTE_USER_SELECTED_LOCALE";
51 private static final String DSKEY_LANGUAGE_DEFAULT = "portal.site.site_property.locale.default";
52 private static final String LANGUAGE_DEFAULT = "en";
53 private static Locale _locale;
54
55
56
57
58 private LocaleService( )
59 {
60 }
61
62
63
64
65
66
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
75
76
77
78
79 public static Locale getUserSelectedLocale( HttpServletRequest request )
80 {
81 Locale locale = LocaleService.getDefault( );
82 HttpSession session = request.getSession( );
83
84 if ( session != null )
85 {
86 Locale localeSession = (Locale) session.getAttribute( ATTRIBUTE_SELECTED_LOCALE );
87
88 if ( localeSession != null )
89 {
90 locale = localeSession;
91 }
92 }
93
94 return locale;
95 }
96
97
98
99
100
101 public static Locale getDefault( )
102 {
103 if ( _locale == null )
104 {
105 String strCountry = DatastoreService.getInstanceDataValue( DSKEY_LANGUAGE_DEFAULT, LANGUAGE_DEFAULT );
106
107 for ( String strISOContry : Locale.getISOCountries( ) )
108 {
109 if ( strISOContry.equalsIgnoreCase( strCountry ) )
110 {
111 _locale = new Locale( strCountry );
112 AppLogService.info( "LocaleService : default locale set to : " + strCountry );
113
114 return _locale;
115 }
116 }
117
118 _locale = Locale.getDefault( );
119 AppLogService.error( "LocaleService : invalid defined locale " + strCountry + " - default set to " +
120 LANGUAGE_DEFAULT );
121 }
122
123 return _locale;
124 }
125 }