Working around the POT templates being inserted into lektor contents
I've spent a few hours digging into lektor-i18n-plugin and trying to figure out what causes lego#37 (closed). I've discovered that running lektor b
with python 3.9+ creates different .po/.pot files than running with python 3.8.x. The main difference is that the buggy translation files translate the empty string ""
as the PO text.
I'm writing this up in detail in case someone else wants to pick up where I've left off, and also to document where exactly the bug is occuring.
The lektor-i18n-plugin code goes through each translation block, and if translating linewise (the default, also what we use), splits each translation block on \n
, and then translates:
def __trans_linewise(self, content, translator):
"""Translate the chunk linewise."""
lines = []
for line in content.split('\n'):
line_stripped = line.strip()
trans_stripline = trans(translator, line_stripped) # translate the stripped version
# and re-inject the stripped translation into original line (not stripped)
lines.append(line.replace(line_stripped,
trans_stripline, 1))
return '\n'.join(lines)
This means that translation blocks that end in a newline end up split into a list like this:
content = 'lorem ipsum\ndolor sit amet\n'
split_lines = ['lorem ipsum' 'dolor sit amet', '']
Notice the empty string at the end of the list; this will get translated to the PO text we've been seeing. A simple workaround is to just not translate empty strings:
...
for line in content.split('\n'):
line_stripped = line.strip()
if line_stripped == '':
trans_stripline = ''
else:
trans_stripline = trans(translator, line_stripped)
...
I'm not entirely happy with this since the bug is still present, we're just avoiding the consequences of it by ignoring empty strings. It feels fragile, but as long as we don't do anything with the buggy translation files that the plugin generates, we should be fine.
I'm hesitant on sending a patch for this upstream because it's not an actual fix. The alternative is maintaining a fork of the plugin in lego with our patch applied.