]> git.basschouten.com Git - openhab-addons.git/blob
6072ae59cba44a39000d937741aa421bd6358ce4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.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;
27
28 /**
29  * {@link ShellyTranslationProvider} provides i18n message lookup
30  *
31  * @author Markus Michels - Initial contribution
32  */
33 @NonNullByDefault
34 @Component(service = ShellyTranslationProvider.class)
35 public class ShellyTranslationProvider {
36     private final Bundle bundle;
37     private final TranslationProvider i18nProvider;
38     private final LocaleProvider localeProvider;
39
40     @Activate
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;
46     }
47
48     public String get(String key, @Nullable Object... arguments) {
49         return getText(key.contains("@text/") || key.contains(".shelly.") ? key : "message." + key, arguments);
50     }
51
52     public String getText(String key, @Nullable Object... arguments) {
53         try {
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;
59         }
60     }
61
62     public @Nullable String getDefaultText(String key) {
63         return i18nProvider.getText(bundle, key, key, Locale.ENGLISH);
64     }
65 }