diff --git a/Po2Lex.py b/Po2Lex.py new file mode 100644 index 0000000..74546fa --- /dev/null +++ b/Po2Lex.py @@ -0,0 +1,53 @@ +import os +import re +from pathlib import Path + +# Function to read and process .po file +def process_po_file(file_name): + with open(file_name, "r") as file: + content = file.read() + + entries = re.findall(r"(msgctxt\s+\".+?\"\nmsgid\s+\".+?\"\nmsgstr\s+\".+?\")", content, re.DOTALL) + + result = [] + for entry in entries: + lines = entry.split("\n") + + msgctxt_line = lines[0] + msgid_lines = [line for line in lines if line.startswith("msgid")] + msgstr_lines = [line for line in lines if line.startswith("msgstr")] + + msgctxt = re.findall(r"msgctxt\s+\"(.+?)\"", msgctxt_line)[0] + msgid = " ".join(re.findall(r"msgid\s+\"(.+?)\"", "\n".join(msgid_lines))) + msgstr = " ".join(re.findall(r"msgstr\s+\"(.+?)\"", "\n".join(msgstr_lines))) + + result.append({"msgctxt": msgctxt, "msgid": msgid, "msgstr": msgstr}) + + return result + +# Function to create .lex file +def create_lex_file(translations, lex_file_name): + with open(lex_file_name, "w") as file: + for translation in translations: + file.write(f"'{translation['msgctxt']}' => '{translation['msgstr']}',\n") + +# Main program +SOURCE_POFILES_DIR = Path("/home/brianr/Documents/smeserver-manager-locale/root/usr/share/smanager/lib/SrvMngr/I18N/Modules") +TARGET_LEXFILES_DIR = Path("/home/brianr/Documents/smeserver-manager/root/usr/share/smanager/lib/SrvMngr/I18N/Modules") + +# ensuring the target directory exists +TARGET_LEXFILES_DIR.mkdir(parents=True, exist_ok=True) + +# Iterate over subdirectories in SOURCE_POFILES_DIR (only one level down) +# Iterate over subdirectories in SOURCE_POFILES_DIR (only one level down) +for subdir in [subdir.path for subdir in os.scandir(SOURCE_POFILES_DIR) if subdir.is_dir()]: + po_subdir_path = Path(subdir + "/pofiles") + newlex_subdir_path = Path(subdir + "/newlex") + newlex_subdir_path.mkdir(parents=True, exist_ok=True) + + for file in os.listdir(subdir): + if file.endswith(".po"): + po_file_path = Path(os.path.join(subdir, file)) + translations = process_po_file(po_file_path) + lex_file_name = newlex_subdir_path / (po_file_path.stem + ".lex") + create_lex_file(translations, lex_file_name)