Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Basic UI internationalization #4338

Merged
merged 1 commit into from Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,6 @@
main.offline-msg = Offline: waiting for connection to become available

sitemaps-list.welcome = Welcome!
sitemaps-list.available-sitemaps = Available sitemaps

sitemaps-list-empty.info = It seems like you have not defined any sitemaps yet. To build one, please check the documentation for guidance.
@@ -0,0 +1,6 @@
main.offline-msg = Non connecté: en attente d'une connexion disponible
sitemaps-list.welcome = Bienvenue!
sitemaps-list.available-sitemaps = Sitemaps disponibles
sitemaps-list-empty.info = Il semble que vous n'ayez encore défini aucune sitemap. Pour en construire une, merci de vous aider de la documentation.
Expand Up @@ -21,6 +21,7 @@ Import-Package:
org.eclipse.emf.ecore.resource,
org.eclipse.jdt.annotation;resolution:=optional,
org.eclipse.smarthome.core.events,
org.eclipse.smarthome.core.i18n,
org.eclipse.smarthome.core.items,
org.eclipse.smarthome.core.items.events,
org.eclipse.smarthome.core.library.items,
Expand Down
Expand Up @@ -11,7 +11,9 @@
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="org.eclipse.smarthome.ui.basic.pagerenderer">
<implementation class="org.eclipse.smarthome.ui.basic.internal.render.PageRenderer"/>

<reference bind="setItemUIRegistry" cardinality="1..1" interface="org.eclipse.smarthome.ui.items.ItemUIRegistry" name="ItemUIRegistry" policy="dynamic" unbind="unsetItemUIRegistry"/>
<reference bind="setItemUIRegistry" cardinality="1..1" interface="org.eclipse.smarthome.ui.items.ItemUIRegistry" name="ItemUIRegistry" policy="static" unbind="unsetItemUIRegistry"/>
<reference bind="setLocaleProvider" cardinality="1..1" interface="org.eclipse.smarthome.core.i18n.LocaleProvider" name="LocaleProvider" policy="static" unbind="unsetLocaleProvider"/>
<reference bind="setTranslationProvider" cardinality="1..1" interface="org.eclipse.smarthome.core.i18n.TranslationProvider" name="TranslationProvider" policy="static" unbind="unsetTranslationProvider"/>
<service>
<provide interface="org.eclipse.smarthome.ui.basic.internal.render.PageRenderer"/>
</service>
Expand Down
Expand Up @@ -56,7 +56,7 @@
<script type="text/html" id="template-offline-notify">
<div class="mdl-notify">
<div class="mdl-notify__text">
Offline: waiting for connection to become available
%main.offline-msg%
</div>
</div>
</script>
Expand Down
@@ -1,5 +1,5 @@
<h4 class="mdl-cell mdl-cell--12-col">Welcome!</h3>
<h5 class="mdl-cell mdl-cell--12-col">Available sitemaps</h5>
<h4 class="mdl-cell mdl-cell--12-col">%sitemaps-list.welcome%</h3>
<h5 class="mdl-cell mdl-cell--12-col">%sitemaps-list.available-sitemaps%</h5>
<div class="mdl-cell mdl-cell--12-col welcome-sitemaps">
%items%
</div>
@@ -1,3 +1,3 @@
<div class="welcome-sitemaps__empty">
It seems like you have not defined any sitemaps yet. To build one, please check the documentation for guidance.
%sitemaps-list-empty.info%
</div>
Expand Up @@ -17,14 +17,17 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.smarthome.core.i18n.I18nUtil;
import org.eclipse.smarthome.core.i18n.LocaleProvider;
import org.eclipse.smarthome.core.i18n.TranslationProvider;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.model.sitemap.Widget;
import org.eclipse.smarthome.ui.basic.internal.WebAppActivator;
import org.eclipse.smarthome.ui.basic.internal.WebAppConfig;
import org.eclipse.smarthome.ui.basic.render.RenderException;
import org.eclipse.smarthome.ui.basic.render.WidgetRenderer;
import org.eclipse.smarthome.ui.items.ItemUIRegistry;
import org.osgi.service.component.ComponentContext;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -42,9 +45,13 @@ abstract public class AbstractWidgetRenderer implements WidgetRenderer {
private final Logger logger = LoggerFactory.getLogger(AbstractWidgetRenderer.class);

protected ItemUIRegistry itemUIRegistry;
protected TranslationProvider i18nProvider;
protected LocaleProvider localeProvider;

protected WebAppConfig config;

private BundleContext bundleContext;

/* the file extension of the snippets */
protected static final String SNIPPET_EXT = ".html";

Expand All @@ -66,10 +73,28 @@ public ItemUIRegistry getItemUIRegistry() {
return itemUIRegistry;
}

protected void activate(ComponentContext context) {
public void setLocaleProvider(LocaleProvider localeProvider) {
this.localeProvider = localeProvider;
}

public void unsetLocaleProvider(final LocaleProvider localeProvider) {
this.localeProvider = null;
}

public void setTranslationProvider(TranslationProvider i18nProvider) {
this.i18nProvider = i18nProvider;
}

public void unsetTranslationProvider(TranslationProvider i18nProvider) {
this.i18nProvider = null;
}

protected void activate(BundleContext context) {
this.bundleContext = context;
}

protected void deactivate(ComponentContext context) {
protected void deactivate(BundleContext context) {
this.bundleContext = null;
}

/**
Expand Down Expand Up @@ -273,4 +298,13 @@ protected String escapeHtml(String s) {
public void setConfig(WebAppConfig config) {
this.config = config;
}

protected String localizeText(String key) {
String result = "";
if (I18nUtil.isConstant(key)) {
result = this.i18nProvider.getText(this.bundleContext.getBundle(), I18nUtil.stripConstant(key), "",
this.localeProvider.getLocale());
}
return result;
}
}
Expand Up @@ -67,6 +67,7 @@ public StringBuilder processPage(String id, String sitemap, String label, EList<
throws RenderException {

String snippet = getSnippet(async ? "layer" : "main");
snippet = snippet.replaceAll("%main.offline-msg%", localizeText("@text/main.offline-msg"));
snippet = snippet.replaceAll("%id%", id);

// if the label contains a value span, we remove this span as
Expand Down Expand Up @@ -200,13 +201,20 @@ public CharSequence renderSitemapList(Set<SitemapProvider> sitemapProviders) thr

StringBuilder sb = new StringBuilder();
if (sitemapList.isEmpty()) {
sb.append(getSnippet("sitemaps_list_empty"));
String listEmptySnippet = getSnippet("sitemaps_list_empty");
listEmptySnippet = StringUtils.replace(listEmptySnippet, "%sitemaps-list-empty.info%",
localizeText("@text/sitemaps-list-empty.info"));
sb.append(listEmptySnippet);
} else {
for (String sitemap : sitemapList) {
sb.append(StringUtils.replace(sitemapSnippet, "%sitemap%", sitemap));
}
}

listSnippet = StringUtils.replace(listSnippet, "%sitemaps-list.welcome%",
localizeText("@text/sitemaps-list.welcome"));
listSnippet = StringUtils.replace(listSnippet, "%sitemaps-list.available-sitemaps%",
localizeText("@text/sitemaps-list.available-sitemaps"));
listSnippet = StringUtils.replace(listSnippet, "%items%", sb.toString());

pageSnippet = StringUtils.replace(pageSnippet, "%title%", "BasicUI");
Expand Down