]> git.basschouten.com Git - openhab-addons.git/blob
b6acd792bb488044614c0145612f5d83bae6213b
[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.nobohub.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.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  * This class provides translated texts
29  *
30  * @author Espen Fossen - Initial contribution
31  */
32 @NonNullByDefault
33 @Component(service = NoboHubTranslationProvider.class)
34 public class NoboHubTranslationProvider {
35
36     private final Bundle bundle;
37     private final TranslationProvider i18nProvider;
38     private final LocaleProvider localeProvider;
39
40     @Activate
41     public NoboHubTranslationProvider(@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 NoboHubTranslationProvider(final NoboHubTranslationProvider 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         Locale locale = localeProvider.getLocale();
56         String message = i18nProvider.getText(bundle, key, this.getDefaultText(key), locale, arguments);
57         if (message != null) {
58             return message;
59         }
60         return key;
61     }
62
63     public @Nullable String getDefaultText(String key) {
64         return i18nProvider.getText(bundle, key, key, Locale.ENGLISH);
65     }
66 }