2 * Copyright (c) 2010-2021 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.shelly.internal.util;
15 import java.util.Locale;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.i18n.LocaleProvider;
20 import org.openhab.core.i18n.TranslationProvider;
21 import org.osgi.framework.Bundle;
24 * {@link ShellyTranslationProvider} provides i18n message lookup
26 * @author Markus Michels - Initial contribution
29 public class ShellyTranslationProvider {
31 private final Bundle bundle;
32 private final TranslationProvider i18nProvider;
33 private final LocaleProvider localeProvider;
35 public ShellyTranslationProvider(Bundle bundle, TranslationProvider i18nProvider, LocaleProvider localeProvider) {
37 this.i18nProvider = i18nProvider;
38 this.localeProvider = localeProvider;
41 public ShellyTranslationProvider(final ShellyTranslationProvider other) {
42 this.bundle = other.bundle;
43 this.i18nProvider = other.i18nProvider;
44 this.localeProvider = other.localeProvider;
47 public @Nullable String get(String key, @Nullable Object... arguments) {
48 return getText(key.contains("@text/") || key.contains(".shelly.") ? key : "message." + key, arguments);
51 public @Nullable String getText(String key, @Nullable Object... arguments) {
53 Locale locale = localeProvider.getLocale();
54 return i18nProvider.getText(bundle, key, getDefaultText(key), locale, arguments);
55 } catch (IllegalArgumentException e) {
56 return "Unable to load message for key " + key;
60 public @Nullable String getDefaultText(String key) {
61 return i18nProvider.getText(bundle, key, key, Locale.ENGLISH);