Convert your Resx files to JSON using Python

Convert your Resx files to JSON using Python

Introduction

During the process of migrating our project to the new UI, we encountered a significant roadblock concerning the localization files. The legacy application relied on the Resx localization format, which was incompatible with the JSON format required for the new project.

Crafting the Python Script

Faced with the lack of suitable conversion tools, our team took the initiative to craft a custom Python script. This script became the perfect bridge between the old and new localization formats, allowing us to efficiently and seamlessly transition from one to the other.

Smooth Localization Experience

Thanks to the ingenious Python script, we successfully ensured a smooth localization experience in the updated application. The conversion process was made much easier, eliminating potential roadblocks and streamlining the migration to the new UI. Our team's resourcefulness and dedication played a vital role in overcoming this challenge and achieving a successful project update.

The Python Code

import os
import xml.etree.ElementTree as ET
import json

def convert_resx_to_json(resx_file):
    # Parse the .resx file as an XML tree
    xml_tree = ET.parse(resx_file)
    root = xml_tree.getroot()

    data = {}
    for data_node in root.findall(".//data"):
        # Extract the 'name' attribute as the key
        key = data_node.get("name")
        # Extract the text content of the <value> element as the value
        value = data_node.find("./value").text

        # Store the key-value pair in the 'data' dictionary
        data[key] = value

    # Create a corresponding .json file name by changing the extension from .resx to .json
    json_file = os.path.splitext(resx_file)[0] + ".json"

    # To keep your information secure and easily accessible, please save the data dictionary as a JSON file.
    with open(json_file, "w") as json_file_handle:
        json.dump(data, json_file_handle, indent=4, ensure_ascii=False)

def find_resx_files(directory):
    # This function searches for .resx files in the specified directory
    # and its subdirectories, then converts each .resx file to .json.

    for root, _, files in os.walk(directory):
        for file in files:
            # Check if the file ends with '.resx'
            if file.endswith(".resx"):
                resx_file = os.path.join(root, file)
                # Use the 'convert_resx_to_json' function
                convert_resx_to_json(resx_file)

def main():
    # Specify the path to the directory where the .resx files are located
    desktop_path = "/Users/username/resxFilePath/"
    # Replace "username" with your actual username

    # Convert all .resx files in specified folder using 'find_resx_files
    find_resx_files(desktop_path)

if __name__ == "__main__":
    # Execute the main function
    main()

From our blog

Join us on a journey of digital discovery.
Web, mobile, cybersecurity, cloud, and beyond

Incident Report: Docker Service Degradation Due to Disk Exhaustion
Incident Report: Docker Service Degradation Due to Disk Exhaustion

Detailed incident report describing Docker service degradation caused by disk exhaustion in Docker Desktop’s WSL2 virtual disk (ext4.vhdx), including impact,...

DOCKER
February 4th, 20264 mins to read
Creating High-Performing Landing Pages:Best Practices for Conversions
Creating High-Performing Landing Pages:Best Practices for Conversions

Boost your online marketing efforts with expert tips for creating high-performing landing pages. Learn how to increase your conversion rates effectively.

LANDING PAGES
October 26th, 20232 mins to read
The Power of Video Marketing:Engaging Your Audience
The Power of Video Marketing:Engaging Your Audience

Explore the Impact of Video Marketing on Audience Engagement

VIDEO MARKETING
October 24th, 20231 mins to read
AI Chatbots:Enhancing Customer Support and User Experience
AI Chatbots:Enhancing Customer Support and User Experience

In today's business landscape, customer satisfaction and providing swift responses have never been more critical. Explore how AI-powered chatbots, like those...

AI
October 19th, 20232 mins to read
Newsletter

Subcribe our newsletter

Do not miss the information from us about the trending in the market. By clicking the button, you are agreeing with our Term & Conditions