-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- indroduce localization of error messages - add new strings for common exceptions - provide helper functions for translation - add test cases Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
- Loading branch information
1 parent
24d5f2d
commit 0fecab5
Showing
10 changed files
with
431 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...nding.knx/src/main/java/org/openhab/binding/knx/internal/i18n/KNXTranslationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* Copyright (c) 2010-2022 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.knx.internal.i18n; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Locale; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.i18n.LocaleProvider; | ||
import org.openhab.core.i18n.TranslationProvider; | ||
import org.osgi.framework.Bundle; | ||
import org.osgi.framework.FrameworkUtil; | ||
|
||
/** | ||
* This class provides translations. It is a helper class for i18n / localization efforts. | ||
* | ||
* @implNote It is implemented as a static singelton, enforced by the single-element enum pattern. | ||
* @apiNote @set() must be called to provide tanslation service, otherwise all functions will return untranslated text. | ||
* Thread safety is ensured. | ||
* @author Holger Friedrich - Initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public enum KNXTranslationProvider { | ||
I18N; | ||
|
||
private @Nullable LocaleProvider localeProvider; | ||
private @Nullable TranslationProvider translationProvider; | ||
private Bundle bundle; | ||
|
||
private KNXTranslationProvider() { | ||
localeProvider = null; | ||
translationProvider = null; | ||
bundle = FrameworkUtil.getBundle(this.getClass()); | ||
} | ||
|
||
/** | ||
* get translated text | ||
* | ||
* @param text text to be translated, may contain placeholders \{n\} for the n-th optional argument of this function | ||
* @param arguments any optional arguments, will be inserted | ||
* @return translated text with subsitutions if translationprovide is set and provides a translation, otherwise | ||
* returns original text with substitutions | ||
*/ | ||
public String get(final String text, @Nullable Object @Nullable... arguments) { | ||
// ensure thread safety: calls to set(..) should not lead to race condition | ||
final TranslationProvider translationProvider = this.translationProvider; | ||
final LocaleProvider localeProvider = this.localeProvider; | ||
if (translationProvider != null) { | ||
// localeProvider might be null, but if not, getLoacle will return NonNull Locale | ||
// locale cannot be cached, as getLocale() will return different result once locale is changed by user | ||
final Locale locale = (localeProvider != null) ? localeProvider.getLocale() : Locale.getDefault(); | ||
final String res = translationProvider.getText(bundle, text, text, locale, arguments); | ||
if (res != null) | ||
return res; | ||
} | ||
// translating not possibe, we still have the original text without any subsititutions | ||
if ((arguments == null) || (arguments.length == 0)) { | ||
return text; | ||
} | ||
// else execute pattern subsitution in untranslated text | ||
return MessageFormat.format(text, arguments); | ||
} | ||
|
||
/** | ||
* get exception in user readable (and possibly localized) form | ||
* | ||
* @param e any exception | ||
* @return localized message in form <description (translated)> (<class name>, <e.getLocalizedMessage (not | ||
* translated)>), empty string for null. May possibly change in further releases. | ||
*/ | ||
public String getLocalizedException(final @Nullable Throwable e) { | ||
if (e == null) { | ||
return ""; | ||
} | ||
StringBuffer res = new StringBuffer(); | ||
final String exName = e.getClass().getSimpleName(); | ||
final String key = "exception." + exName; | ||
final String translatedDescription = KNXTranslationProvider.I18N.get(key); | ||
Boolean foundTranslation = !key.equals(translatedDescription); | ||
// detailed message cannot be translated, e.getLocalizedMessage will likely return English | ||
String detail = e.getLocalizedMessage(); | ||
if (detail == null) { | ||
detail = ""; | ||
} | ||
|
||
if (foundTranslation) { | ||
res.append(translatedDescription); | ||
res.append(" ("); | ||
res.append(exName); | ||
if (!detail.isBlank()) { | ||
res.append(", "); | ||
res.append(detail); | ||
} | ||
res.append(")"); | ||
} else { | ||
res.append(exName); | ||
if (!detail.isBlank()) { | ||
res.append(", "); | ||
res.append(detail); | ||
} | ||
} | ||
return res.toString(); | ||
} | ||
|
||
/** | ||
* Set translation providers. To be called to make any translation work. | ||
* | ||
* @param localeProvider openHAB locale provider, can be generated via \@Activate / \@Reference LocaleProvider in | ||
* handler factory | ||
* @param translationProvider openHAB locale provider, can be generated via \@Activate / \@Reference | ||
* TranslationProvider in handler factory | ||
*/ | ||
public void setProvider(@Nullable LocaleProvider localeProvider, | ||
@Nullable TranslationProvider translationProvider) { | ||
this.localeProvider = localeProvider; | ||
this.translationProvider = translationProvider; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.