Expand Po2Lex to do all directories, expand Placeholder message if no english equiv found

This commit is contained in:
Brian Read 2024-07-21 08:29:05 +01:00
parent d56452ead1
commit b399cb1a5f
2 changed files with 31 additions and 19 deletions

View File

@ -19,9 +19,19 @@ def parse_lex_file(lex_file):
with open(lex_file, 'r', encoding='utf-8') as file: with open(lex_file, 'r', encoding='utf-8') as file:
content = file.read() content = file.read()
# Use regex to find all key-value pairs, handling quotes around keys optionally # Use regex to find all key-value pairs, handling quotes around keys optionally
# matches = re.finditer(r"""
# (['"]?) # Optional opening quote for the key
# ([^'" \t]+?) # The key itself (excluding quotes and whitespace)
# \1 # Optional closing quote matching the opening quote
# \s*=>\s* # The delimiter
# ' # Opening quote for the value
# (.*?) # The value (non-greedy to capture minimal text)
# ' # Closing quote for the value
# \s*,\s* # Trailing comma and optional whitespace
# """, content, re.DOTALL | re.VERBOSE)
matches = re.finditer(r""" matches = re.finditer(r"""
(['"]?) # Optional opening quote for the key (['"]?) # Optional opening quote for the key
([^'" \t]+?) # The key itself (excluding quotes and whitespace) (.*?) # The key itself (including spaces and any characters)
\1 # Optional closing quote matching the opening quote \1 # Optional closing quote matching the opening quote
\s*=>\s* # The delimiter \s*=>\s* # The delimiter
' # Opening quote for the value ' # Opening quote for the value
@ -29,7 +39,6 @@ def parse_lex_file(lex_file):
' # Closing quote for the value ' # Closing quote for the value
\s*,\s* # Trailing comma and optional whitespace \s*,\s* # Trailing comma and optional whitespace
""", content, re.DOTALL | re.VERBOSE) """, content, re.DOTALL | re.VERBOSE)
#matches = re.findall(r"['\"]?([^'\"]+)['\"]?\s*=>\s*['\"](.*?)['\"]\s*,", content, re.DOTALL)
for match in matches: for match in matches:
key, value = match.group(2).strip(), match.group(3).strip() key, value = match.group(2).strip(), match.group(3).strip()
entries[key] = value.replace("\\n", "\n") entries[key] = value.replace("\\n", "\n")
@ -99,16 +108,19 @@ def convert_lex_to_po(lex_file, po_file, en_entries, general_en_entries,IsGenera
new_entries = [] new_entries = []
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 spacey version of msgctxt (Msg ID)
if not msgid: if not msgid:
print(f"Warning: Could not find original text for Msg ID '{msgctxt} ({language_code})") 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:
msgid = "Placeholder for missing original text" msgid = "Placeholder for missing original text - this means that \
there was a string in the translated lex file which did not appear in the english base file.\
Probably due to it no longer being required."
new_entries.append((msgctxt, msgid)) new_entries.append((msgctxt, msgid))
else: else:
print(f"Found {msgctxt} => {msgid} in general") print(f"Found {msgctxt} => {msgid} in general")
entry = polib.POEntry( entry = polib.POEntry(
msgctxt=msgctxt, msgctxt=msgctxt,
msgid=msgid, msgid=msgid,

View File

@ -25,16 +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
if module_name == 'Backup': #if module_name == 'Backup':
print(f"Processing {module_name}") print(f"Processing {module_name}")
po_dir = Path(os.path.join(subdir, "pofiles")) po_dir = Path(os.path.join(subdir, "pofiles"))
lex_dir = Path(os.path.join(subdir, "newlex")) lex_dir = Path(os.path.join(subdir, "newlex"))
# 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): for file in os.listdir(po_dir):
if file.endswith(".po"): if file.endswith(".po"):
po_file_path = Path(os.path.join(po_dir, file)) po_file_path = Path(os.path.join(po_dir, file))
translations = process_po_file(po_file_path) translations = process_po_file(po_file_path)
lex_file_name = lex_dir / (po_file_path.stem + ".lex") lex_file_name = lex_dir / (po_file_path.stem + ".lex")
create_lex_file(translations, lex_file_name) create_lex_file(translations, lex_file_name)