-
Notifications
You must be signed in to change notification settings - Fork 782
[i18n] Added translation feature for label and description of channels inside a channel-group-type #5751
[i18n] Added translation feature for label and description of channels inside a channel-group-type #5751
Conversation
String description = thingTypeI18nUtil.getChannelGroupDescription(bundle, channelGroupTypeUID, | ||
channelGroupType.getDescription(), locale); | ||
|
||
List<ChannelDefinition> localizedChannelDefinitions = new ArrayList<>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lines 119-142 are almost identical to 154-176 but I could not find a way to extract a method for them. Any thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Difficult to identify the problem in the Github diff view only. What's the problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maggu2810 Thanks for your review.
This is the duplicate code which I want to extract into a new method. The problem is that I want to determine the channelLabel
and channelDescription
inside the foreach loop derived from either the thingTypeUID
or the channelGroupTypeUID
- I marked the code with **.
private List<ChannelDefinition> createLocalizedChannelDefinitions(Bundle bundle,
List<ChannelDefinition> channelDefinitions, Locale locale) {
// start
List<ChannelDefinition> localizedChannelDefinitions = new ArrayList<>(channelDefinitions.size());
for (ChannelDefinition channelDefinition : channelDefinitions) {
String channelLabel = thingTypeI18nUtil.getChannelLabel(bundle, **<thingTypeUID|channelGroupTypeUID>**, channelDefinition,
channelDefinition.getLabel(), locale);
String channelDescription = thingTypeI18nUtil.getChannelDescription(bundle, **<thingTypeUID|channelGroupTypeUID>**, channelDefinition,
channelDefinition.getDescription(), locale);
if (channelLabel == null || channelDescription == null) {
ChannelTypeUID channelTypeUID = channelDefinition.getChannelTypeUID();
ChannelType channelType = channelTypeRegistry.getChannelType(channelTypeUID, locale);
if (channelType != null) {
if (channelLabel == null) {
channelLabel = thingTypeI18nUtil.getChannelLabel(bundle, channelTypeUID, channelType.getLabel(),
locale);
}
if (channelDescription == null) {
channelDescription = thingTypeI18nUtil.getChannelDescription(bundle, channelTypeUID,
channelType.getDescription(), locale);
}
}
}
localizedChannelDefinitions
.add(new ChannelDefinition(channelDefinition.getId(), channelDefinition.getChannelTypeUID(),
channelDefinition.getProperties(), channelLabel, channelDescription));
}
// end
return localizedChannelDefinitions;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
diff --git a/bundles/core/org.eclipse.smarthome.core.thing/src/main/java/org/eclipse/smarthome/core/thing/i18n/ThingTypeI18nLocalizationService.java b/bundles/core/org.eclipse.smarthome.core.thing/src/main/java/org/eclipse/smarthome/core/thing/i18n/ThingTypeI18nLocalizationService.java
index df5c1c14fb..2a2b50c766 100644
--- a/bundles/core/org.eclipse.smarthome.core.thing/src/main/java/org/eclipse/smarthome/core/thing/i18n/ThingTypeI18nLocalizationService.java
+++ b/bundles/core/org.eclipse.smarthome.core.thing/src/main/java/org/eclipse/smarthome/core/thing/i18n/ThingTypeI18nLocalizationService.java
@@ -15,6 +15,7 @@ package org.eclipse.smarthome.core.thing.i18n;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
+import java.util.function.Function;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@@ -116,31 +117,12 @@ public class ThingTypeI18nLocalizationService {
String description = thingTypeI18nUtil.getChannelGroupDescription(bundle, channelGroupTypeUID,
channelGroupType.getDescription(), locale);
- List<ChannelDefinition> localizedChannelDefinitions = new ArrayList<>(
- channelGroupType.getChannelDefinitions().size());
- for (ChannelDefinition channelDefinition : channelGroupType.getChannelDefinitions()) {
- String channelLabel = thingTypeI18nUtil.getChannelLabel(bundle, channelGroupTypeUID, channelDefinition,
- channelDefinition.getLabel(), locale);
- String channelDescription = thingTypeI18nUtil.getChannelDescription(bundle, channelGroupTypeUID,
- channelDefinition, channelDefinition.getDescription(), locale);
- if (channelLabel == null || channelDescription == null) {
- ChannelTypeUID channelTypeUID = channelDefinition.getChannelTypeUID();
- ChannelType channelType = channelTypeRegistry.getChannelType(channelTypeUID, locale);
- if (channelType != null) {
- if (channelLabel == null) {
- channelLabel = thingTypeI18nUtil.getChannelLabel(bundle, channelTypeUID, channelType.getLabel(),
- locale);
- }
- if (channelDescription == null) {
- channelDescription = thingTypeI18nUtil.getChannelDescription(bundle, channelTypeUID,
- channelType.getDescription(), locale);
- }
- }
- }
- localizedChannelDefinitions
- .add(new ChannelDefinition(channelDefinition.getId(), channelDefinition.getChannelTypeUID(),
- channelDefinition.getProperties(), channelLabel, channelDescription));
- }
+ List<ChannelDefinition> localizedChannelDefinitions = localizeChannelDefinitions(bundle, channelGroupType.getChannelDefinitions(),
+ channelDefinition -> thingTypeI18nUtil.getChannelLabel(bundle, channelGroupTypeUID, channelDefinition,
+ channelDefinition.getLabel(), locale),
+ channelDefinition -> thingTypeI18nUtil.getChannelDescription(bundle, channelGroupTypeUID,
+ channelDefinition, channelDefinition.getDescription(), locale),
+ locale);
return new ChannelGroupType(channelGroupTypeUID, channelGroupType.isAdvanced(), label, description,
channelGroupType.getCategory(), localizedChannelDefinitions);
@@ -151,30 +133,12 @@ public class ThingTypeI18nLocalizationService {
String label = thingTypeI18nUtil.getLabel(bundle, thingTypeUID, thingType.getLabel(), locale);
String description = thingTypeI18nUtil.getDescription(bundle, thingTypeUID, thingType.getDescription(), locale);
- List<ChannelDefinition> localizedChannelDefinitions = new ArrayList<>(thingType.getChannelDefinitions().size());
- for (ChannelDefinition channelDefinition : thingType.getChannelDefinitions()) {
- String channelLabel = thingTypeI18nUtil.getChannelLabel(bundle, thingTypeUID, channelDefinition,
- channelDefinition.getLabel(), locale);
- String channelDescription = thingTypeI18nUtil.getChannelDescription(bundle, thingTypeUID, channelDefinition,
- channelDefinition.getDescription(), locale);
- if (channelLabel == null || channelDescription == null) {
- ChannelTypeUID channelTypeUID = channelDefinition.getChannelTypeUID();
- ChannelType channelType = channelTypeRegistry.getChannelType(channelTypeUID, locale);
- if (channelType != null) {
- if (channelLabel == null) {
- channelLabel = thingTypeI18nUtil.getChannelLabel(bundle, channelTypeUID, channelType.getLabel(),
- locale);
- }
- if (channelDescription == null) {
- channelDescription = thingTypeI18nUtil.getChannelDescription(bundle, channelTypeUID,
- channelType.getDescription(), locale);
- }
- }
- }
- localizedChannelDefinitions
- .add(new ChannelDefinition(channelDefinition.getId(), channelDefinition.getChannelTypeUID(),
- channelDefinition.getProperties(), channelLabel, channelDescription));
- }
+ List<ChannelDefinition> localizedChannelDefinitions = localizeChannelDefinitions(bundle, thingType.getChannelDefinitions(),
+ channelDefinition -> thingTypeI18nUtil.getChannelLabel(bundle, thingTypeUID, channelDefinition,
+ channelDefinition.getLabel(), locale),
+ channelDefinition -> thingTypeI18nUtil.getChannelDescription(bundle, thingTypeUID, channelDefinition,
+ channelDefinition.getDescription(), locale),
+ locale);
List<ChannelGroupDefinition> localizedChannelGroupDefinitions = new ArrayList<>(
thingType.getChannelGroupDefinitions().size());
@@ -213,4 +177,32 @@ public class ThingTypeI18nLocalizationService {
return builder.build();
}
+ public <T> List<ChannelDefinition> localizeChannelDefinitions(final Bundle bundle, final List<ChannelDefinition> channelDefinitions,
+ final Function<ChannelDefinition, @Nullable String> channelLabelResolver,
+ final Function<ChannelDefinition, @Nullable String> channelDescriptionResolver, final Locale locale) {
+ List<ChannelDefinition> localizedChannelDefinitions = new ArrayList<>(channelDefinitions.size());
+ for (ChannelDefinition channelDefinition : channelDefinitions) {
+ String channelLabel = channelLabelResolver.apply(channelDefinition);
+ String channelDescription = channelDescriptionResolver.apply(channelDefinition);
+ if (channelLabel == null || channelDescription == null) {
+ ChannelTypeUID channelTypeUID = channelDefinition.getChannelTypeUID();
+ ChannelType channelType = channelTypeRegistry.getChannelType(channelTypeUID, locale);
+ if (channelType != null) {
+ if (channelLabel == null) {
+ channelLabel = thingTypeI18nUtil.getChannelLabel(bundle, channelTypeUID, channelType.getLabel(),
+ locale);
+ }
+ if (channelDescription == null) {
+ channelDescription = thingTypeI18nUtil.getChannelDescription(bundle, channelTypeUID,
+ channelType.getDescription(), locale);
+ }
+ }
+ }
+ localizedChannelDefinitions
+ .add(new ChannelDefinition(channelDefinition.getId(), channelDefinition.getChannelTypeUID(),
+ channelDefinition.getProperties(), channelLabel, channelDescription));
+ }
+ return localizedChannelDefinitions;
+ }
+
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice. Works like a charm. Thank you very much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I will leave it to another committer to press the merge button 😉
Just for my understanding: what is wrong exactly ? I am running the openHAB snapshot 1302 and I get the correct translations in French in Paper UI with the WU binding. |
@lolodomo The weather binding does not contain an example for this. Try to translate the label and description in |
Ok I see the difference now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
String description = thingTypeI18nUtil.getChannelGroupDescription(bundle, channelGroupTypeUID, | ||
channelGroupType.getDescription(), locale); | ||
|
||
List<ChannelDefinition> localizedChannelDefinitions = new ArrayList<>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Difficult to identify the problem in the Github diff view only. What's the problem?
…nel-group-type Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
@maggu2810 Sry to break your approval. I had to rebase because of #5864. Thus I used the opportunity to switch to the new |
@cweitkamp I agree with the |
@triller-telekom WRT to the builder, would you like to have a look at, too? |
I have added annotations in my i18n part and switched to the new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just had a look at the builder and except for one typo it looks good to me.
/** | ||
* Specify whether this is an advanced {@link ChannelGroupType}, default is false | ||
* | ||
* @param advanced true is this is an advanced {@link ChannelGroupType} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: "if" this is an...
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
@maggu2810 The typo I found is fixed now, feel free to merge if travis agrees :) |
Upps... Thanks, fixed now with #5883 :) |
Thank you, too. |
…s inside a channel-group-type (#5751) Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
@maggu2810 Thanks |
channel-group-type
ThingTypeI18nLocalizationService
ChannelTypeBuilder
APIChannelGroupTypeBuilder
See https://github.com/openhab/openhab2-addons/pull/3678#issuecomment-397965219
Signed-off-by: Christoph Weitkamp github@christophweitkamp.de