]> git.basschouten.com Git - openhab-addons.git/blob
903bde253ea21c9a5a7e829703db3eb88fd9ba7f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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     @NonNullByDefault
57     private class UnknownLocale implements LocaleProvider {
58         @Override
59         public Locale getLocale() {
60             return java.util.Locale.ROOT;
61         }
62     }
63
64     /*
65      * ************************
66      * ***** Constructors *****
67      */
68
69     /**
70      * Constructor
71      * <P>
72      * Initializes the {@link Localization} module without any framework informations.
73      */
74     Localization() {
75         this.localeProvider = new UnknownLocale();
76     }
77
78     /**
79      * Constructor
80      * <P>
81      * Initializes the {@link Localization} module with framework informations.
82      *
83      * @param localeProvider providing a locale,
84      * @param i18nProvider as service interface for internationalization.
85      */
86     public Localization(final LocaleProvider localeProvider, final TranslationProvider i18nProvider) {
87         logger.trace("Localization(Constructor w/ {},{}) called.", localeProvider, i18nProvider);
88         this.localeProvider = localeProvider;
89         this.i18nProvider = i18nProvider;
90     }
91
92     /**
93      * Converts a given message into an equivalent localized message.
94      *
95      * @param key the message of type {@link String} to be converted,
96      * @param arguments (optional) arguments being referenced within the messageString.
97      * @return <B>localizedMessageString</B> the resulted message of type {@link String}.
98      */
99     public String getText(String key, Object... arguments) {
100         if (i18nProvider == null) {
101             logger.trace("getText() returns default as no i18nProvider existant.");
102             return key;
103         }
104         Bundle bundle = FrameworkUtil.getBundle(this.getClass()).getBundleContext().getBundle();
105         Locale locale = localeProvider.getLocale();
106         String defaultText = OPENBRACKET.concat(key).concat(CLOSEBRACKET);
107
108         String text = i18nProvider.getText(bundle, key, defaultText, locale, arguments);
109         if (text == null) {
110             logger.warn("Internal error: localization for key {} is missing.", key);
111             text = defaultText;
112         }
113         logger.trace("getText() returns {}.", text);
114         return text;
115     }
116 }