From 31494c0d8ac6b0ff0fef65cc0b35e3a336f805f0 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Wed, 5 May 2021 01:19:23 +0200 Subject: [PATCH] Added i18n feature for ConfigurableServices (#2333) Signed-off-by: Christoph Weitkamp --- .../resources/OH-INF/i18n/audio.properties | 2 ++ .../resources/OH-INF/i18n/inbox.properties | 2 ++ .../OH-INF/i18n/ephemeris.properties | 2 ++ .../resources/OH-INF/i18n/restauth.properties | 2 ++ .../service/ConfigurableServiceResource.java | 34 +++++++++++++++---- .../resources/OH-INF/i18n/addons.properties | 2 ++ .../OH-INF/i18n/persistence.properties | 2 ++ .../OH-INF/i18n/jsonStorage.properties | 2 ++ .../resources/OH-INF/i18n/chart.properties | 2 ++ .../resources/OH-INF/i18n/voice.properties | 2 ++ .../resources/OH-INF/i18n/i18n.properties | 2 ++ .../resources/OH-INF/i18n/network.properties | 2 ++ 12 files changed, 49 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.core.audio/src/main/resources/OH-INF/i18n/audio.properties b/bundles/org.openhab.core.audio/src/main/resources/OH-INF/i18n/audio.properties index 030866e513d..cd77df3c11d 100644 --- a/bundles/org.openhab.core.audio/src/main/resources/OH-INF/i18n/audio.properties +++ b/bundles/org.openhab.core.audio/src/main/resources/OH-INF/i18n/audio.properties @@ -2,3 +2,5 @@ system.config.audio.defaultSource.label = Default Source system.config.audio.defaultSource.description = The default audio source to use if no other is specified. system.config.audio.defaultSink.label = Default Sink system.config.audio.defaultSink.description = The default audio sink to use if no other is specified. + +service.system.audio.label = Audio diff --git a/bundles/org.openhab.core.config.discovery/src/main/resources/OH-INF/i18n/inbox.properties b/bundles/org.openhab.core.config.discovery/src/main/resources/OH-INF/i18n/inbox.properties index 92c603e4c6f..a7ffad7222a 100644 --- a/bundles/org.openhab.core.config.discovery/src/main/resources/OH-INF/i18n/inbox.properties +++ b/bundles/org.openhab.core.config.discovery/src/main/resources/OH-INF/i18n/inbox.properties @@ -2,3 +2,5 @@ system.config.inbox.autoIgnore.label = Auto Ignore system.config.inbox.autoIgnore.description = If enabled, an Inbox result matching an existing thing is automatically ignored.
If set to false, the Inbox may contain results although identical things already exist. system.config.inbox.autoApprove.label = Auto Approve system.config.inbox.autoApprove.description = If enabled, Inbox results are automatically approved, unless they were marked as ignored. + +service.system.inbox.label = Inbox diff --git a/bundles/org.openhab.core.ephemeris/src/main/resources/OH-INF/i18n/ephemeris.properties b/bundles/org.openhab.core.ephemeris/src/main/resources/OH-INF/i18n/ephemeris.properties index 4a3283daad2..c8c9d43fefa 100644 --- a/bundles/org.openhab.core.ephemeris/src/main/resources/OH-INF/i18n/ephemeris.properties +++ b/bundles/org.openhab.core.ephemeris/src/main/resources/OH-INF/i18n/ephemeris.properties @@ -6,3 +6,5 @@ system.config.ephemeris.region.label = Region system.config.ephemeris.region.description = Get the holidays for a specific state (e.g. New York = "ny"). system.config.ephemeris.city.label = City system.config.ephemeris.city.description = Get the holidays for a specific city (e.g. New York City = "nyc"). + +service.system.ephemeris.label = Ephemeris diff --git a/bundles/org.openhab.core.io.rest.auth/src/main/resources/OH-INF/i18n/restauth.properties b/bundles/org.openhab.core.io.rest.auth/src/main/resources/OH-INF/i18n/restauth.properties index 8e84728a367..c9f12239b17 100644 --- a/bundles/org.openhab.core.io.rest.auth/src/main/resources/OH-INF/i18n/restauth.properties +++ b/bundles/org.openhab.core.io.rest.auth/src/main/resources/OH-INF/i18n/restauth.properties @@ -4,3 +4,5 @@ system.config.restauth.cacheExpiration.label = Cache Expiration Time system.config.restauth.cacheExpiration.description = When basic authentication is activated, credentials are put in a cache in order to speed up request authorization. The entries in the cache expire after a while in order to not keep credentials in memory indefinitely. This value defines the expiration time in hours. Set it to 0 for disabling the cache. system.config.restauth.implicitUserRole.label = Implicit User Role system.config.restauth.implicitUserRole.description = By default, operations requiring the "user" role are available when unauthenticated. Disabling this option will enforce authorization for these operations. Warning: This causes clients that do not support authentication to break. + +service.system.restauth.label = API Security diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/service/ConfigurableServiceResource.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/service/ConfigurableServiceResource.java index 2172d16078a..4a7f09674b6 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/service/ConfigurableServiceResource.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/service/ConfigurableServiceResource.java @@ -45,6 +45,9 @@ import org.openhab.core.config.core.ConfigurableService; import org.openhab.core.config.core.ConfigurableServiceUtil; import org.openhab.core.config.core.Configuration; +import org.openhab.core.i18n.I18nUtil; +import org.openhab.core.i18n.LocaleProvider; +import org.openhab.core.i18n.TranslationProvider; import org.openhab.core.io.rest.RESTConstants; import org.openhab.core.io.rest.RESTResource; import org.openhab.core.io.rest.core.config.ConfigurationService; @@ -105,15 +108,21 @@ public class ConfigurableServiceResource implements RESTResource { private final BundleContext bundleContext; private final ConfigDescriptionRegistry configDescRegistry; private final ConfigurationService configurationService; + private final TranslationProvider i18nProvider; + private final LocaleProvider localeProvider; @Activate public ConfigurableServiceResource( // final BundleContext bundleContext, // - final @Reference ConfigurationService configurationService, - final @Reference ConfigDescriptionRegistry configDescRegistry) { + final @Reference ConfigurationService configurationService, // + final @Reference ConfigDescriptionRegistry configDescRegistry, // + final @Reference TranslationProvider translationProvider, // + final @Reference LocaleProvider localeProvider) { this.bundleContext = bundleContext; this.configDescRegistry = configDescRegistry; this.configurationService = configurationService; + this.i18nProvider = translationProvider; + this.localeProvider = localeProvider; } @GET @@ -278,12 +287,18 @@ private List getServicesByFilter(String filter) { ConfigurableService configurableService = ConfigurableServiceUtil .asConfigurableService((key) -> serviceReference.getProperty(key)); - String label = configurableService.label(); - if (label.isEmpty()) { // for multi context services the label can be changed and must be read from - // config admin. - label = configurationService.getProperty(id, OpenHAB.SERVICE_CONTEXT); + String defaultLabel = configurableService.label(); + if (defaultLabel.isEmpty()) { // for multi context services the label can be changed and must be read + // from config admin. + defaultLabel = configurationService.getProperty(id, OpenHAB.SERVICE_CONTEXT); } + String key = I18nUtil.stripConstantOr(defaultLabel, + () -> inferKey(configurableService.description_uri(), "label")); + + String label = i18nProvider.getText(serviceReference.getBundle(), key, defaultLabel, + localeProvider.getLocale()); + String category = configurableService.category(); String configDescriptionURI = configurableService.description_uri(); @@ -294,7 +309,8 @@ private List getServicesByFilter(String filter) { boolean multiple = configurableService.factory(); - services.add(new ConfigurableServiceDTO(id, label, category, configDescriptionURI, multiple)); + services.add(new ConfigurableServiceDTO(id, label == null ? defaultLabel : label, category, + configDescriptionURI, multiple)); } } return services; @@ -380,4 +396,8 @@ private String getServicePID(final String cn, final List pids) { return first; } } + + private String inferKey(String uri, String lastSegment) { + return "service." + uri.replaceAll(":", ".") + "." + lastSegment; + } } diff --git a/bundles/org.openhab.core.karaf/src/main/resources/OH-INF/i18n/addons.properties b/bundles/org.openhab.core.karaf/src/main/resources/OH-INF/i18n/addons.properties index 720d94274ce..722575b3be3 100644 --- a/bundles/org.openhab.core.karaf/src/main/resources/OH-INF/i18n/addons.properties +++ b/bundles/org.openhab.core.karaf/src/main/resources/OH-INF/i18n/addons.properties @@ -1,2 +1,4 @@ system.config.addons.remote.label = Access Remote Repository system.config.addons.remote.description = Defines whether openHAB should access the remote repository for add-on installation. + +service.system.addons.label = Add-on Management diff --git a/bundles/org.openhab.core.persistence/src/main/resources/OH-INF/i18n/persistence.properties b/bundles/org.openhab.core.persistence/src/main/resources/OH-INF/i18n/persistence.properties index 3bdc9cf2ba2..2626b59b4da 100644 --- a/bundles/org.openhab.core.persistence/src/main/resources/OH-INF/i18n/persistence.properties +++ b/bundles/org.openhab.core.persistence/src/main/resources/OH-INF/i18n/persistence.properties @@ -1,2 +1,4 @@ system.config.persistence.default.label = Default Service system.config.persistence.default.description = The persistence service to use if no other is specified. + +service.system.persistence.label = Persistence diff --git a/bundles/org.openhab.core.storage.json/src/main/resources/OH-INF/i18n/jsonStorage.properties b/bundles/org.openhab.core.storage.json/src/main/resources/OH-INF/i18n/jsonStorage.properties index b50daaca71d..4e4b4939906 100644 --- a/bundles/org.openhab.core.storage.json/src/main/resources/OH-INF/i18n/jsonStorage.properties +++ b/bundles/org.openhab.core.storage.json/src/main/resources/OH-INF/i18n/jsonStorage.properties @@ -4,3 +4,5 @@ system.config.json_storage.write_delay.label = Write Delay system.config.json_storage.write_delay.description = Sets the time to wait before writing changes to disk. This can reduce the number of writes when many changes are being introduced within a short period. Time is defined in milliseconds. system.config.json_storage.max_defer_delay.label = Maximum Write Delay system.config.json_storage.max_defer_delay.description = Sets the maximum period the service will wait to write data to disk in the event that many changes are happening continually. + +service.system.json_storage.label = Json Storage diff --git a/bundles/org.openhab.core.ui/src/main/resources/OH-INF/i18n/chart.properties b/bundles/org.openhab.core.ui/src/main/resources/OH-INF/i18n/chart.properties index 21cb550f795..986970dc3c4 100644 --- a/bundles/org.openhab.core.ui/src/main/resources/OH-INF/i18n/chart.properties +++ b/bundles/org.openhab.core.ui/src/main/resources/OH-INF/i18n/chart.properties @@ -6,3 +6,5 @@ system.config.chart.scale.label = Scale system.config.chart.scale.description = Defines the scale to apply to the requested chart height (0.5 to divide by 2 for example). system.config.chart.maxWidth.label = Maximum Width system.config.chart.maxWidth.description = Defines the maximum width in pixels for the chart to build. + +service.system.chart.label = Charts diff --git a/bundles/org.openhab.core.voice/src/main/resources/OH-INF/i18n/voice.properties b/bundles/org.openhab.core.voice/src/main/resources/OH-INF/i18n/voice.properties index 61da8eeaeb0..5ed7f79ebb8 100644 --- a/bundles/org.openhab.core.voice/src/main/resources/OH-INF/i18n/voice.properties +++ b/bundles/org.openhab.core.voice/src/main/resources/OH-INF/i18n/voice.properties @@ -12,3 +12,5 @@ system.config.voice.keyword.label = Magic Word system.config.voice.keyword.description = The magic word to spot before initiating a dialog. system.config.voice.listeningItem.label = Listening Switch system.config.voice.listeningItem.description = If provided, the item will be switched on during the period when the dialog processor has spotted the keyword and is listening for commands. + +service.system.voice.label = Voice diff --git a/bundles/org.openhab.core/src/main/resources/OH-INF/i18n/i18n.properties b/bundles/org.openhab.core/src/main/resources/OH-INF/i18n/i18n.properties index 3ea7823479b..d0f6cb296bb 100644 --- a/bundles/org.openhab.core/src/main/resources/OH-INF/i18n/i18n.properties +++ b/bundles/org.openhab.core/src/main/resources/OH-INF/i18n/i18n.properties @@ -14,3 +14,5 @@ system.config.i18n.measurementSystem.label = Measurement System system.config.i18n.measurementSystem.description = The measurement system is used for unit conversion. system.config.i18n.measurementSystem.option.SI = Metric system.config.i18n.measurementSystem.option.US = Imperial (US) + +service.system.i18n.label = Regional Settings diff --git a/bundles/org.openhab.core/src/main/resources/OH-INF/i18n/network.properties b/bundles/org.openhab.core/src/main/resources/OH-INF/i18n/network.properties index c32adcfc210..3ca0eaa71d7 100644 --- a/bundles/org.openhab.core/src/main/resources/OH-INF/i18n/network.properties +++ b/bundles/org.openhab.core/src/main/resources/OH-INF/i18n/network.properties @@ -6,3 +6,5 @@ system.config.network.useOnlyOneAddress.label = Single IP Address per Interface system.config.network.useOnlyOneAddress.description = Use only one IP address per interface and family. system.config.network.useIPv6.label = Use IPv6 system.config.network.useIPv6.description = Use IPv6 Addresses if available. + +service.system.network.label = Network Settings