]> git.basschouten.com Git - openhab-addons.git/blob
b6dabaebab18cdd14776fc00ca9872f38dddccc5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.velux.internal.utils;
14
15 import java.util.Locale;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.i18n.LocaleProvider;
19 import org.openhab.core.i18n.TranslationProvider;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.FrameworkUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * This is a utility class for dealing with localization.
27  *
28  * It provides the following methods:
29  * <ul>
30  * <li>{@link #getText} returns the localized message.</li>
31  * </ul>
32  * <p>
33  *
34  * @author Guenther Schreiner - Initial contribution
35  */
36 @NonNullByDefault
37 public class Localization {
38     private final Logger logger = LoggerFactory.getLogger(Localization.class);
39
40     // Public definition
41
42     public static final Localization UNKNOWN = new Localization();
43
44     /*
45      * ***************************
46      * ***** Private Objects *****
47      */
48     private static final String OPENBRACKET = "(";
49     private static final String CLOSEBRACKET = ")";
50     private LocaleProvider localeProvider;
51     private @NonNullByDefault({}) TranslationProvider i18nProvider;
52
53     /**
54      * Class, which is needed to maintain a @NonNullByDefault for class {@link Localization}.
55      */
56     private class UnknownLocale implements LocaleProvider {
57         @Override
58         public Locale getLocale() {
59             return java.util.Locale.ROOT;
60         }
61     }
62
63     /*
64      * ************************
65      * ***** Constructors *****
66      */
67
68     /**
69      * Constructor
70      * <P>
71      * Initializes the {@link Localization} module without any framework informations.
72      */
73     Localization() {
74         this.localeProvider = new UnknownLocale();
75     }
76
77     /**
78      * Constructor
79      * <P>
80      * Initializes the {@link Localization} module with framework informations.
81      *
82      * @param localeProvider providing a locale,
83      * @param i18nProvider as service interface for internationalization.
84      */
85     public Localization(final LocaleProvider localeProvider, final TranslationProvider i18nProvider) {
86         logger.trace("Localization(Constructor w/ {},{}) called.", localeProvider, i18nProvider);
87         this.localeProvider = localeProvider;
88         this.i18nProvider = i18nProvider;
89     }
90
91     /**
92      * Converts a given message into an equivalent localized message.
93      *
94      * @param key the message of type {@link String} to be converted,
95      * @param arguments (optional) arguments being referenced within the messageString.
96      * @return <B>localizedMessageString</B> the resulted message of type {@link String}.
97      */
98     public String getText(String key, Object... arguments) {
99         if (i18nProvider == null) {
100             logger.trace("getText() returns default as no i18nProvider existant.");
101             return key;
102         }
103         Bundle bundle = FrameworkUtil.getBundle(this.getClass()).getBundleContext().getBundle();
104         Locale locale = localeProvider.getLocale();
105         String defaultText = OPENBRACKET.concat(key).concat(CLOSEBRACKET);
106
107         String text = i18nProvider.getText(bundle, key, defaultText, locale, arguments);
108         if (text == null) {
109             logger.warn("Internal error: localization for key {} is missing.", key);
110             text = defaultText;
111         }
112         logger.trace("getText() returns {}.", text);
113         return text;
114     }
115 }