From 3ad25d695af2b95d16a29c57a90959160d22b33e Mon Sep 17 00:00:00 2001 From: lolodomo Date: Mon, 19 Aug 2024 09:19:11 +0200 Subject: [PATCH] [freeboxos] Add translatable labels for discovered things (#17287) * [freeboxos] Add translatable labels for discovered things Signed-off-by: Laurent Garnier --- .../config/PhoneConfigurationBuilder.java | 4 ++-- .../discovery/FreeboxOsDiscoveryService.java | 23 +++++++++++++++---- .../OH-INF/i18n/freeboxos.properties | 9 ++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/config/PhoneConfigurationBuilder.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/config/PhoneConfigurationBuilder.java index a474b4c59a3..5dd1fcfe74c 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/config/PhoneConfigurationBuilder.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/config/PhoneConfigurationBuilder.java @@ -46,10 +46,10 @@ public class PhoneConfigurationBuilder { String label; if (Type.DECT.equals(config.type())) { thingUID = new ThingUID(THING_TYPE_DECT, bridgeUID, Integer.toString(config.id())); - label = "DECT Phone"; + label = "@text/discovery.dect.label"; } else { thingUID = new ThingUID(THING_TYPE_FXS, bridgeUID, Integer.toString(config.id())); - label = "Landline Phone"; + label = "@text/discovery.fxs.label"; } logger.debug("Adding new Freebox Phone {} to inbox", thingUID); diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/discovery/FreeboxOsDiscoveryService.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/discovery/FreeboxOsDiscoveryService.java index aa40187ff16..c137038fef1 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/discovery/FreeboxOsDiscoveryService.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/discovery/FreeboxOsDiscoveryService.java @@ -48,11 +48,14 @@ import org.openhab.binding.freeboxos.internal.handler.FreeboxOsHandler; import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService; import org.openhab.core.config.discovery.DiscoveryResult; import org.openhab.core.config.discovery.DiscoveryResultBuilder; +import org.openhab.core.i18n.LocaleProvider; +import org.openhab.core.i18n.TranslationProvider; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.ThingUID; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.ServiceScope; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -83,6 +86,16 @@ public class FreeboxOsDiscoveryService extends AbstractThingHandlerDiscoveryServ DISCOVERY_TIME_SECONDS); } + @Reference(unbind = "-") + public void bindTranslationProvider(TranslationProvider translationProvider) { + this.i18nProvider = translationProvider; + } + + @Reference(unbind = "-") + public void bindLocaleProvider(LocaleProvider localeProvider) { + this.localeProvider = localeProvider; + } + @Override protected void startBackgroundDiscovery() { stopBackgroundDiscovery(); @@ -162,7 +175,7 @@ public class FreeboxOsDiscoveryService extends AbstractThingHandlerDiscoveryServ ThingUID thingUID = new ThingUID(THING_TYPE_CALL, bridgeUID, "calls"); logger.debug("Adding new Call thing {} to inbox", thingUID); DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID) - .withLabel("Phone Calls").build(); + .withLabel("@text/discovery.call.label").build(); thingDiscovered(discoveryResult); } } @@ -181,7 +194,8 @@ public class FreeboxOsDiscoveryService extends AbstractThingHandlerDiscoveryServ bridgeUID, mac.toHexString(false)); logger.debug("Adding new Freebox Network Host {} to inbox", thingUID); DiscoveryResultBuilder builder = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID) - .withLabel(lanHost.getPrimaryName().orElse("Network Device %s".formatted(macString))) + .withLabel(lanHost.getPrimaryName() + .orElse("@text/discovery.network-device.label [ \"%s\" ]".formatted(macString))) .withTTL(300).withProperty(Thing.PROPERTY_MAC_ADDRESS, macString) .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS); thingDiscovered(builder.build()); @@ -201,7 +215,8 @@ public class FreeboxOsDiscoveryService extends AbstractThingHandlerDiscoveryServ logger.debug("Adding new VM Device {} to inbox", thingUID); DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID) .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS) - .withLabel("%s (VM)".formatted(vm.name())).withProperty(ClientConfiguration.ID, vm.id()) + .withLabel("@text/discovery.vm.label [ \"%s\" ]".formatted(vm.name())) + .withProperty(ClientConfiguration.ID, vm.id()) .withProperty(Thing.PROPERTY_MAC_ADDRESS, mac.toColonDelimitedString()).build(); thingDiscovered(discoveryResult); }); @@ -219,7 +234,7 @@ public class FreeboxOsDiscoveryService extends AbstractThingHandlerDiscoveryServ ThingUID thingUID = new ThingUID(THING_TYPE_REPEATER, bridgeUID, Integer.toString(repeater.id())); DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID) - .withLabel("Repeater %s".formatted(repeater.name())) + .withLabel("@text/discovery.repeater.label [ \"%s\" ]".formatted(repeater.name())) .withProperty(Thing.PROPERTY_MAC_ADDRESS, mac.toColonDelimitedString()) .withProperty(ClientConfiguration.ID, repeater.id()) .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).build(); diff --git a/bundles/org.openhab.binding.freeboxos/src/main/resources/OH-INF/i18n/freeboxos.properties b/bundles/org.openhab.binding.freeboxos/src/main/resources/OH-INF/i18n/freeboxos.properties index fbe6c125853..1cd0d21c1f7 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/resources/OH-INF/i18n/freeboxos.properties +++ b/bundles/org.openhab.binding.freeboxos/src/main/resources/OH-INF/i18n/freeboxos.properties @@ -385,6 +385,15 @@ info-host-not-reachable = Host is not reachable info-player-not-reachable = Player is not reachable info-vm-not-running = VM is not running +# discovery result + +discovery.call.label = Phone Calls +discovery.dect.label = DECT Phone +discovery.fxs.label = Landline Phone +discovery.network-device.label = Network Device {0} +discovery.repeater.label = Repeater {0} +discovery.vm.label = Virtual Machine {0} + # iconprovider iconset.label = FreeboxOS Icons