-
-
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.
[sonyprojector] Allow translation of exception messages that can be d…
…isplayed in MainUI Signed-off-by: Laurent Garnier <lg.hc@free.fr>
- Loading branch information
Showing
10 changed files
with
183 additions
and
65 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
...sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/I18nException.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,103 @@ | ||
/** | ||
* Copyright (c) 2010-2021 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.sonyprojector.internal; | ||
|
||
import java.util.Locale; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.i18n.TranslationProvider; | ||
import org.osgi.framework.Bundle; | ||
|
||
/** | ||
* The {@link I18nException} class is a generic exception class implementing the internationalization of | ||
* exception message in the context of openHAB. | ||
* | ||
* @author Laurent Garnier - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class I18nException extends Exception { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private String msgKey; | ||
private @Nullable Object @Nullable [] msgParams; | ||
private @Nullable Bundle bundle; | ||
private @Nullable TranslationProvider i18nProvider; | ||
private @Nullable Locale locale; | ||
|
||
public I18nException(@Nullable String message, @Nullable Throwable cause) { | ||
super(message, cause); | ||
this.msgKey = ""; | ||
} | ||
|
||
public I18nException(@Nullable Throwable cause, String msgKey, @Nullable Object @Nullable... msgParams) { | ||
super(null, cause); | ||
this.msgKey = msgKey; | ||
this.msgParams = msgParams; | ||
} | ||
|
||
public void setupI18n(Bundle bundle, TranslationProvider i18nProvider) { | ||
setupI18n(bundle, i18nProvider, null); | ||
} | ||
|
||
public void setupI18n(Bundle bundle, TranslationProvider i18nProvider, @Nullable Locale locale) { | ||
this.bundle = bundle; | ||
this.i18nProvider = i18nProvider; | ||
this.locale = locale; | ||
} | ||
|
||
@Override | ||
public @Nullable String getMessage() { | ||
Bundle localeBundle = this.bundle; | ||
TranslationProvider localI18nProvider = this.i18nProvider; | ||
if (msgKey.isBlank() || localeBundle == null || localI18nProvider == null) { | ||
return getMessage(); | ||
} else { | ||
return localI18nProvider.getText(localeBundle, msgKey, null, Locale.ENGLISH, msgParams); | ||
} | ||
} | ||
|
||
@Override | ||
public @Nullable String getLocalizedMessage() { | ||
Bundle localeBundle = this.bundle; | ||
TranslationProvider localI18nProvider = this.i18nProvider; | ||
Locale localLocale = this.locale; | ||
if (msgKey.isBlank() || localeBundle == null || localI18nProvider == null || localLocale == null) { | ||
return getMessage(); | ||
} else { | ||
return localI18nProvider.getText(localeBundle, msgKey, null, localLocale, msgParams); | ||
} | ||
} | ||
|
||
public @Nullable String getMessageOrKeyWithParams() { | ||
if (msgKey.isBlank()) { | ||
return getMessage(); | ||
} | ||
String result = "@text/" + msgKey; | ||
Object @Nullable [] params = msgParams; | ||
if (params != null && params.length > 0) { | ||
result += " ["; | ||
boolean first = true; | ||
for (Object param : params) { | ||
if (first) { | ||
first = false; | ||
} else { | ||
result += ","; | ||
} | ||
result += String.format(" \"%s\"", param == null ? "" : param.toString()); | ||
} | ||
result += " ]"; | ||
} | ||
return result; | ||
} | ||
} |
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
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.