Move po2lex to use polib and (Lex2Po.py) add more info to entries in .lex file not found in _en file
This commit is contained in:
parent
aa70bcaa46
commit
d56452ead1
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.log
|
@ -9,7 +9,7 @@ SOURCE_LOCALE_DIR = Path("/home/brianr/Documents/smeserver-manager-locale/root/u
|
|||||||
SOURCE_MANAGER_DIR = Path("/home/brianr/Documents/smeserver-manager/root/usr/share/smanager/lib/SrvMngr/I18N/Modules")
|
SOURCE_MANAGER_DIR = Path("/home/brianr/Documents/smeserver-manager/root/usr/share/smanager/lib/SrvMngr/I18N/Modules")
|
||||||
|
|
||||||
def extract_language_from_filename(filename):
|
def extract_language_from_filename(filename):
|
||||||
match = re.search(r'[_-]([a-z]{2}(?:-[a-z]{2})?)\.lex', filename, re.IGNORECASE)
|
match = re.search(r'[_-]([a-z]{2}(?:-[a-z]{2})?)\..*', filename, re.IGNORECASE)
|
||||||
if match:
|
if match:
|
||||||
return match.group(1).replace("-", "_").lower()
|
return match.group(1).replace("-", "_").lower()
|
||||||
return None
|
return None
|
||||||
@ -101,7 +101,7 @@ def convert_lex_to_po(lex_file, po_file, en_entries, general_en_entries,IsGenera
|
|||||||
for msgctxt, msgstr in translated_entries.items():
|
for msgctxt, msgstr in translated_entries.items():
|
||||||
msgid = en_entries.get(msgctxt, "") # Find the original text using msgctxt (Msg ID)
|
msgid = en_entries.get(msgctxt, "") # Find the original text using msgctxt (Msg ID)
|
||||||
if not msgid:
|
if not msgid:
|
||||||
print(f"Warning: Could not find original text for Msg ID '{msgctxt}'")
|
print(f"Warning: Could not find original text for Msg ID '{msgctxt} ({language_code})")
|
||||||
# See if in General
|
# See if in General
|
||||||
msgid = general_en_entries.get(msgctxt, "")
|
msgid = general_en_entries.get(msgctxt, "")
|
||||||
if not msgid:
|
if not msgid:
|
||||||
|
62
Po2Lex.py
62
Po2Lex.py
@ -1,32 +1,18 @@
|
|||||||
import re
|
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import polib
|
||||||
|
|
||||||
# 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):
|
||||||
try:
|
try:
|
||||||
with open(file_name, "r") as file:
|
po = polib.pofile(file_name)
|
||||||
content = file.read()
|
result = []
|
||||||
|
for entry in po:
|
||||||
entries = re.findall(r"(msgctxt\s+\".+?\"\nmsgid\s+\".+?\"\nmsgstr\s+\".+?\")", content, re.DOTALL)
|
result.append({"msgctxt": entry.msgctxt, "msgid": entry.msgid, "msgstr": entry.msgstr})
|
||||||
|
return result
|
||||||
result = []
|
except Exception as e:
|
||||||
for entry in entries:
|
print(f"Process_po_file Exception {e} in {file_name}")
|
||||||
lines = entry.split("\n")
|
quit()
|
||||||
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
|
|
||||||
except Exception as e:
|
|
||||||
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):
|
||||||
@ -39,18 +25,16 @@ SOURCE_BASE_DIR = Path("/home/brianr/Documents/smeserver-manager-locale/root/usr
|
|||||||
|
|
||||||
# Iterate over subdirectories in SOURCE_BASE_DIR
|
# Iterate over subdirectories in SOURCE_BASE_DIR
|
||||||
for subdir in [subdir.path for subdir in os.scandir(SOURCE_BASE_DIR) if subdir.is_dir()]:
|
for subdir in [subdir.path for subdir in os.scandir(SOURCE_BASE_DIR) if subdir.is_dir()]:
|
||||||
module_name = Path(subdir).name
|
module_name = Path(subdir).name
|
||||||
print(f"Processing {module_name}")
|
if module_name == 'Backup':
|
||||||
po_dir = Path(os.path.join(subdir, "pofiles"))
|
print(f"Processing {module_name}")
|
||||||
#print(po_dir)
|
po_dir = Path(os.path.join(subdir, "pofiles"))
|
||||||
lex_dir = Path(os.path.join(subdir, "newlex"))
|
lex_dir = Path(os.path.join(subdir, "newlex"))
|
||||||
#print(lex_dir)
|
# Ensure the newlex directory exists
|
||||||
# Ensure the newlex directory exists
|
lex_dir.mkdir(parents=True, exist_ok=True)
|
||||||
lex_dir.mkdir(parents=True, exist_ok=True)
|
for file in os.listdir(po_dir):
|
||||||
|
if file.endswith(".po"):
|
||||||
for file in os.listdir(po_dir):
|
po_file_path = Path(os.path.join(po_dir, file))
|
||||||
if file.endswith(".po"):
|
translations = process_po_file(po_file_path)
|
||||||
po_file_path = Path(os.path.join(po_dir, file))
|
lex_file_name = lex_dir / (po_file_path.stem + ".lex")
|
||||||
translations = process_po_file(po_file_path)
|
create_lex_file(translations, lex_file_name)
|
||||||
lex_file_name = lex_dir / (po_file_path.stem + ".lex")
|
|
||||||
create_lex_file(translations, lex_file_name)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user