]> git.basschouten.com Git - openhab-addons.git/blob
9f9bc33fcaac2ea7c9d59736a424c14b0bc405b2
[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.digitalstrom.internal.providers;
14
15 import java.util.Locale;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
18
19 import org.openhab.core.i18n.TranslationProvider;
20 import org.osgi.framework.Bundle;
21 import org.osgi.service.component.ComponentContext;
22
23 /**
24  * The {@link BaseDsI18n} provides the internationalization service in form of the
25  * {@link org.openhab.core.i18n.TranslationProvider} of the
26  * digitalSTROM-Bindings. So this class can be implement e.g. by provider implementations like the
27  * {@link org.openhab.core.thing.type.ChannelTypeProvider}.
28  *
29  * @author Michael Ochel - initial contributer
30  * @author Matthias Siegele - initial contributer
31  */
32 public abstract class BaseDsI18n {
33
34     public static final String LABEL_ID = "label";
35     public static final String DESC_ID = "desc";
36     public static final String SEPERATOR = "_";
37
38     private TranslationProvider translationProvider;
39     private Bundle bundle;
40
41     /**
42      * Initializes the {@link BaseDsI18n}.
43      *
44      * @param componentContext
45      */
46     protected void activate(ComponentContext componentContext) {
47         this.bundle = componentContext.getBundleContext().getBundle();
48         init();
49     }
50
51     /**
52      * Will be call after the {@link BaseDsI18n} is initialized and can be overridden by subclasses to handle some
53      * initial jobs.
54      */
55     protected void init() {
56         // Can be overridden by subclasses
57     }
58
59     /**
60      * Disposes the {@link BaseDsI18n}.
61      *
62      * @param componentContext
63      */
64     protected void deactivate(ComponentContext componentContext) {
65         this.bundle = null;
66     }
67
68     /**
69      * Sets the {@link TranslationProvider} at the {@link BaseDsI18n}.
70      *
71      * @param translationProvider
72      */
73
74     protected void setTranslationProvider(TranslationProvider translationProvider) {
75         this.translationProvider = translationProvider;
76     }
77
78     /**
79      * Unsets the {@link TranslationProvider} at the {@link BaseDsI18n}.
80      *
81      * @param translationProvider
82      */
83     protected void unsetTranslationProvider(TranslationProvider translationProvider) {
84         this.translationProvider = null;
85     }
86
87     /**
88      * Returns the internationalized text in the language of the {@link Locale} of the given key. If the key does not
89      * exist at the internationalization of the {@link Locale} the {@link Locale#ENGLISH} will be used. If the key does
90      * not exists in {@link Locale#ENGLISH}, too, the key will be returned.
91      *
92      * @param key
93      * @param locale
94      * @return internationalized text
95      */
96     protected String getText(String key, Locale locale) {
97         return translationProvider != null
98                 ? translationProvider.getText(bundle, key,
99                         translationProvider.getText(bundle, key, key, Locale.ENGLISH), locale)
100                 : key;
101     }
102
103     /**
104      * Returns the internationalized label in the language of the {@link Locale} of the given key.
105      *
106      * @param key of internationalization label
107      * @param locale of the wished language
108      * @return internationalized label
109      * @see #getText(String, Locale)
110      */
111     protected String getLabelText(String key, Locale locale) {
112         return getText(buildIdentifier(key, LABEL_ID), locale);
113     }
114
115     /**
116      * Returns the internationalized description in the language of the {@link Locale} of the given key.
117      *
118      * @param key of internationalization description
119      * @param locale of the wished language
120      * @return internationalized description
121      * @see #getText(String, Locale)
122      */
123     protected String getDescText(String key, Locale locale) {
124         return getText(buildIdentifier(key, DESC_ID), locale);
125     }
126
127     /**
128      * Builds the key {@link String} through the given {@link Object}s.<br>
129      * The key will be build as lower case {@link Object#toString()} + {@link #SEPERATOR} + {@link Object#toString()} +
130      * ... , so the result {@link String} will be look like "object1_object2"
131      *
132      * @param parts to join
133      * @return key
134      */
135     public static String buildIdentifier(Object... parts) {
136         return Stream.of(parts) //
137                 .map(Object::toString) //
138                 .map(String::toLowerCase) //
139                 .collect(Collectors.joining(SEPERATOR));
140     }
141 }