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