]> git.basschouten.com Git - openhab-addons.git/blob
b39225c99a3aad8c9c6bcd25fdbf9b7ad050ed91
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.shelly.internal.provider;
14
15 import java.util.Locale;
16
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;
22 import org.osgi.framework.FrameworkUtil;
23 import org.osgi.service.component.annotations.Activate;
24 import org.osgi.service.component.annotations.Component;
25 import org.osgi.service.component.annotations.Reference;
26
27 /**
28  * {@link ShellyTranslationProvider} provides i18n message lookup
29  *
30  * @author Markus Michels - Initial contribution
31  */
32 @NonNullByDefault
33 @Component(service = ShellyTranslationProvider.class)
34 public class ShellyTranslationProvider {
35     private final Bundle bundle;
36     private final TranslationProvider i18nProvider;
37     private final LocaleProvider localeProvider;
38
39     @Activate
40     public ShellyTranslationProvider(@Reference TranslationProvider i18nProvider,
41             @Reference LocaleProvider localeProvider) {
42         this.bundle = FrameworkUtil.getBundle(this.getClass());
43         this.i18nProvider = i18nProvider;
44         this.localeProvider = localeProvider;
45     }
46
47     public @Nullable String get(String key, @Nullable Object... arguments) {
48         return getText(key.contains("@text/") || key.contains(".shelly.") ? key : "message." + key, arguments);
49     }
50
51     public @Nullable String getText(String key, @Nullable Object... arguments) {
52         try {
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;
57         }
58     }
59
60     public @Nullable String getDefaultText(String key) {
61         return i18nProvider.getText(bundle, key, key, Locale.ENGLISH);
62     }
63 }