From 31b2b0e990bddb44693e3985077f32090a0c605e Mon Sep 17 00:00:00 2001 From: Brian Read Date: Sun, 21 Jul 2024 14:48:47 +0100 Subject: [PATCH] (Check_translations) Add in build dict of missing translation, and an exclude list --- Check_Translation.py | 63 ++++++++++++++++++++++++++++++++++++++------ Lex2Po.py | 2 +- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/Check_Translation.py b/Check_Translation.py index f01803b..37a7bb9 100644 --- a/Check_Translation.py +++ b/Check_Translation.py @@ -1,5 +1,24 @@ import os import argparse +import re + +do_not_need_translating = ['AM','PM','AM/PM','nfs', 'cifs'] + +def infer_prefix(entries, filename,IsGeneral=False): + """ + Infer the 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): base_locale_path = f"/home/brianr//Documents/smeserver-manager-locale/root/usr/share/smanager/lib/SrvMngr/I18N/Modules/{modulename}/" @@ -9,6 +28,7 @@ def find_lex_files(modulename): lex_files = [f for f in os.listdir(base_locale_path) if f.endswith('.lex')] # Find the corresponding _en.lex file en_file = next((f for f in os.listdir(base_en_path) if f.endswith('_en.lex')), None) + # Assuming the _en.lex file exists if en_file is None: @@ -23,6 +43,12 @@ def find_lex_files(modulename): key, message = line.split('=>', 1) 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 for lex_file in lex_files: #Skip any english lang file in there (there might be one) @@ -30,21 +56,42 @@ def find_lex_files(modulename): continue with open(os.path.join(base_locale_path, lex_file), 'r', encoding='utf-8') as loc_f: 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: if '=>' in line: key, message = line.split('=>', 1) key = key.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 - language_code = lex_file.split('.')[0][-2:] # Extracting language code from filename - #print(f"{lex_file} {language_code}") - #quit() - if key in translations: - if message == translations[key] or message == '': - print(f"Missing translation in module '{modulename}', ({language_code})', key '{key}'") - else: - print(f"Key '{key}' not found in english lex for module '{modulename} ({language_code})'.") + if stripped_key not in do_not_need_translating: # Check without prefix + if key in translations: + if message == translations[key] or message == '': + #print(f"Missing translation in module '{modulename}', ({language_code})', key '{key}'") + missing_translations[language_code].append(key) + else: + #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__": parser = argparse.ArgumentParser(description="Check for missing translations in lex files.") diff --git a/Lex2Po.py b/Lex2Po.py index afb179c..6ad7dbd 100644 --- a/Lex2Po.py +++ b/Lex2Po.py @@ -50,7 +50,7 @@ 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,IsGeneral): +def infer_prefix(entries, filename,IsGeneral=False): """ Infer the prefix from entries that contain it, fallback to the first 3 characters of the filename. """