Restore formatter function help texts to Transifex

We now use the form processed for easy translation. Fixes #2117352 [Translatable raw strings cause spurious newlines on Transifex](https://bugs.launchpad.net/calibre/+bug/2117352)
This commit is contained in:
Kovid Goyal 2025-07-22 18:53:27 +05:30
parent 4e66d2fff6
commit 29c309d23a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 33 additions and 7 deletions

View File

@ -8,6 +8,7 @@ __docformat__ = 'restructuredtext en'
import errno
import glob
import hashlib
import inspect
import json
import os
import re
@ -28,6 +29,20 @@ from setup.iso_codes import iso_data
from setup.parallel_build import batched_parallel_jobs
def serialize_msgid(text):
'''Serialize a string in the format used by msgid in GNU POT files.'''
if not text:
return 'msgid ""\n'
# Escape backslashes and quotes
escaped = text.replace('\\', r'\\').replace('"', r'\"')
ans = ['msgid ""']
lines = escaped.splitlines()
for line in lines:
trailer = '"' if line is lines[-1] else r'\n"'
ans.append(f'"{line}{trailer}')
return '\n'.join(ans)
def qt_sources():
qtdir = os.environ.get('QT_SRC', '/usr/src/qt6/qtbase')
j = partial(os.path.join, qtdir)
@ -71,6 +86,15 @@ class POT(Command): # {{{
ans.append(self.a(self.j(root, name)))
return ans
def get_ffml_docs(self):
from calibre.utils.formatter_functions import _formatter_builtins as b
ans = []
for ff in b:
lnum = inspect.getsourcelines(ff.__doc__getter__)[1]
text = ff.__doc__getter__().msgid
ans.append(f'#: src/calibre/utils/formatter_function.py:{lnum}\n' + serialize_msgid(text) + '\nmsgstr ""\n\n')
return ''.join(ans)
def get_tweaks_docs(self):
path = self.a(self.j(self.SRC, '..', 'resources', 'default_tweaks.py'))
with open(path, 'rb') as f:
@ -218,12 +242,12 @@ class POT(Command): # {{{
'--from-code=UTF-8', '--sort-by-file', '--omit-header',
'--no-wrap', '-kQT_TRANSLATE_NOOP:2', '-ktr', '-ktranslate:2',
] + qt_inputs)
with open(out.name, 'rb') as f:
src = f.read().decode('utf-8')
os.remove(out.name)
src = pot_header + '\n' + src
src += '\n\n' + self.get_tweaks_docs()
src += '\n\n' + self.get_ffml_docs()
bdir = os.path.join(self.TRANSLATIONS, __appname__)
if not os.path.exists(bdir):
os.makedirs(bdir)

View File

@ -60,20 +60,20 @@ URL_FUNCTIONS = _('URL functions')
# Class and method to save an untranslated copy of translated strings
class TranslatedStringWithRaw(str):
def __new__(cls, raw_english, raw_other, formatted_english, formatted_other):
def __new__(cls, raw_english, raw_other, formatted_english, formatted_other, msgid):
instance = super().__new__(cls, formatted_other)
instance.raw_english = raw_english
instance.raw_other = raw_other
instance.formatted_english = formatted_english
instance.formatted_other = formatted_other
instance.msgid = msgid
instance.did_format = False
return instance
def format(self, *args, **kw):
formatted_english = self.raw_english.format(*args, **kw)
formatted_other = self.raw_other.format(*args, **kw)
v = TranslatedStringWithRaw(self.raw_english, self.raw_other,
formatted_english, formatted_other)
v = TranslatedStringWithRaw(self.raw_english, self.raw_other, formatted_english, formatted_other, self.msgid)
v.saved_args = args
v.saved_kwargs = kw
v.did_format = True
@ -87,9 +87,11 @@ class TranslatedStringWithRaw(str):
def translate_ffml(txt):
from calibre.utils.ffml_processor import FFMLProcessor
lookup = FFMLProcessor().document_to_transifex(txt, '', safe=True)
translated = xlated(lookup)
return TranslatedStringWithRaw(txt, translated, txt, translated)
msgid = FFMLProcessor().document_to_transifex(txt, '', safe=True).strip()
translated = xlated(msgid)
if translated == msgid:
translated = txt
return TranslatedStringWithRaw(txt, translated, txt, translated, msgid)
class StoredObjectType(Enum):