1 | |
package org.apache.turbine.util; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.Locale; |
23 | |
|
24 | |
import org.apache.commons.lang3.StringUtils; |
25 | |
import org.apache.fulcrum.mimetype.MimeTypeService; |
26 | |
import org.apache.logging.log4j.LogManager; |
27 | |
import org.apache.logging.log4j.Logger; |
28 | |
import org.apache.turbine.Turbine; |
29 | |
import org.apache.turbine.TurbineConstants; |
30 | |
import org.apache.turbine.services.ServiceManager; |
31 | |
import org.apache.turbine.services.TurbineServices; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public class LocaleUtils |
39 | |
{ |
40 | |
|
41 | 78 | private static final Logger log = LogManager.getLogger(LocaleUtils.class); |
42 | |
|
43 | |
|
44 | 78 | private static Locale defaultLocale = null; |
45 | |
|
46 | |
|
47 | 78 | private static String defaultCharSet = null; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public static String getDefaultInputEncoding() |
55 | |
{ |
56 | |
|
57 | 105 | String inputEncoding = Turbine.getConfiguration() |
58 | 105 | .getString(TurbineConstants.PARAMETER_ENCODING_KEY, |
59 | |
TurbineConstants.PARAMETER_ENCODING_DEFAULT); |
60 | |
|
61 | 105 | log.debug("Input Encoding has been set to {}", inputEncoding); |
62 | |
|
63 | 105 | return inputEncoding; |
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public static Locale getDefaultLocale() |
76 | |
{ |
77 | 3 | if (defaultLocale == null) |
78 | |
{ |
79 | |
|
80 | 3 | String lang = Turbine.getConfiguration() |
81 | 3 | .getString(TurbineConstants.LOCALE_DEFAULT_LANGUAGE_KEY, |
82 | |
TurbineConstants.LOCALE_DEFAULT_LANGUAGE_DEFAULT); |
83 | |
|
84 | 3 | String country = Turbine.getConfiguration() |
85 | 3 | .getString(TurbineConstants.LOCALE_DEFAULT_COUNTRY_KEY, |
86 | |
TurbineConstants.LOCALE_DEFAULT_COUNTRY_DEFAULT); |
87 | |
|
88 | |
|
89 | 3 | defaultLocale = new Locale(lang, country); |
90 | |
} |
91 | |
|
92 | 3 | return defaultLocale; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public static String getDefaultCharSet() |
102 | |
{ |
103 | 9 | if (defaultCharSet == null) |
104 | |
{ |
105 | |
|
106 | 6 | defaultCharSet = Turbine.getConfiguration() |
107 | 6 | .getString(TurbineConstants.LOCALE_DEFAULT_CHARSET_KEY, |
108 | |
TurbineConstants.LOCALE_DEFAULT_CHARSET_DEFAULT); |
109 | 6 | log.debug("defaultCharSet = {} (From Properties)", defaultCharSet); |
110 | |
} |
111 | |
|
112 | 9 | String charset = defaultCharSet; |
113 | |
|
114 | 9 | if (StringUtils.isEmpty(charset)) |
115 | |
{ |
116 | 3 | log.debug("Default charset is empty!"); |
117 | |
|
118 | 3 | Locale locale = getDefaultLocale(); |
119 | 3 | log.debug("Locale is {}", locale); |
120 | |
|
121 | 3 | if (!locale.equals(Locale.US)) |
122 | |
{ |
123 | 3 | log.debug("We don't have US Locale!"); |
124 | 3 | ServiceManager serviceManager = TurbineServices.getInstance(); |
125 | 3 | MimeTypeService mimeTypeService = null; |
126 | |
try |
127 | |
{ |
128 | 3 | mimeTypeService = (MimeTypeService) serviceManager.getService(MimeTypeService.ROLE); |
129 | |
} |
130 | 0 | catch (Exception e) |
131 | |
{ |
132 | 0 | throw new RuntimeException(e); |
133 | 3 | } |
134 | 3 | charset = mimeTypeService.getCharSet(locale); |
135 | |
|
136 | 3 | log.debug("Charset now {}", charset); |
137 | |
} |
138 | |
} |
139 | |
|
140 | 9 | log.debug("Returning default Charset of {}", charset); |
141 | 9 | return charset; |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public static String getOverrideCharSet() |
152 | |
{ |
153 | 192 | return Turbine.getConfiguration() |
154 | 96 | .getString(TurbineConstants.LOCALE_OVERRIDE_CHARSET_KEY); |
155 | |
} |
156 | |
} |