Skip to content

Commit 5c1a25d

Browse files
committed
f
1 parent b295855 commit 5c1a25d

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

scripts/translator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import concurrent.futures
1111
from tqdm import tqdm #pip3 install tqdm
1212
import traceback
13+
import re
1314

1415

1516
MASTER_BRANCH = "master"
@@ -85,6 +86,10 @@ def _get_encoding_for_model(model: str):
8586
print(f"Tokenizer for model {model} not found. Falling back to {FINAL_TOKENIZER_FALLBACK}.")
8687
return tiktoken.get_encoding(FINAL_TOKENIZER_FALLBACK)
8788

89+
def _fix_translated_shortcodes(text: str) -> str:
90+
"""Keep mdBook shortcode attribute names valid after translation."""
91+
return re.sub(r'(\{\{#tab\s+)(?!name=)[^=\s}]+=', r'\1name=', text)
92+
8893
def reportTokens(prompt, model):
8994
encoding = _get_encoding_for_model(model)
9095
# print number of tokens in light gray, with first 50 characters of prompt in green. if truncated, show that it is truncated
@@ -263,6 +268,7 @@ def translate_text(language, text, file_path, model, cont=0, slpitted=False, cli
263268
response_message = response_message.replace("bypassy", "bypasses") # PL translations translates that from time to time
264269
response_message = response_message.replace("Bypassy", "Bypasses")
265270
response_message = response_message.replace("-privec.md", "-privesc.md") # PL translations translates that from time to time
271+
response_message = _fix_translated_shortcodes(response_message)
266272

267273
# Sometimes chatgpt modified the number of "#" at the beginning of the text, so we need to fix that. This is specially important for the first line of the MD that mucst have only 1 "#"
268274
cont2 = 0

0 commit comments

Comments
 (0)