fixed dashboard localization (#233)

- included ESH-INF to binary
- simplified getLocalizedText method as it was now only called with a null locale

Signed-off-by: Kai Kreuzer <kai@openhab.org>
This commit is contained in:
Kai Kreuzer 2017-11-19 00:25:17 +01:00 committed by GitHub
parent 3fa30c1fba
commit 008a67486e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 16 deletions

View File

@ -4,5 +4,6 @@ bin.includes = META-INF/,\
OSGI-INF/,\
web/,\
templates/,\
about.html
about.html,\
ESH-INF/
source.. = src/main/java/

View File

@ -233,19 +233,19 @@ public class DashboardService {
* fall-back would be present it returns the key.
*
* @param key key to get locale from
* @param locale known locale
* @return localized text for the key
*/
private String getLocalizedText(String key, Locale locale) {
Locale useLocale = locale == null ? localeProvider.getLocale() : locale;
private String getLocalizedText(String key) {
Locale useLocale = localeProvider.getLocale() == null ? Locale.ENGLISH : localeProvider.getLocale();
if ("locale".equals(key)) {
// The return value for "locale" key is an ISO 639-1 language code
// In case there is no translation for the used locale provided with the dashboard, "en" is returned
return bundleContext.getBundle()
.getEntry("ESH-INF/i18n/dashboard_" + useLocale.getLanguage() + ".properties") != null
? useLocale.getLanguage() : "en";
? useLocale.getLanguage()
: "en";
} else {
return i18nProvider.getText(bundleContext.getBundle(), key, key, useLocale);
}

View File

@ -12,10 +12,8 @@ import java.io.IOException;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -61,11 +59,11 @@ public class DashboardServlet extends HttpServlet {
private Set<DashboardTile> tiles;
private BiFunction<String, Locale, String> localizeFunction;
private Function<String, String> localizeFunction;
public DashboardServlet(ConfigurationAdmin configurationAdmin, String indexTemplate, String entryTemplate,
String warnTemplate, String setupTemplate, Set<DashboardTile> tiles,
BiFunction<String, Locale, String> localizeFunction) {
Function<String, String> localizeFunction) {
this.configurationAdmin = configurationAdmin;
this.indexTemplate = indexTemplate;
this.entryTemplate = entryTemplate;
@ -113,8 +111,7 @@ public class DashboardServlet extends HttpServlet {
replaceMap.put("warn", isExposed(req) ? warnTemplate : "");
// Set the messages in the session
resp.setContentType("text/html;charset=UTF-8");
// We use for UI language the server locale rather than the browser locale that we can get with req.getLocale()
resp.getWriter().append(replaceKeysWithLocaleFunction(replaceKeysFromMap(indexTemplate, replaceMap), null));
resp.getWriter().append(replaceKeysWithLocaleFunction(replaceKeysFromMap(indexTemplate, replaceMap)));
resp.getWriter().close();
}
@ -126,9 +123,7 @@ public class DashboardServlet extends HttpServlet {
Map<String, String> replaceMap = new HashMap<>();
replaceMap.put("version", OpenHAB.getVersion() + " " + OpenHAB.buildString());
resp.setContentType("text/html;charset=UTF-8");
// We use for UI language the server locale rather than the browser locale that we can get with
// req.getLocale()
resp.getWriter().append(replaceKeysWithLocaleFunction(replaceKeysFromMap(setupTemplate, replaceMap), null));
resp.getWriter().append(replaceKeysWithLocaleFunction(replaceKeysFromMap(setupTemplate, replaceMap)));
resp.getWriter().close();
}
}
@ -204,8 +199,8 @@ public class DashboardServlet extends HttpServlet {
return null;
}
private String replaceKeysWithLocaleFunction(String template, Locale locale) {
return replaceKeysWithFunction(template, (key) -> localizeFunction.apply(key, locale));
private String replaceKeysWithLocaleFunction(String template) {
return replaceKeysWithFunction(template, (key) -> localizeFunction.apply(key));
}
private String replaceKeysFromMap(String template, Map<String, String> map) {