(Check_translations) Add in build dict of missing translation, and an exclude list

This commit is contained in:
Brian Read 2024-07-21 14:48:47 +01:00
parent 2b5006ff0f
commit 31b2b0e990
2 changed files with 56 additions and 9 deletions

View File

@ -1,5 +1,24 @@
import os import os
import argparse import argparse
import re
do_not_need_translating = ['AM','PM','AM/PM','nfs', 'cifs']
def infer_prefix(entries, filename,IsGeneral=False):
"""
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:
prefix = match.group(1)
if prefix:
print(f"Found prefix:{prefix} {filename}")
return prefix
# If no prefix is found, return the first 3 characters of the filename
return filename.stem[:3].lower() + "_"
def find_lex_files(modulename): def find_lex_files(modulename):
base_locale_path = f"/home/brianr//Documents/smeserver-manager-locale/root/usr/share/smanager/lib/SrvMngr/I18N/Modules/{modulename}/" base_locale_path = f"/home/brianr//Documents/smeserver-manager-locale/root/usr/share/smanager/lib/SrvMngr/I18N/Modules/{modulename}/"
@ -10,6 +29,7 @@ def find_lex_files(modulename):
# Find the corresponding _en.lex file # Find the corresponding _en.lex file
en_file = next((f for f in os.listdir(base_en_path) if f.endswith('_en.lex')), None) en_file = next((f for f in os.listdir(base_en_path) if f.endswith('_en.lex')), None)
# Assuming the _en.lex file exists # Assuming the _en.lex file exists
if en_file is None: if en_file is None:
print(f"No _en.lex file found for module: {modulename}") print(f"No _en.lex file found for module: {modulename}")
@ -23,6 +43,12 @@ def find_lex_files(modulename):
key, message = line.split('=>', 1) key, message = line.split('=>', 1)
translations[key.strip().strip("'")] = message.strip().strip(",").strip("'") translations[key.strip().strip("'")] = message.strip().strip(",").strip("'")
module_prefix = infer_prefix(translations,en_file);
# Dictionary to accumulate missing translations
missing_translations = {}
key_not_in_en = {}
# Compare against each lex file in the locale directory # Compare against each lex file in the locale directory
for lex_file in lex_files: for lex_file in lex_files:
#Skip any english lang file in there (there might be one) #Skip any english lang file in there (there might be one)
@ -30,21 +56,42 @@ def find_lex_files(modulename):
continue continue
with open(os.path.join(base_locale_path, lex_file), 'r', encoding='utf-8') as loc_f: with open(os.path.join(base_locale_path, lex_file), 'r', encoding='utf-8') as loc_f:
print(f"File:{lex_file}") print(f"File:{lex_file}")
language_code = lex_file.split('.')[0][-2:] # Extracting language code from filename
# Ensure the language code entry in the dictionary
if language_code not in missing_translations:
missing_translations[language_code] = [] # Initialize an empty list for this language code
if language_code not in key_not_in_en:
key_not_in_en[language_code] = [] # Initialize an empty list for this language code
for line in loc_f: for line in loc_f:
if '=>' in line: if '=>' in line:
key, message = line.split('=>', 1) key, message = line.split('=>', 1)
key = key.strip().strip("'") key = key.strip().strip("'")
message = message.strip().strip(",").strip("'") message = message.strip().strip(",").strip("'")
# Remove the module prefix to check against do_not_need_translating
stripped_key = key[len(module_prefix):] # Remove the prefix and the underscore
# Checking for missing translations # Checking for missing translations
language_code = lex_file.split('.')[0][-2:] # Extracting language code from filename if stripped_key not in do_not_need_translating: # Check without prefix
#print(f"{lex_file} {language_code}")
#quit()
if key in translations: if key in translations:
if message == translations[key] or message == '': if message == translations[key] or message == '':
print(f"Missing translation in module '{modulename}', ({language_code})', key '{key}'") #print(f"Missing translation in module '{modulename}', ({language_code})', key '{key}'")
missing_translations[language_code].append(key)
else: else:
print(f"Key '{key}' not found in english lex for module '{modulename} ({language_code})'.") #print(f"Key '{key}' not found in english lex for module '{modulename} ({language_code})'.")
key_not_in_en[language_code].append(key)
for lang_code, keys in missing_translations.items():
if keys:
print(f"Missing translations for module '{modulename}', ({lang_code}) - #{len(keys)}:")
print(keys)
for lang_code, keys in key_not_in_en.items():
if keys:
print(f"Keys not found in english lex for module '{modulename} ({lang_code}) - #{len(keys)}'.")
print(keys)
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Check for missing translations in lex files.") parser = argparse.ArgumentParser(description="Check for missing translations in lex files.")

View File

@ -50,7 +50,7 @@ def save_lex_file(entries, lex_file):
escaped_value = value.replace("'", "\\'") # Escape single quotes escaped_value = value.replace("'", "\\'") # Escape single quotes
file.write(f"'{key}' => '{escaped_value}',\n") file.write(f"'{key}' => '{escaped_value}',\n")
def infer_prefix(entries, filename,IsGeneral): def infer_prefix(entries, filename,IsGeneral=False):
""" """
Infer the <abc> prefix from entries that contain it, fallback to the first 3 characters of the filename. Infer the <abc> prefix from entries that contain it, fallback to the first 3 characters of the filename.
""" """