Update Po2Lex - still WIP

This commit is contained in:
Brian Read 2024-07-19 10:27:11 +01:00
parent 670264ec6b
commit aa70bcaa46

View File

@ -1,29 +1,32 @@
import os
import re import re
import os
from pathlib import Path from pathlib import Path
# Function to read and process .po file # Function to read and process .po file
def process_po_file(file_name): def process_po_file(file_name):
with open(file_name, "r") as file: try:
content = file.read() with open(file_name, "r") as file:
content = file.read()
entries = re.findall(r"(msgctxt\s+\".+?\"\nmsgid\s+\".+?\"\nmsgstr\s+\".+?\")", content, re.DOTALL) entries = re.findall(r"(msgctxt\s+\".+?\"\nmsgid\s+\".+?\"\nmsgstr\s+\".+?\")", content, re.DOTALL)
result = [] result = []
for entry in entries: for entry in entries:
lines = entry.split("\n") 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_line = lines[0] msgctxt = re.findall(r"msgctxt\s+\"(.+?)\"", msgctxt_line)[0]
msgid_lines = [line for line in lines if line.startswith("msgid")] msgid = " ".join(re.findall(r"msgid\s+\"(.+?)\"", "\n".join(msgid_lines)))
msgstr_lines = [line for line in lines if line.startswith("msgstr")] msgstr = " ".join(re.findall(r"msgstr\s+\"(.+?)\"", "\n".join(msgstr_lines)))
msgctxt = re.findall(r"msgctxt\s+\"(.+?)\"", msgctxt_line)[0] result.append({"msgctxt": msgctxt, "msgid": msgid, "msgstr": msgstr})
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
except Exception as e:
return result print(f"Process_po_file Exception {e} in {file_name} {msgctxt_line}")
quit()
# Function to create .lex file # Function to create .lex file
def create_lex_file(translations, lex_file_name): def create_lex_file(translations, lex_file_name):
@ -32,22 +35,22 @@ def create_lex_file(translations, lex_file_name):
file.write(f"'{translation['msgctxt']}' => '{translation['msgstr']}',\n") file.write(f"'{translation['msgctxt']}' => '{translation['msgstr']}',\n")
# Main program # Main program
SOURCE_POFILES_DIR = Path("/home/brianr/Documents/smeserver-manager-locale/root/usr/share/smanager/lib/SrvMngr/I18N/Modules") SOURCE_BASE_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 # Iterate over subdirectories in SOURCE_BASE_DIR
TARGET_LEXFILES_DIR.mkdir(parents=True, exist_ok=True) for subdir in [subdir.path for subdir in os.scandir(SOURCE_BASE_DIR) if subdir.is_dir()]:
module_name = Path(subdir).name
print(f"Processing {module_name}")
po_dir = Path(os.path.join(subdir, "pofiles"))
#print(po_dir)
lex_dir = Path(os.path.join(subdir, "newlex"))
#print(lex_dir)
# Ensure the newlex directory exists
lex_dir.mkdir(parents=True, exist_ok=True)
# Iterate over subdirectories in SOURCE_POFILES_DIR (only one level down) for file in os.listdir(po_dir):
# Iterate over subdirectories in SOURCE_POFILES_DIR (only one level down) if file.endswith(".po"):
for subdir in [subdir.path for subdir in os.scandir(SOURCE_POFILES_DIR) if subdir.is_dir()]: po_file_path = Path(os.path.join(po_dir, file))
po_subdir_path = Path(subdir + "/pofiles") translations = process_po_file(po_file_path)
newlex_subdir_path = Path(subdir + "/newlex") lex_file_name = lex_dir / (po_file_path.stem + ".lex")
newlex_subdir_path.mkdir(parents=True, exist_ok=True) create_lex_file(translations, lex_file_name)
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)