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