diff --git a/_static/translation_stats.json b/_static/translation_stats.json new file mode 100644 index 000000000..a24201d3d --- /dev/null +++ b/_static/translation_stats.json @@ -0,0 +1,118 @@ +{ + "es": { + "continuous-integration": { + "total": 38, + "translated": 0, + "fuzzy": 0, + "untranslated": 38, + "percentage": 0.0 + }, + "CONTRIBUTING": { + "total": 124, + "translated": 0, + "fuzzy": 0, + "untranslated": 124, + "percentage": 0.0 + }, + "documentation": { + "total": 9, + "translated": 8, + "fuzzy": 1, + "untranslated": 0, + "percentage": 88.89 + }, + "index": { + "total": 14, + "translated": 11, + "fuzzy": 1, + "untranslated": 2, + "percentage": 78.57 + }, + "package-structure-code": { + "total": 131, + "translated": 126, + "fuzzy": 1, + "untranslated": 4, + "percentage": 96.18 + }, + "tests": { + "total": 1, + "translated": 0, + "fuzzy": 1, + "untranslated": 0, + "percentage": 0.0 + }, + "TRANSLATING": { + "total": 108, + "translated": 0, + "fuzzy": 0, + "untranslated": 108, + "percentage": 0.0 + }, + "tutorials": { + "total": 12, + "translated": 11, + "fuzzy": 1, + "untranslated": 0, + "percentage": 91.67 + } + }, + "ja": { + "continuous-integration": { + "total": 38, + "translated": 38, + "fuzzy": 0, + "untranslated": 0, + "percentage": 100.0 + }, + "CONTRIBUTING": { + "total": 124, + "translated": 124, + "fuzzy": 0, + "untranslated": 0, + "percentage": 100.0 + }, + "documentation": { + "total": 468, + "translated": 467, + "fuzzy": 1, + "untranslated": 0, + "percentage": 99.79 + }, + "index": { + "total": 14, + "translated": 11, + "fuzzy": 1, + "untranslated": 2, + "percentage": 78.57 + }, + "package-structure-code": { + "total": 87, + "translated": 86, + "fuzzy": 1, + "untranslated": 0, + "percentage": 98.85 + }, + "tests": { + "total": 1, + "translated": 0, + "fuzzy": 1, + "untranslated": 0, + "percentage": 0.0 + }, + "TRANSLATING": { + "total": 25, + "translated": 24, + "fuzzy": 1, + "untranslated": 0, + "percentage": 96.0 + }, + "tutorials": { + "total": 12, + "translated": 11, + "fuzzy": 1, + "untranslated": 0, + "percentage": 91.67 + } + } +} diff --git a/scripts/translation_stats.py b/scripts/translation_stats.py new file mode 100644 index 000000000..9988ae6ff --- /dev/null +++ b/scripts/translation_stats.py @@ -0,0 +1,106 @@ +from babel.messages import pofile + +from pathlib import Path +from babel.messages import pofile + +# Paths needed for this script +BASE_DIR = Path(__file__).resolve().parent.parent # Repository base directory +LOCALES_DIR = BASE_DIR / "locales" # Locales directory +STATIC_DIR = BASE_DIR / "_static" # Static directory + +def calculate_translation_percentage(po_path : Path, locale : str) -> dict: + """ + Calculate the translation percentage for a given .po file. + + Parameters + ---------- + po_path : Path + Path to the .po file. + locale : str + Locale code (e.g., 'es', 'fr'). + + Returns + ------- + dict + A dictionary containing the total number of strings, translated strings, + fuzzy strings, untranslated strings, and the translation percentage. + """ + with open(po_path, "r", encoding="utf-8") as f: + catalog = pofile.read_po(f, locale=locale) + + total = 0 + translated = 0 + fuzzy = 0 + + for message in catalog: + if message.id: + total += 1 + # Check if the message is fuzzy + # Fuzzy messages are not considered translated + if message.fuzzy: + fuzzy += 1 + break + # Check if the message is translated + if message.string: + translated += 1 + + percentage = (translated / total * 100) if total > 0 else 0 + + return { + "total": total, + "translated": translated, + "fuzzy": fuzzy, + "untranslated": total - translated - fuzzy, + "percentage": round(percentage, 2) + } + +def main(): + # Get all .po files in the locales directory + po_files = list(LOCALES_DIR.rglob("*.po")) + + # Let's use a dictionary to store the results + # + # We will store the info as + # { + # "es": { + # "file1": { + # "total": 100, + # "translated": 50, + # "fuzzy": 0, + # "untranslated": 50, + # "percentage": 50.0 + # }, + # ... + # }, + # "fr": { + # "file1": { + # "total": 100, + # "translated": 50, + # "fuzzy": 0, + # "untranslated": 50, + # "percentage": 50.0 + # }, + # ... + # } + results = {} + + # Calculate translation percentages for each file + for po_file in po_files: + # Get the locale from the file path + locale = po_file.parent.parent.name + stats = calculate_translation_percentage(po_file, locale) + print(f"({po_file.stem}): {stats['percentage']}% translated ({stats['translated']} of {stats['total']})") + + # Store the results in the dictionary + if locale not in results: + results[locale] = {} + + results[locale][po_file.stem] = stats + + # Dump the results to a JSON file + with open(STATIC_DIR / "translation_stats.json", "w") as f: + import json + json.dump(results, f, indent=4) + +if __name__ == "__main__": + main()