2 * Copyright (c) 2010-2023 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;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
19 import org.openhab.core.i18n.TranslationProvider;
20 import org.osgi.framework.Bundle;
21 import org.osgi.service.component.ComponentContext;
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}.
29 * @author Michael Ochel - Initial contribution
30 * @author Matthias Siegele - Initial contribution
32 public abstract class BaseDsI18n {
34 public static final String LABEL_ID = "label";
35 public static final String DESC_ID = "desc";
36 public static final String SEPERATOR = "_";
38 private TranslationProvider translationProvider;
39 private Bundle bundle;
42 * Initializes the {@link BaseDsI18n}.
44 * @param componentContext
46 protected void activate(ComponentContext componentContext) {
47 this.bundle = componentContext.getBundleContext().getBundle();
52 * Will be call after the {@link BaseDsI18n} is initialized and can be overridden by subclasses to handle some
55 protected void init() {
56 // Can be overridden by subclasses
60 * Disposes the {@link BaseDsI18n}.
62 * @param componentContext
64 protected void deactivate(ComponentContext componentContext) {
69 * Sets the {@link TranslationProvider} at the {@link BaseDsI18n}.
71 * @param translationProvider
74 protected void setTranslationProvider(TranslationProvider translationProvider) {
75 this.translationProvider = translationProvider;
79 * Unsets the {@link TranslationProvider} at the {@link BaseDsI18n}.
81 * @param translationProvider
83 protected void unsetTranslationProvider(TranslationProvider translationProvider) {
84 this.translationProvider = null;
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.
94 * @return internationalized text
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)
104 * Returns the internationalized label in the language of the {@link Locale} of the given key.
106 * @param key of internationalization label
107 * @param locale of the wished language
108 * @return internationalized label
109 * @see #getText(String, Locale)
111 protected String getLabelText(String key, Locale locale) {
112 return getText(buildIdentifier(key, LABEL_ID), locale);
116 * Returns the internationalized description in the language of the {@link Locale} of the given key.
118 * @param key of internationalization description
119 * @param locale of the wished language
120 * @return internationalized description
121 * @see #getText(String, Locale)
123 protected String getDescText(String key, Locale locale) {
124 return getText(buildIdentifier(key, DESC_ID), locale);
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"
132 * @param parts to join
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));