Skip to content

Commit 54a9ed0

Browse files
committed
support for comments in strings
1 parent e8a2820 commit 54a9ed0

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

pluralize/__init__.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import ast
2+
import json
13
import os
24
import re
3-
import json
45
import threading
5-
import ast
66

77
__version__ = "20240515.1"
88

@@ -46,7 +46,7 @@ def xml(self):
4646

4747

4848
class Translator(object):
49-
def __init__(self, folder=None, encoding="utf-8"):
49+
def __init__(self, folder=None, encoding="utf-8", comment_marker=None):
5050
"""
5151
creates a translator object loading languages and pluralizations from translations/en-US.py files
5252
usage:
@@ -61,6 +61,7 @@ def __init__(self, folder=None, encoding="utf-8"):
6161
self.missing = set()
6262
self.folder = folder
6363
self.encoding = encoding
64+
self.comment_marker = comment_marker
6465
if folder:
6566
self.load(folder)
6667

@@ -69,16 +70,26 @@ def load(self, folder):
6970
self.languages = {}
7071
for filename in os.listdir(folder):
7172
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:
7376
self.languages[filename[:-5].lower()] = json.load(fp)
7477

7578
def save(self, folder=None, ensure_ascii=True):
7679
"""save the loaded translation files"""
7780
folder = folder or self.folder
7881
for key in self.languages:
7982
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+
)
8293

8394
def select(self, accepted_languages="fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5"):
8495
"""given appected_langauges string from HTTP header, picks the best match"""
@@ -118,10 +129,14 @@ def _translator(self, text, **kwargs):
118129
elif isinstance(translations, dict) and translations:
119130
k = max(int(i) for i in translations.keys() if int(i) <= n)
120131
text = translations[str(k)].format(**kwargs)
132+
if text and self.comment_marker:
133+
text = text.split(self.comment_marker)[0]
121134
return text.format(**kwargs)
122135

123136
@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+
):
125140
"""finds all strings in files in folder needing translations"""
126141
matches_found = set()
127142
re_string_t = (

tests/test_simple.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import unittest
2+
23
from pluralize import Translator
34

45

56
class TestPluralization(unittest.TestCase):
67
def setUp(self):
7-
T = Translator()
8+
T = Translator(comment_marker="##")
89
T.update_languages(T.find_matches("./"))
910
T.languages = {
1011
"it": {
@@ -14,7 +15,10 @@ def setUp(self):
1415
"2": "due cani",
1516
"3": "alcuni cani",
1617
"10": "tanti cani",
17-
}
18+
},
19+
"dog##dialect": {
20+
"0": "nisciuno cane",
21+
},
1822
}
1923
}
2024
T.select("en;q=0.9,it-IT;q=0.1")
@@ -36,6 +40,13 @@ def test_simple(self):
3640
dog + " " + plus + " " + dog.format(n=2), "un cane piu' due cani"
3741
)
3842

43+
def test_comments(self):
44+
T = self.T
45+
dog = T("dog")
46+
self.assertEqual(str(dog.format(n=0)), "no cane")
47+
dog = T("dog##dialect")
48+
self.assertEqual(str(dog.format(n=0)), "nisciuno cane")
49+
3950
def test_idempotency(self):
4051
T = self.T
4152
text = "dog"

0 commit comments

Comments
 (0)