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

[i18n] Added translation feature for label and description of channels inside a channel-group-type #5751

Merged
merged 6 commits into from
Jul 11, 2018

Conversation

cweitkamp
Copy link
Contributor

@cweitkamp cweitkamp commented Jun 18, 2018

  • Added translation feature for label and description of channels inside a channel-group-type
  • Refactoring for centralized handling of translations in ThingTypeI18nLocalizationService
  • Switched to ChannelTypeBuilder API
  • Introduced a ChannelGroupTypeBuilder
  • Updated documentation

See https://github.com/openhab/openhab2-addons/pull/3678#issuecomment-397965219

Signed-off-by: Christoph Weitkamp github@christophweitkamp.de

String description = thingTypeI18nUtil.getChannelGroupDescription(bundle, channelGroupTypeUID,
channelGroupType.getDescription(), locale);

List<ChannelDefinition> localizedChannelDefinitions = new ArrayList<>(
Copy link
Contributor Author

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?

Copy link
Contributor

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?

Copy link
Contributor Author

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;
    }

Copy link
Contributor

@maggu2810 maggu2810 Jun 29, 2018

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;
+    }
+
 }

Copy link
Contributor Author

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.

Copy link
Contributor

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 😉

@lolodomo
Copy link
Contributor

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.

@cweitkamp
Copy link
Contributor Author

@lolodomo
Copy link
Contributor

Ok I see the difference now.

Copy link
Contributor

@maggu2810 maggu2810 left a 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<>(
Copy link
Contributor

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>
@cweitkamp
Copy link
Contributor Author

cweitkamp commented Jul 8, 2018

@maggu2810 Sry to break your approval. I had to rebase because of #5864. Thus I used the opportunity to switch to the new ChannelTypeBuilder API. I additionally introduced a new ChannelGroupTypeBuilder API on my own.

@maggu2810
Copy link
Contributor

@cweitkamp I agree with the ChannelGroupTypeBuilder. If the code of the i18n stuff has been changed only (because of the nullness annotations), I assume my review is still valid.

@maggu2810
Copy link
Contributor

@triller-telekom WRT to the builder, would you like to have a look at, too?

@cweitkamp
Copy link
Contributor Author

If the code of the i18n stuff has been changed only (because of the nullness annotations)

I have added annotations in my i18n part and switched to the new I18nUtil.stripConstantOr() method.

Copy link
Contributor

@triller-telekom triller-telekom left a 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}
Copy link
Contributor

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>
@triller-telekom
Copy link
Contributor

@maggu2810 The typo I found is fixed now, feel free to merge if travis agrees :)

@triller-telekom
Copy link
Contributor

Upps... Thanks, fixed now with #5883 :)

@cweitkamp
Copy link
Contributor Author

Thank you, too.

@maggu2810 maggu2810 merged commit ee847b5 into eclipse-archived:master Jul 11, 2018
maggu2810 pushed a commit that referenced this pull request Jul 11, 2018
…s inside a channel-group-type (#5751)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
@cweitkamp cweitkamp deleted the bugfix-channel-i18n branch July 11, 2018 07:02
@cweitkamp
Copy link
Contributor Author

@maggu2810 Thanks

@htreu htreu added this to the 0.10.0 milestone Oct 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants