Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Technik
protocol-translation-live
Commits
085f6617
Commit
085f6617
authored
May 03, 2022
by
Jakob Lerch
Browse files
do not ignore leading space
parent
d9e4a2b1
Changes
1
Hide whitespace changes
Inline
Side-by-side
protocol_translation_live/__main__.py
View file @
085f6617
...
...
@@ -11,11 +11,13 @@ import utils
from
pad
import
Etherpad
,
HedgedocNote
,
Pad
from
translation
import
DeepL
,
LibreTranslate
,
Translation
# TODO: implement initial test of config file (if values are ok and stuff), provide fallback values, error msgs
# TODO: scheint nur bei fem-instanz zu funktionieren. bei libretranslate.de kackt das ab
# TODO: document config properly
# TODO: proper typing
# TODO: update setup.py
# TODO: tab in quelldatei führt zu 8space in destination
# TODO: übersetzung parallelisieren mit echten threads
# TODO: proper typing
# TODO: error msgs werden nicht in systemd krams geschrieben
# TODO: ordentliche setup anleitung
def
main
():
...
...
@@ -124,20 +126,39 @@ def main():
def
translate_with_leading_whitespace
(
lines
:
List
[
str
])
->
List
[
str
]:
""" translate list of strings with trailing whitespace lines:
* strip leading lines consisting of whitespace,
* add them when writing to dst_pad
* strip leading space string from each line
* add both when writing to dst_pad
This has to be done, because libretranslate ignores leading whitespace,
leading to incorrect order of the translated lines.
"""
leading_space_lines
=
list
(
# filter leading space lines
space_lines
=
list
(
takewhile
(
lambda
line
:
line
.
isspace
()
or
not
line
,
lines
))
text_to_translate
=
'
\n
'
.
join
(
lines
[
len
(
leading_space_lines
):])
# filter leading space strings of each line
lstrings
:
List
[
str
]
=
[]
lines_to_translate
:
List
[
str
]
=
[]
for
line
in
lines
[
len
(
space_lines
):]:
# compute leading strings
lchars
=
list
(
takewhile
(
lambda
character
:
character
.
isspace
(),
line
))
lstring
=
''
.
join
(
lchars
)
lstrings
.
append
(
lstring
)
# create lines where leading space strings are stripped
lines_to_translate
.
append
(
line
[
len
(
lchars
):])
# translate it
text_to_translate
=
'
\n
'
.
join
(
lines_to_translate
)
translated_lines
=
t
.
translate
(
text_to_translate
,
src_lang
,
dst_lang
).
splitlines
()
return
leading_space_lines
+
translated_lines
# append leading strings per line
translated_lines_lspace
:
List
[
str
]
=
[]
for
space
,
line
in
zip
(
lstrings
,
translated_lines
):
translated_lines_lspace
.
append
(
space
+
line
)
return
space_lines
+
translated_lines_lspace
dst_pad
.
write
(
"initializing..."
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment