Skip to content

Package: DefaultRequestLocaleManager

DefaultRequestLocaleManager

nameinstructionbranchcomplexitylinemethod
DefaultRequestLocaleManager()
M: 6 C: 72
92%
M: 11 C: 11
50%
M: 11 C: 1
8%
M: 0 C: 2
100%
M: 0 C: 1
100%
getDateTimeFormatter()
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getLocaleSuffixes()
M: 28 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getLocales()
M: 31 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
requestReset()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setLocale(Locale)
M: 25 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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