2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.digitalstrom.internal.providers;
15 import java.util.Locale;
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;
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}.
28 * @author Michael Ochel - initial contributer
29 * @author Matthias Siegele - initial contributer
31 public abstract class BaseDsI18n {
33 public static final String LABEL_ID = "label";
34 public static final String DESC_ID = "desc";
35 public static final String SEPERATOR = "_";
37 private TranslationProvider translationProvider;
38 private Bundle bundle;
41 * Initializes the {@link BaseDsI18n}.
43 * @param componentContext
45 protected void activate(ComponentContext componentContext) {
46 this.bundle = componentContext.getBundleContext().getBundle();
51 * Will be call after the {@link BaseDsI18n} is initialized and can be overridden by subclasses to handle some
54 protected void init() {
55 // Can be overridden by subclasses
59 * Disposes the {@link BaseDsI18n}.
61 * @param componentContext
63 protected void deactivate(ComponentContext componentContext) {
68 * Sets the {@link TranslationProvider} at the {@link BaseDsI18n}.
70 * @param translationProvider
73 protected void setTranslationProvider(TranslationProvider translationProvider) {
74 this.translationProvider = translationProvider;
78 * Unsets the {@link TranslationProvider} at the {@link BaseDsI18n}.
80 * @param translationProvider
82 protected void unsetTranslationProvider(TranslationProvider translationProvider) {
83 this.translationProvider = null;
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.
93 * @return internationalized text
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)
103 * Returns the internationalized label in the language of the {@link Locale} of the given key.
105 * @param key of internationalization label
106 * @param locale of the wished language
107 * @return internationalized label
108 * @see #getText(String, Locale)
110 protected String getLabelText(String key, Locale locale) {
111 return getText(buildIdentifier(key, LABEL_ID), locale);
115 * Returns the internationalized description in the language of the {@link Locale} of the given key.
117 * @param key of internationalization description
118 * @param locale of the wished language
119 * @return internationalized description
120 * @see #getText(String, Locale)
122 protected String getDescText(String key, Locale locale) {
123 return getText(buildIdentifier(key, DESC_ID), locale);
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"
131 * @param parts to join
134 public static String buildIdentifier(Object... parts) {
135 return StringUtils.join(parts, SEPERATOR).toLowerCase();