Skip to content

enh: adding a translation_stats.py file #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions _static/translation_stats.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
106 changes: 106 additions & 0 deletions scripts/translation_stats.py
Original file line number Diff line number Diff line change
@@ -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()
Loading