]> git.basschouten.com Git - openhab-addons.git/blob
135b3a5e81abe4acb6a6f4412a97bf55f6ce0864
[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.miele.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
24 /**
25  * {@link MieleTranslationProvider} provides i18n message lookup
26  *
27  * @author Jacob Laursen - Initial contribution
28  */
29 @NonNullByDefault
30 public class MieleTranslationProvider {
31
32     private final Bundle bundle;
33     private final TranslationProvider i18nProvider;
34     private final LocaleProvider localeProvider;
35     @Nullable
36     private final Locale locale;
37
38     public MieleTranslationProvider(TranslationProvider i18nProvider, LocaleProvider localeProvider) {
39         this.bundle = FrameworkUtil.getBundle(this.getClass());
40         this.i18nProvider = i18nProvider;
41         this.localeProvider = localeProvider;
42         this.locale = null;
43     }
44
45     public MieleTranslationProvider(TranslationProvider i18nProvider, LocaleProvider localeProvider, Locale locale) {
46         this.bundle = FrameworkUtil.getBundle(this.getClass());
47         this.i18nProvider = i18nProvider;
48         this.localeProvider = localeProvider;
49         this.locale = locale;
50     }
51
52     public String getText(String key, String defaultText, @Nullable Object... arguments) {
53         String text = i18nProvider.getText(bundle, key, defaultText,
54                 locale != null ? locale : localeProvider.getLocale(), arguments);
55         if (text == null) {
56             return defaultText;
57         }
58         return text;
59     }
60 }