1
+ import ast
2
+ import json
1
3
import os
2
4
import re
3
- import json
4
5
import threading
5
- import ast
6
6
7
7
__version__ = "20240515.1"
8
8
@@ -46,7 +46,7 @@ def xml(self):
46
46
47
47
48
48
class Translator (object ):
49
- def __init__ (self , folder = None , encoding = "utf-8" ):
49
+ def __init__ (self , folder = None , encoding = "utf-8" , comment_marker = None ):
50
50
"""
51
51
creates a translator object loading languages and pluralizations from translations/en-US.py files
52
52
usage:
@@ -61,6 +61,7 @@ def __init__(self, folder=None, encoding="utf-8"):
61
61
self .missing = set ()
62
62
self .folder = folder
63
63
self .encoding = encoding
64
+ self .comment_marker = comment_marker
64
65
if folder :
65
66
self .load (folder )
66
67
@@ -69,16 +70,26 @@ def load(self, folder):
69
70
self .languages = {}
70
71
for filename in os .listdir (folder ):
71
72
if re_language .match (filename ):
72
- with open (os .path .join (folder , filename ), "r" , encoding = self .encoding ) as fp :
73
+ with open (
74
+ os .path .join (folder , filename ), "r" , encoding = self .encoding
75
+ ) as fp :
73
76
self .languages [filename [:- 5 ].lower ()] = json .load (fp )
74
77
75
78
def save (self , folder = None , ensure_ascii = True ):
76
79
"""save the loaded translation files"""
77
80
folder = folder or self .folder
78
81
for key in self .languages :
79
82
filename = "%s.json" % key
80
- with open (os .path .join (folder , filename ), "w" , encoding = self .encoding ) as fp :
81
- json .dump (self .languages [key ], fp , sort_keys = True , indent = 4 , ensure_ascii = ensure_ascii )
83
+ with open (
84
+ os .path .join (folder , filename ), "w" , encoding = self .encoding
85
+ ) as fp :
86
+ json .dump (
87
+ self .languages [key ],
88
+ fp ,
89
+ sort_keys = True ,
90
+ indent = 4 ,
91
+ ensure_ascii = ensure_ascii ,
92
+ )
82
93
83
94
def select (self , accepted_languages = "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5" ):
84
95
"""given appected_langauges string from HTTP header, picks the best match"""
@@ -118,10 +129,14 @@ def _translator(self, text, **kwargs):
118
129
elif isinstance (translations , dict ) and translations :
119
130
k = max (int (i ) for i in translations .keys () if int (i ) <= n )
120
131
text = translations [str (k )].format (** kwargs )
132
+ if text and self .comment_marker :
133
+ text = text .split (self .comment_marker )[0 ]
121
134
return text .format (** kwargs )
122
135
123
136
@staticmethod
124
- def find_matches (folder , name = "T" , extensions = ["py" , "js" , "html" ], encoding = "utf-8" ):
137
+ def find_matches (
138
+ folder , name = "T" , extensions = ["py" , "js" , "html" ], encoding = "utf-8"
139
+ ):
125
140
"""finds all strings in files in folder needing translations"""
126
141
matches_found = set ()
127
142
re_string_t = (
0 commit comments