(self)
Convert this:
            id1: en => 'yes', sp => 'si'
            id2: en => 'yes', sp => 'si'
            id3: en => 'if', sp => 'si'

        to this:
            id1: en => 'yes', sp => 'si'
            id3: en => 'if', sp => 'si'

        and rename the appropriate label references.
Convert this:
            id1: en => 'yes', sp => 'si'
            id2: en => 'yes', sp => 'si'
            id3: en => 'if', sp => 'si'
Convert this : id1 : en = > yes sp = > si id2 : en = > yes sp = > si id3 : en = > if sp = > si
def normalize_itext(self):
        """
        Convert this:
            id1: en => 'yes', sp => 'si'
            id2: en => 'yes', sp => 'si'
            id3: en => 'if', sp => 'si'

        to this:
            id1: en => 'yes', sp => 'si'
            id3: en => 'if', sp => 'si'

        and rename the appropriate label references.
        """
        translations = self.translations()
        node_groups = self._itext_node_groups()

        duplicate_dict = defaultdict(list)
        for g in node_groups.values():
            duplicate_dict[g].append(g)

        duplicates = [sorted(g, key=lambda ng: ng.id) for g in duplicate_dict.values() if len(g) > 1]

        for dup in duplicates:
            for group in dup[1:]:
                itext_ref = '{{f}}text[@id="{0}"]'.format(group.id)
                for lang in group.nodes.keys():
                    translation = translations[lang]
                    node = translation.find(itext_ref)
                    translation.remove(node.xml)

        def replace_ref_s(xmlstring, find, replace):
            find = find.encode('utf-8', 'xmlcharrefreplace')
            replace = replace.encode('utf-8', 'xmlcharrefreplace')
            return xmlstring.replace(find, replace)

        xf_string = self.render()
        for dup in duplicates:
            reference = dup[0]
            new_ref = "jr:itext('{0}')".format(reference.id)

            for group in dup[1:]:
                old_ref = 'jr:itext(\'{0}\')'.format(group.id)
                xf_string = replace_ref_s(xf_string, old_ref, new_ref)

        self.xml = parse_xml(xf_string)

        self._reset_translations_cache()
def normalize_itext ( self ) : translations = self . translations ( ) node_groups = self . _itext_node_groups ( ) duplicate_dict = defaultdict ( list ) for g in node_groups . values ( ) : duplicate_dict [ g ] . append ( g ) duplicates = [ sorted ( g , key = lambda ng : ng . id ) for g in duplicate_dict . values ( ) if len ( g ) > 1 ] for dup in duplicates : for group in dup [ 1 : ] : itext_ref = '{{f}}text[@id="{0}"]' . format ( group . id ) for lang in group . nodes . keys ( ) : translation = translations [ lang ] node = translation . find ( itext_ref ) translation . remove ( node . xml ) def replace_ref_s ( xmlstring , find , replace ) : find = find . encode ( 'utf-8' , 'xmlcharrefreplace' ) replace = replace . encode ( 'utf-8' , 'xmlcharrefreplace' ) return xmlstring . replace ( find , replace ) xf_string = self . render ( ) for dup in duplicates : reference = dup [ 0 ] new_ref = "jr:itext('{0}')" . format ( reference . id ) for group in dup [ 1 : ] : old_ref = 'jr:itext(\'{0}\')' . format ( group . id ) xf_string = replace_ref_s ( xf_string , old_ref , new_ref ) self . xml = parse_xml ( xf_string ) self . _reset_translations_cache ( )