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.shelly.internal.provider;
15 import java.util.Locale;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.shelly.internal.util.ShellyUtils;
20 import org.openhab.core.i18n.LocaleProvider;
21 import org.openhab.core.i18n.TranslationProvider;
22 import org.osgi.framework.Bundle;
23 import org.osgi.framework.FrameworkUtil;
24 import org.osgi.service.component.annotations.Activate;
25 import org.osgi.service.component.annotations.Component;
26 import org.osgi.service.component.annotations.Reference;
29 * {@link ShellyTranslationProvider} provides i18n message lookup
31 * @author Markus Michels - Initial contribution
34 @Component(service = ShellyTranslationProvider.class)
35 public class ShellyTranslationProvider {
36 private final Bundle bundle;
37 private final TranslationProvider i18nProvider;
38 private final LocaleProvider localeProvider;
41 public ShellyTranslationProvider(@Reference TranslationProvider i18nProvider,
42 @Reference LocaleProvider localeProvider) {
43 this.bundle = FrameworkUtil.getBundle(this.getClass());
44 this.i18nProvider = i18nProvider;
45 this.localeProvider = localeProvider;
48 public String get(String key, @Nullable Object... arguments) {
49 return getText(key.contains("@text/") || key.contains(".shelly.") ? key : "message." + key, arguments);
52 public String getText(String key, @Nullable Object... arguments) {
54 Locale locale = localeProvider.getLocale();
55 String message = i18nProvider.getText(bundle, key, getDefaultText(key), locale, arguments);
56 return ShellyUtils.getString(message);
57 } catch (IllegalArgumentException e) {
58 return "Unable to load message for key " + key;
62 public @Nullable String getDefaultText(String key) {
63 return i18nProvider.getText(bundle, key, key, Locale.ENGLISH);