]> git.basschouten.com Git - openhab-addons.git/blob
67ef01c081bc909f8fb2cc42621989ce09b44891
[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.modbus.helioseasycontrols.internal;
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.BundleContext;
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  * This class provides translated texts
29  *
30  * @author Bernhard Bauer - Initial contribution
31  */
32 @NonNullByDefault
33 @Component(service = HeliosEasyControlsTranslationProvider.class)
34 public class HeliosEasyControlsTranslationProvider {
35
36     private final Bundle bundle;
37     private final TranslationProvider i18nProvider;
38     private final LocaleProvider localeProvider;
39
40     @Activate
41     public HeliosEasyControlsTranslationProvider(@Reference TranslationProvider i18nProvider,
42             @Reference LocaleProvider localeProvider, BundleContext context) {
43         this.bundle = context.getBundle();
44         this.i18nProvider = i18nProvider;
45         this.localeProvider = localeProvider;
46     }
47
48     public HeliosEasyControlsTranslationProvider(final HeliosEasyControlsTranslationProvider other) {
49         this.bundle = other.bundle;
50         this.i18nProvider = other.i18nProvider;
51         this.localeProvider = other.localeProvider;
52     }
53
54     public String getText(String key, @Nullable Object... arguments) {
55         try {
56             Locale locale = localeProvider.getLocale();
57             String message = i18nProvider.getText(bundle, key, this.getDefaultText(key), locale, arguments);
58             if (message != null) {
59                 return message;
60             }
61         } catch (IllegalArgumentException e) {
62         }
63         return "Unable to load message for key " + key;
64     }
65
66     public @Nullable String getDefaultText(String key) {
67         return i18nProvider.getText(bundle, key, key, Locale.ENGLISH);
68     }
69 }