Jump to content

Locale string syntax checker


Recommended Posts

Hello everyone,

Since I've decided to remove all the Korean texts from my source, I wrote a really simple python program that allow to check if the syntax of the locale_string.txt is correct.
As you know, just one misplaced comma can essentially screw up your locale_string.

How it works is really easy to understand, however I'm attaching a screenshot to remove any doubts.
I hope this can be useful to someone.

Screenshot

import re
import tkinter as tk
from tkinter import filedialog

def check_format(string):
    pattern = r'^"[^"]+";$'
    return re.match(pattern, string)

def check_file():
    file_path = filedialog.askopenfilename(filetypes=[('Text files', '*.txt')])
    if file_path:
        count_respect_format = 0
        count_do_not_respect_format = 0
        invalid_strings = []

        with open(file_path, 'r') as file:
            content = file.read()
            pairs = content.split('\n\n')
            for pair in pairs:
                string1, string2 = pair.strip().split('\n')
                if check_format(string1.strip()) and check_format(string2.strip()):
                    count_respect_format += 1
                else:
                    count_do_not_respect_format += 1
                    invalid_strings.append((string1.strip(), string2.strip()))

        result_text.delete('1.0', 'end')
        result_text.insert('end', f"Number of pairs that respect the format: {count_respect_format}\n"
                                  f"Number of pairs that do not respect the format: {count_do_not_respect_format}\n\n")

        if count_do_not_respect_format > 0:
            result_text.insert('end', "Invalid strings:\n\n")
            for invalid_string in invalid_strings:
                result_text.insert('end', f"{invalid_string[0]}\n{invalid_string[1]}\n\n")
    else:
        result_text.delete('1.0', 'end')
        result_text.insert('end', "No file selected.")

# Window gui
window = tk.Tk()
window.title("Locale String Format Checker - Exodus v.1.0.0")
window.geometry("500x300")
window.resizable(False, False)

# button
select_button = tk.Button(window, text="Select File", command=check_file)
select_button.pack()

# Widget used for whow text
result_text = tk.Text(window)
result_text.pack()

## 
window.mainloop()
Edited by Metin2 Dev International
Core X - External 2 Internal
  • Metin2 Dev 2
  • Good 3
  • Love 1
  • Love 1
Link to comment
Share on other sites

def check_file():
	file_path = filedialog.askopenfilename(filetypes=[('Text files', '*.txt')])
	if file_path:
		count_respect_format = 0
		count_do_not_respect_format = 0
		invalid_strings = []

		with open(file_path, 'r') as file:
			content = file.read()
			pairs = content.split('\n\n')
			for pair in pairs:
				strings = pair.strip().split('\n')
				if len(strings) == 2:
					string1, string2 = strings
					if check_format(string1.strip()) and check_format(string2.strip()):
						count_respect_format += 1
					else:
						count_do_not_respect_format += 1
						invalid_strings.append((string1.strip(), string2.strip()))
				else:
					print(f"Skipping pair: {pair}")

		result_text.delete('1.0', 'end')
		result_text.insert('end', f"Number of pairs that respect the format: {count_respect_format}\n"
								  f"Number of pairs that do not respect the format: {count_do_not_respect_format}\n\n")

		if count_do_not_respect_format > 0:
			result_text.insert('end', "Invalid strings:\n\n")
			for invalid_string in invalid_strings:
				result_text.insert('end', f"{invalid_string[0]}\n{invalid_string[1]}\n\n")
	else:
		result_text.delete('1.0', 'end')
		result_text.insert('end', "No file selected.")

 

  • Good 1
  • Love 1
Link to comment
Share on other sites

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.