2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.velux.internal.utils;
15 import java.util.Locale;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.i18n.LocaleProvider;
19 import org.openhab.core.i18n.TranslationProvider;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.FrameworkUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * This is a utility class for dealing with localization.
28 * It provides the following methods:
30 * <li>{@link #getText} returns the localized message.</li>
34 * @author Guenther Schreiner - Initial contribution
37 public class Localization {
38 private final Logger logger = LoggerFactory.getLogger(Localization.class);
42 public static final Localization UNKNOWN = new Localization();
45 * ***************************
46 * ***** Private Objects *****
48 private static final String OPENBRACKET = "(";
49 private static final String CLOSEBRACKET = ")";
50 private LocaleProvider localeProvider;
51 private @NonNullByDefault({}) TranslationProvider i18nProvider;
54 * Class, which is needed to maintain a @NonNullByDefault for class {@link Localization}.
56 private class UnknownLocale implements LocaleProvider {
58 public Locale getLocale() {
59 return java.util.Locale.ROOT;
64 * ************************
65 * ***** Constructors *****
71 * Initializes the {@link Localization} module without any framework informations.
74 this.localeProvider = new UnknownLocale();
80 * Initializes the {@link Localization} module with framework informations.
82 * @param localeProvider providing a locale,
83 * @param i18nProvider as service interface for internationalization.
85 public Localization(final LocaleProvider localeProvider, final TranslationProvider i18nProvider) {
86 logger.trace("Localization(Constructor w/ {},{}) called.", localeProvider, i18nProvider);
87 this.localeProvider = localeProvider;
88 this.i18nProvider = i18nProvider;
92 * Converts a given message into an equivalent localized message.
94 * @param key the message of type {@link String} to be converted,
95 * @param arguments (optional) arguments being referenced within the messageString.
96 * @return <B>localizedMessageString</B> the resulted message of type {@link String}.
98 public String getText(String key, Object... arguments) {
99 if (i18nProvider == null) {
100 logger.trace("getText() returns default as no i18nProvider existant.");
103 Bundle bundle = FrameworkUtil.getBundle(this.getClass()).getBundleContext().getBundle();
104 Locale locale = localeProvider.getLocale();
105 String defaultText = OPENBRACKET.concat(key).concat(CLOSEBRACKET);
107 String text = i18nProvider.getText(bundle, key, defaultText, locale, arguments);
109 logger.warn("Internal error: localization for key {} is missing.", key);
112 logger.trace("getText() returns {}.", text);