]> git.basschouten.com Git - openhab-addons.git/blob
f4026e13e75290225413ff258078e9b27a6bbda7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.icloud.internal.utilities;
14
15 import java.util.Locale;
16
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.openhab.core.i18n.LocaleProvider;
19 import org.openhab.core.i18n.TranslationProvider;
20 import org.osgi.framework.Bundle;
21
22 /**
23  * Utility class to translate strings (i18n).
24  *
25  * @author Patrik Gfeller - Initial contribution
26  */
27 public class ICloudTextTranslator {
28
29     private final Bundle bundle;
30     private final TranslationProvider i18nProvider;
31     private final LocaleProvider localeProvider;
32
33     public ICloudTextTranslator(Bundle bundle, TranslationProvider i18nProvider, LocaleProvider localeProvider) {
34         this.bundle = bundle;
35         this.i18nProvider = i18nProvider;
36         this.localeProvider = localeProvider;
37     }
38
39     public String getText(String key, Object... arguments) {
40         Locale locale = localeProvider != null ? localeProvider.getLocale() : Locale.ENGLISH;
41         return i18nProvider != null ? i18nProvider.getText(bundle, key, getDefaultText(key), locale, arguments) : key;
42     }
43
44     public String getDefaultText(@Nullable String key) {
45         return i18nProvider.getText(bundle, key, key, Locale.ENGLISH);
46     }
47 }