|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator |
| 5 | +
|
| 6 | +SPDX-FileCopyrightText: 2024 Amilcar do Carmo Lucas <[email protected]> |
| 7 | +
|
| 8 | +SPDX-License-Identifier: GPL-3.0-or-later |
| 9 | +""" |
| 10 | + |
| 11 | +import argparse |
| 12 | +import gettext |
| 13 | +import glob |
| 14 | +import os |
| 15 | + |
| 16 | + |
| 17 | +def parse_arguments() -> argparse.Namespace: |
| 18 | + parser = argparse.ArgumentParser(description="Extract missing translations from a .po (GNU gettext) file.") |
| 19 | + |
| 20 | + # Add argument for language code |
| 21 | + parser.add_argument( |
| 22 | + "--lang-code", |
| 23 | + default="zh_CN", |
| 24 | + type=str, |
| 25 | + help="The language code for which to extract missing translations (e.g., 'zh_CN', 'pt'). Defaults to %(default)s", |
| 26 | + ) |
| 27 | + |
| 28 | + # Add argument for output file |
| 29 | + parser.add_argument( |
| 30 | + "--output-file", |
| 31 | + default="missing_translation", |
| 32 | + type=str, |
| 33 | + help="The base name of the file(s) where the missing translations will be written. " |
| 34 | + "This file will contain lines in the format 'index:msgid'. Defaults to %(default)s", |
| 35 | + ) |
| 36 | + |
| 37 | + # Add argument for maximum number of translations per output file |
| 38 | + parser.add_argument( |
| 39 | + "--max-translations", |
| 40 | + default=80, |
| 41 | + type=int, |
| 42 | + help="The maximum number of missing translations to write to each output file. Defaults to %(default)s", |
| 43 | + ) |
| 44 | + |
| 45 | + return parser.parse_args() |
| 46 | + |
| 47 | + |
| 48 | +def extract_missing_translations(lang_code: str) -> list[tuple[int, str]]: |
| 49 | + # Set up the translation catalog |
| 50 | + po_file = os.path.join("MethodicConfigurator", "locale", lang_code, "LC_MESSAGES", "MethodicConfigurator.po") |
| 51 | + language = gettext.translation( |
| 52 | + "MethodicConfigurator", localedir="MethodicConfigurator\\locale", languages=[lang_code], fallback=True |
| 53 | + ) |
| 54 | + |
| 55 | + # Read the .po file entries |
| 56 | + with open(po_file, encoding="utf-8") as f: |
| 57 | + lines = f.readlines() |
| 58 | + |
| 59 | + missing_translations: list[tuple[int, str]] = [] |
| 60 | + |
| 61 | + # Iterate through lines to find untranslated msgid |
| 62 | + msgid = "" |
| 63 | + in_msgid = False |
| 64 | + for i, f_line in enumerate(lines): |
| 65 | + line = f_line.strip() |
| 66 | + |
| 67 | + if line.startswith("msgid "): |
| 68 | + msgid = "" |
| 69 | + in_msgid = True |
| 70 | + |
| 71 | + if in_msgid and not line.startswith("msgstr "): |
| 72 | + line_split = line.split('"') |
| 73 | + if len(line_split) > 1: |
| 74 | + msgid += '"'.join(line_split[1:-1]) # Get the msgid string |
| 75 | + else: |
| 76 | + print(f"Error on line {i}") |
| 77 | + continue |
| 78 | + |
| 79 | + if in_msgid and line.startswith("msgstr "): |
| 80 | + in_msgid = False |
| 81 | + # escape \ characters in a string |
| 82 | + msgid_escaped = msgid.replace("\\", "\\\\") |
| 83 | + # Check if the translation exists |
| 84 | + if language.gettext(msgid_escaped) == msgid: # If translation is the same as msgid, it's missing |
| 85 | + missing_translations.append((i - 1, msgid)) |
| 86 | + |
| 87 | + return missing_translations |
| 88 | + |
| 89 | + |
| 90 | +def output_to_files(missing_translations: list[tuple[int, str]], output_file_base_name: str, max_translations: int) -> None: |
| 91 | + # Remove any existing output files with the same base name |
| 92 | + existing_files = glob.glob(f"{output_file_base_name}.txt") |
| 93 | + existing_files += glob.glob(f"{output_file_base_name}_*.txt") |
| 94 | + |
| 95 | + for existing_file in existing_files: |
| 96 | + os.remove(existing_file) |
| 97 | + |
| 98 | + # Determine the number of files needed |
| 99 | + total_missing = len(missing_translations) |
| 100 | + num_files = (total_missing // max_translations) + (1 if total_missing % max_translations else 0) |
| 101 | + |
| 102 | + # Write untranslated msgids along with their indices to the output file(s) |
| 103 | + for file_index in range(num_files): |
| 104 | + start_index = file_index * max_translations |
| 105 | + end_index = start_index + max_translations |
| 106 | + |
| 107 | + # Set the name of the output file based on the index |
| 108 | + current_output_file = output_file_base_name |
| 109 | + current_output_file += f"_{file_index + 1}" if num_files > 1 else "" |
| 110 | + current_output_file += ".txt" |
| 111 | + |
| 112 | + # Write untranslated msgids along with their indices to the output file |
| 113 | + with open(current_output_file, "w", encoding="utf-8") as f: |
| 114 | + for index, item in missing_translations[start_index:end_index]: |
| 115 | + f.write(f"{index}:{item}\n") |
| 116 | + |
| 117 | + |
| 118 | +def main() -> None: |
| 119 | + args = parse_arguments() |
| 120 | + missing_translations = extract_missing_translations(args.lang_code) |
| 121 | + output_to_files(missing_translations, args.output_file, args.max_translations) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + main() |
0 commit comments