-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslation_utils.py
More file actions
42 lines (36 loc) 路 1.78 KB
/
Copy pathtranslation_utils.py
File metadata and controls
42 lines (36 loc) 路 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import re
from logger import log_debug
# Note: This function is retained for backward compatibility but should be replaced
# with the LanguageManager class for new code. Translation handlers should use
# language_manager.get_tesseract_code() instead.
def get_lang_code_for_translation_api(lang_code):
"""Converts various language codes to standard ISO 639-1 for translation APIs."""
code_map = {
'eng': 'en', 'pol': 'pl', 'fra': 'fr', 'deu': 'de', 'spa': 'es',
'ita': 'it', 'jpn': 'ja', 'kor': 'ko', 'chi_sim': 'zh-cn',
'chi_tra': 'zh-tw', 'rus': 'ru', 'por': 'pt', 'nld': 'nl',
'ara': 'ar', 'hin': 'hi', 'ukr': 'uk', 'ces': 'cs', 'dan': 'da',
'fin': 'fi', 'swe': 'sv', 'nor': 'no', 'auto': 'auto'
}
if isinstance(lang_code, str):
lang_code_lower = lang_code.lower()
result = code_map.get(lang_code_lower, lang_code_lower)
# log_debug(f"Converting language code '{lang_code}' to API code '{result}'") # Can be verbose
return result
return lang_code # Should be a string
def post_process_translation_text(text):
if not text: return text
# Fix spacing before punctuation marks
text = re.sub(r'\s+\?', '?', text)
text = re.sub(r'([.!?])([A-Z])', r'\1 \2', text)
# PRESERVE dialog line breaks while cleaning up excessive spaces
# Split by newlines, clean spaces within each line, then rejoin
lines = text.split('\n')
cleaned_lines = []
for line in lines:
# Clean up multiple spaces within each line, but preserve the line structure
cleaned_line = re.sub(r'[ \t]{2,}', ' ', line) # Only target spaces and tabs, not newlines
cleaned_lines.append(cleaned_line)
# Rejoin with preserved newlines
text = '\n'.join(cleaned_lines)
return text