Sort out lack of prefix for General directory

This commit is contained in:
Brian Read 2024-07-18 12:07:36 +01:00
parent b04b27f101
commit b061a07cbd

View File

@ -41,10 +41,12 @@ def save_lex_file(entries, lex_file):
escaped_value = value.replace("'", "\\'") # Escape single quotes
file.write(f"'{key}' => '{escaped_value}',\n")
def infer_prefix(entries, filename):
def infer_prefix(entries, filename,IsGeneral):
"""
Infer the <abc> prefix from entries that contain it, fallback to the first 3 characters of the filename.
"""
if IsGeneral:
return "" #No prefix for General items
for key in entries.keys():
match = re.match(r"(.*?_)?.*", key)
if match:
@ -78,12 +80,12 @@ def ensure_prefix(entries, prefix,file_path):
updated_entries[key] = value
return updated_entries
def convert_lex_to_po(lex_file, po_file, en_entries, general_en_entries):
def convert_lex_to_po(lex_file, po_file, en_entries, general_en_entries,IsGeneral=False):
translated_entries = parse_lex_file(lex_file)
language_code = extract_language_from_filename(po_file.name)
# Infer prefix from original en_entries and ensure all entries have this prefix
prefix = infer_prefix(en_entries, Path(lex_file))
prefix = infer_prefix(en_entries, Path(lex_file),IsGeneral)
en_entries = ensure_prefix(en_entries, prefix,lex_file)
translated_entries = ensure_prefix(translated_entries, prefix,lex_file)
@ -149,10 +151,8 @@ def process_directory_with_en_mapping(source_directory, master_directory):
# Apply it to find the corresponding en directory
corresponding_en_dir = master_directory / subservient_dir_name
#Skip the General directory as it has not prefix's
if subservient_dir_name == "General":
# and skip to next
continue
#Tag the General directory as it has no prefix's
IsGeneral = subservient_dir_name == "General"
# Find corresponding `*`_en.lex` files
en_entries = {}
@ -173,7 +173,7 @@ def process_directory_with_en_mapping(source_directory, master_directory):
#po_file = po_file.lower().replace("-", "_")
print(f"Converting {lex_file} to {po_file}")
convert_lex_to_po(lex_file, po_file, en_entries,general_en_entries)
convert_lex_to_po(lex_file, po_file, en_entries,general_en_entries,IsGeneral)
# Save the updated en_entries to the en.lex file in the locale directory
if en_entries: