Skip to content

Commit 92b4c46

Browse files
RobPasMuelwasser
authored andcommitted
feat: adding script to get translation stats
1 parent f6242e8 commit 92b4c46

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed

_static/translation_stats.json

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"es": {
3+
"continuous-integration": {
4+
"total": 38,
5+
"translated": 0,
6+
"fuzzy": 0,
7+
"untranslated": 38,
8+
"percentage": 0.0
9+
},
10+
"CONTRIBUTING": {
11+
"total": 124,
12+
"translated": 0,
13+
"fuzzy": 0,
14+
"untranslated": 124,
15+
"percentage": 0.0
16+
},
17+
"documentation": {
18+
"total": 9,
19+
"translated": 8,
20+
"fuzzy": 1,
21+
"untranslated": 0,
22+
"percentage": 88.89
23+
},
24+
"index": {
25+
"total": 14,
26+
"translated": 11,
27+
"fuzzy": 1,
28+
"untranslated": 2,
29+
"percentage": 78.57
30+
},
31+
"package-structure-code": {
32+
"total": 131,
33+
"translated": 126,
34+
"fuzzy": 1,
35+
"untranslated": 4,
36+
"percentage": 96.18
37+
},
38+
"tests": {
39+
"total": 1,
40+
"translated": 0,
41+
"fuzzy": 1,
42+
"untranslated": 0,
43+
"percentage": 0.0
44+
},
45+
"TRANSLATING": {
46+
"total": 108,
47+
"translated": 0,
48+
"fuzzy": 0,
49+
"untranslated": 108,
50+
"percentage": 0.0
51+
},
52+
"tutorials": {
53+
"total": 12,
54+
"translated": 11,
55+
"fuzzy": 1,
56+
"untranslated": 0,
57+
"percentage": 91.67
58+
}
59+
},
60+
"ja": {
61+
"continuous-integration": {
62+
"total": 38,
63+
"translated": 38,
64+
"fuzzy": 0,
65+
"untranslated": 0,
66+
"percentage": 100.0
67+
},
68+
"CONTRIBUTING": {
69+
"total": 124,
70+
"translated": 124,
71+
"fuzzy": 0,
72+
"untranslated": 0,
73+
"percentage": 100.0
74+
},
75+
"documentation": {
76+
"total": 468,
77+
"translated": 467,
78+
"fuzzy": 1,
79+
"untranslated": 0,
80+
"percentage": 99.79
81+
},
82+
"index": {
83+
"total": 14,
84+
"translated": 11,
85+
"fuzzy": 1,
86+
"untranslated": 2,
87+
"percentage": 78.57
88+
},
89+
"package-structure-code": {
90+
"total": 87,
91+
"translated": 86,
92+
"fuzzy": 1,
93+
"untranslated": 0,
94+
"percentage": 98.85
95+
},
96+
"tests": {
97+
"total": 1,
98+
"translated": 0,
99+
"fuzzy": 1,
100+
"untranslated": 0,
101+
"percentage": 0.0
102+
},
103+
"TRANSLATING": {
104+
"total": 25,
105+
"translated": 24,
106+
"fuzzy": 1,
107+
"untranslated": 0,
108+
"percentage": 96.0
109+
},
110+
"tutorials": {
111+
"total": 12,
112+
"translated": 11,
113+
"fuzzy": 1,
114+
"untranslated": 0,
115+
"percentage": 91.67
116+
}
117+
}
118+
}

scripts/translate_table.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from babel.messages import pofile
2+
3+
from pathlib import Path
4+
from babel.messages import pofile
5+
6+
# Paths needed for this script
7+
BASE_DIR = Path(__file__).resolve().parent.parent # Repository base directory
8+
LOCALES_DIR = BASE_DIR / "locales" # Locales directory
9+
STATIC_DIR = BASE_DIR / "_static" # Static directory
10+
11+
def calculate_translation_percentage(po_path : Path, locale : str) -> dict:
12+
"""
13+
Calculate the translation percentage for a given .po file.
14+
15+
Parameters
16+
----------
17+
po_path : Path
18+
Path to the .po file.
19+
locale : str
20+
Locale code (e.g., 'es', 'fr').
21+
22+
Returns
23+
-------
24+
dict
25+
A dictionary containing the total number of strings, the number
26+
of translated strings, the number of untranslated strings, and
27+
the translation percentage.
28+
"""
29+
with open(po_path, "r", encoding="utf-8") as f:
30+
catalog = pofile.read_po(f, locale=locale)
31+
32+
total = 0
33+
translated = 0
34+
fuzzy = 0
35+
36+
for message in catalog:
37+
if message.id:
38+
total += 1
39+
# Check if the message is fuzzy
40+
# Fuzzy messages are not considered translated
41+
if message.fuzzy:
42+
fuzzy += 1
43+
break
44+
# Check if the message is translated
45+
if message.string:
46+
translated += 1
47+
48+
percentage = (translated / total * 100) if total > 0 else 0
49+
50+
return {
51+
"total": total,
52+
"translated": translated,
53+
"fuzzy": fuzzy,
54+
"untranslated": total - translated - fuzzy,
55+
"percentage": round(percentage, 2)
56+
}
57+
58+
def main():
59+
# Get all .po files in the locales directory
60+
po_files = list(LOCALES_DIR.rglob("*.po"))
61+
62+
# Let's use a dictionary to store the results
63+
#
64+
# We will store the info as
65+
# {
66+
# "es": {
67+
# "file1": {
68+
# "total": 100,
69+
# "translated": 50,
70+
# "fuzzy": 0,
71+
# "untranslated": 50,
72+
# "percentage": 50.0
73+
# },
74+
# ...
75+
# },
76+
# "fr": {
77+
# "file1": {
78+
# "total": 100,
79+
# "translated": 50,
80+
# "fuzzy": 0,
81+
# "untranslated": 50,
82+
# "percentage": 50.0
83+
# },
84+
# ...
85+
# }
86+
results = {}
87+
88+
# Calculate translation percentages for each file
89+
for po_file in po_files:
90+
# Get the locale from the file path
91+
locale = po_file.parent.parent.name
92+
stats = calculate_translation_percentage(po_file, locale)
93+
print(f"({po_file.stem}): {stats['percentage']}% translated ({stats['translated']} of {stats['total']})")
94+
95+
# Store the results in the dictionary
96+
if locale not in results:
97+
results[locale] = {}
98+
99+
results[locale][po_file.stem] = stats
100+
101+
# Dump the results to a JSON file
102+
with open(STATIC_DIR / "translation_stats.json", "w") as f:
103+
import json
104+
json.dump(results, f, indent=4)
105+
106+
if __name__ == "__main__":
107+
main()

0 commit comments

Comments
 (0)