cancel
Showing results for 
Search instead for 
Did you mean: 

Reorder indexes of languages/translation

nico23
Senior III

I have four languagese/translation with their translations in TouchGFX Designer

nico23_0-1762422651088.png

Is there a way to reorder them? I would like to have IT GB ESP FR

1 ACCEPTED SOLUTION

Accepted Solutions

Yes, that was the solution.

A simple Python script that reorders the texts.xml according to my necessity works just fine. The Designer picks up the updated texts.xml file with the new ordering.

Below is the code if someone needs it in the future.

import xml.etree.ElementTree as ET
from xml.dom import minidom

def reorder_languages(input_file, output_file):
    """
    Reorders languages and translations from GB, IT, FR, ESP to IT, GB, ESP, FR
    """
    # Define the new order
    new_order = ['IT', 'GB', 'ESP', 'FR']
    
    # Parse the XML file
    tree = ET.parse(input_file)
    root = tree.getroot()
    
    # Find and reorder Languages section
    languages = root.find('Languages')
    if languages is not None:
        # Get all Language elements
        lang_elements = list(languages.findall('Language'))
        
        # Create a dictionary for easy lookup
        lang_dict = {elem.get('Id'): elem for elem in lang_elements}
        
        # Clear existing languages
        languages.clear()
        
        # Add languages in new order
        for lang_id in new_order:
            if lang_id in lang_dict:
                languages.append(lang_dict[lang_id])
    
    # Find and reorder Translations in all Text elements
    texts = root.find('Texts')
    if texts is not None:
        for text_group in texts.findall('.//TextGroup'):
            for text in text_group.findall('Text'):
                # Get all Translation elements
                translations = list(text.findall('Translation'))
                
                # Create a dictionary for easy lookup
                trans_dict = {elem.get('Language'): elem for elem in translations}
                
                # Clear existing translations
                for trans in translations:
                    text.remove(trans)
                
                # Add translations in new order
                for lang_id in new_order:
                    if lang_id in trans_dict:
                        text.append(trans_dict[lang_id])
    
    # Convert to string with proper formatting
    xml_str = ET.tostring(root, encoding='utf-8')
    
    # Pretty print
    dom = minidom.parseString(xml_str)
    pretty_xml = dom.toprettyxml(indent="  ", encoding='utf-8')
    
    # Remove extra blank lines
    lines = pretty_xml.decode('utf-8').split('\n')
    lines = [line for line in lines if line.strip()]
    
    # Write to output file
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write('\n'.join(lines))
    
    print(f"Successfully reordered languages in {output_file}")
    print(f"New order: {' -> '.join(new_order)}")

if __name__ == "__main__":
    # Usage
    input_file = "texts.xml"
    output_file = "output.xml"  # Change to your desired output file name
    
    reorder_languages(input_file, output_file)

 

View solution in original post

7 REPLIES 7
nico23
Senior III

It doesn't seem to be an option to do that (correct me if I'm wrong), but, because the text file is just an XML, a simple Python script that reorders, according to your input, the sequence of the languages is a solution.

mathiasmarkussen
ST Employee

By right-clicking on the column, you can both rename and delete the column:

mathiasmarkussen_0-1762430735392.png

mathiasmarkussen_1-1762430739530.png

The only constraint is that you cannot have two columns of the same name.

If I'm understanding right, this would just rename the column. If I already have content in the column (so translations), it won't change accordingly

I need to reorder the translations, not just rename the column

Or, am I understanding incorrectly?

mathiasmarkussen
ST Employee

It is true that there is no user interface way of doing this. My testing shows that reordering in texts.xml does change the order of the columns in the user interface.

Why is the order in the interface so important to you?

Visualization purpose when scanning the array of languages. I could remap them in code, but it would add more useless headroom.

mathiasmarkussen
ST Employee

I see.

Have you tried reordering texts.xml and seeing if that reorders them in Designer as well? That seems to be the case on my side.

Yes, that was the solution.

A simple Python script that reorders the texts.xml according to my necessity works just fine. The Designer picks up the updated texts.xml file with the new ordering.

Below is the code if someone needs it in the future.

import xml.etree.ElementTree as ET
from xml.dom import minidom

def reorder_languages(input_file, output_file):
    """
    Reorders languages and translations from GB, IT, FR, ESP to IT, GB, ESP, FR
    """
    # Define the new order
    new_order = ['IT', 'GB', 'ESP', 'FR']
    
    # Parse the XML file
    tree = ET.parse(input_file)
    root = tree.getroot()
    
    # Find and reorder Languages section
    languages = root.find('Languages')
    if languages is not None:
        # Get all Language elements
        lang_elements = list(languages.findall('Language'))
        
        # Create a dictionary for easy lookup
        lang_dict = {elem.get('Id'): elem for elem in lang_elements}
        
        # Clear existing languages
        languages.clear()
        
        # Add languages in new order
        for lang_id in new_order:
            if lang_id in lang_dict:
                languages.append(lang_dict[lang_id])
    
    # Find and reorder Translations in all Text elements
    texts = root.find('Texts')
    if texts is not None:
        for text_group in texts.findall('.//TextGroup'):
            for text in text_group.findall('Text'):
                # Get all Translation elements
                translations = list(text.findall('Translation'))
                
                # Create a dictionary for easy lookup
                trans_dict = {elem.get('Language'): elem for elem in translations}
                
                # Clear existing translations
                for trans in translations:
                    text.remove(trans)
                
                # Add translations in new order
                for lang_id in new_order:
                    if lang_id in trans_dict:
                        text.append(trans_dict[lang_id])
    
    # Convert to string with proper formatting
    xml_str = ET.tostring(root, encoding='utf-8')
    
    # Pretty print
    dom = minidom.parseString(xml_str)
    pretty_xml = dom.toprettyxml(indent="  ", encoding='utf-8')
    
    # Remove extra blank lines
    lines = pretty_xml.decode('utf-8').split('\n')
    lines = [line for line in lines if line.strip()]
    
    # Write to output file
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write('\n'.join(lines))
    
    print(f"Successfully reordered languages in {output_file}")
    print(f"New order: {' -> '.join(new_order)}")

if __name__ == "__main__":
    # Usage
    input_file = "texts.xml"
    output_file = "output.xml"  # Change to your desired output file name
    
    reorder_languages(input_file, output_file)