Package: DefaultRequestLocaleManager
DefaultRequestLocaleManager
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultRequestLocaleManager() |
|
|
|
|
|
||||||||||||||||||||
getDateTimeFormatter() |
|
|
|
|
|
||||||||||||||||||||
getLocaleSuffixes() |
|
|
|
|
|
||||||||||||||||||||
getLocales() |
|
|
|
|
|
||||||||||||||||||||
requestReset() |
|
|
|
|
|
||||||||||||||||||||
setLocale(Locale) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.core.impl.model;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import javax.inject.Provider;
32: import java.time.ZoneId;
33: import java.time.format.DateTimeFormatter;
34: import java.time.format.FormatStyle;
35: import java.util.ArrayList;
36: import java.util.List;
37: import java.util.Locale;
38: import org.springframework.beans.factory.annotation.Configurable;
39: import it.tidalwave.northernwind.core.model.RequestLocaleManager;
40: import it.tidalwave.northernwind.core.model.SiteProvider;
41: import it.tidalwave.northernwind.core.model.spi.RequestResettable;
42: import lombok.extern.slf4j.Slf4j;
43:
44: /***********************************************************************************************************************
45: *
46: * The default implementation of {@link RequestLocaleManager}.
47: *
48: * @author Fabrizio Giudici
49: *
50: **********************************************************************************************************************/
51: @Configurable @Slf4j
52:•public class DefaultRequestLocaleManager implements RequestLocaleManager, RequestResettable
53: {
54: @Inject
55: private Provider<SiteProvider> siteProvider;
56:
57:• private final ThreadLocal<Locale> localeHolder = new ThreadLocal<>();
58:
59: /*******************************************************************************************************************
60: *
61: * {@inheritDoc}
62: *
63: ******************************************************************************************************************/
64: @Override @Nonnull
65: public List<Locale> getLocales()
66: {
67: final var requestLocale = localeHolder.get();
68: final List<Locale> locales = new ArrayList<>(siteProvider.get().getSite().getConfiguredLocales());
69:
70:• if (requestLocale != null)
71: {
72: locales.remove(requestLocale);
73: locales.add(0, requestLocale);
74: }
75:
76: log.debug(">>>> locales: {}", locales);
77:
78: return locales;
79: }
80:
81: /*******************************************************************************************************************
82: *
83: * {@inheritDoc}
84: *
85: ******************************************************************************************************************/
86: @Override @Nonnull
87: public List<String> getLocaleSuffixes()
88: {
89: final List<String> suffixes = new ArrayList<>();
90: suffixes.add("");
91:
92:• for (final var locale : getLocales())
93: {
94: suffixes.add("_" + locale.getLanguage());
95: }
96:
97: return suffixes;
98: }
99:
100: /*******************************************************************************************************************
101: *
102: * {@inheritDoc}
103: *
104: ******************************************************************************************************************/
105: @Override
106: public boolean setLocale (@Nonnull final Locale locale)
107: {
108:• if (siteProvider.get().getSite().getConfiguredLocales().contains(locale))
109: {
110: log.debug("setting locale to {} ...", locale);
111: localeHolder.set(locale);
112: return true;
113: }
114: else
115: {
116: log.warn("Can't set locale to {}, not in the configured ones", locale);
117: return false;
118: }
119: }
120:
121: /*******************************************************************************************************************
122: *
123: * {@inheritDoc}
124: *
125: ******************************************************************************************************************/
126: @Override
127: public void requestReset()
128: {
129: localeHolder.remove();
130: }
131:
132: /*******************************************************************************************************************
133: *
134: * {@inheritDoc}
135: *
136: ******************************************************************************************************************/
137: @Override @Nonnull
138: public DateTimeFormatter getDateTimeFormatter()
139: {
140: return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
141: .withLocale(getLocales().get(0))
142: .withZone(ZoneId.systemDefault());
143: }
144: }