Lex2Po/Check_Translation.py

55 lines
2.2 KiB
Python
Raw Normal View History

2024-07-21 11:59:16 +02:00
import os
import argparse
def find_lex_files(modulename):
2024-07-21 12:47:14 +02:00
base_locale_path = f"/home/brianr//Documents/smeserver-manager-locale/root/usr/share/smanager/lib/SrvMngr/I18N/Modules/{modulename}/"
base_en_path = f"/home/brianr//Documents/smeserver-manager/root/usr/share/smanager/lib/SrvMngr/I18N/Modules/{modulename}/"
2024-07-21 11:59:16 +02:00
2024-07-21 12:47:14 +02:00
# Get all .lex files in the specified directory
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)
2024-07-21 11:59:16 +02:00
2024-07-21 12:47:14 +02:00
# Assuming the _en.lex file exists
if en_file is None:
print(f"No _en.lex file found for module: {modulename}")
return
2024-07-21 11:59:16 +02:00
2024-07-21 12:47:14 +02:00
# Read translation pairs from the _en file
translations = {}
with open(os.path.join(base_en_path, en_file), 'r', encoding='utf-8') as en_f:
for line in en_f:
if '=>' in line:
key, message = line.split('=>', 1)
translations[key.strip().strip("'")] = message.strip().strip(",").strip("'")
2024-07-21 11:59:16 +02:00
2024-07-21 12:47:14 +02:00
# 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)
if lex_file.endswith('_en.lex'):
continue
with open(os.path.join(base_locale_path, lex_file), 'r', encoding='utf-8') as loc_f:
print(f"File:{lex_file}")
for line in loc_f:
if '=>' in line:
key, message = line.split('=>', 1)
key = key.strip().strip("'")
message = message.strip().strip(",").strip("'")
2024-07-21 11:59:16 +02:00
2024-07-21 12:47:14 +02:00
# 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})'.")
2024-07-21 11:59:16 +02:00
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Check for missing translations in lex files.")
parser.add_argument("modulename", help="The name of the module to check translations for.")
args = parser.parse_args()
find_lex_files(args.modulename)