translate content files earlier in build

this moves the translation of contents.lr files earlier in the build
process, in `on_before_build_all` instead of `on_before_build`, to
ensure that the build queue accounts for the alternative content files
(`contents+*.lr`) during the build process, instead of requiring
multiple (expensive) build runs

the caveat is that translated alternative contents files are now only
produced during a full build, which likely breaks "on-the-fly" creation
of those files when authoring using the Lektor web admin interface
This commit is contained in:
Jérôme Charaoui 2024-10-28 09:18:22 -04:00
parent 237afb9bfa
commit a27d8b77d3
No known key found for this signature in database
GPG Key ID: 69C52F658E988542

View File

@ -362,37 +362,33 @@ class I18NPlugin(Plugin):
return newblocks return newblocks
def on_before_build(self, builder, build_state, source, prog, **extra): def process_contents(self):
"""Before building a page, produce all its alternatives (=translated pages) """Produce all content file alternatives (=translated pages)
using the gettext translations available.""" using the gettext translations available."""
if self.enabled and isinstance(source,Page) and source.alt in (PRIMARY_ALT, self.content_language): for root, _, files in os.walk(os.path.join(self.env.root_path, 'content')):
contents = None if re.match('content$', root):
for fn in source.iter_source_filenames(): continue
try: if 'contents.lr' in files:
contents=FileContents(fn) fn = os.path.join(root, 'contents.lr')
except IOError: contents = FileContents(fn)
pass # next for language in self.translations_languages:
translator = gettext.translation("contents", join(self.i18npath, '_compiled'), languages=[language], fallback=True)
for language in self.translations_languages: translated_filename = os.path.join(root, "contents+%s.lr" % language)
translator = gettext.translation("contents", with contents.open(encoding='utf-8') as file:
join(self.i18npath,'_compiled'), languages=[language], fallback = True) chunks = self.__parse_source_structure(file.readlines())
translated_filename = join(dirname(source.source_filename), with open(translated_filename, "w") as f:
"contents+%s.lr"%language) reporter.report_generic("writing to " + translated_filename)
with contents.open(encoding='utf-8') as file: for type, content in chunks: # see __parse_source_structure
chunks = self.__parse_source_structure(file.readlines()) if type == 'raw':
with open(translated_filename,"w") as f: f.write(content)
for type, content in chunks: # see __parse_source_structure elif type == 'translatable':
if type == 'raw': if self.trans_parwise: # translate per paragraph
f.write(content) f.write(self.__trans_parwise(content, translator))
elif type == 'translatable': else:
if self.trans_parwise: # translate per paragraph f.write(self.__trans_linewise(content, translator))
f.write(self.__trans_parwise(content,
translator))
else: else:
f.write(self.__trans_linewise(content, raise RuntimeError("Unknown chunk type detected, this is a bug")
translator))
else:
raise RuntimeError("Unknown chunk type detected, this is a bug")
def __trans_linewise(self, content, translator): def __trans_linewise(self, content, translator):
"""Translate the chunk linewise.""" """Translate the chunk linewise."""
@ -447,6 +443,9 @@ class I18NPlugin(Plugin):
reporter.report_generic("Parsing templates for i18n into %s" \ reporter.report_generic("Parsing templates for i18n into %s" \
% relpath(templates_pot_filename, builder.env.root_path)) % relpath(templates_pot_filename, builder.env.root_path))
translations.parse_templates(templates_pot_filename) translations.parse_templates(templates_pot_filename)
# walk through contents.lr files and produce alternatives
# before the build system creates its work queue
self.process_contents()
def on_after_build_all(self, builder, **extra): def on_after_build_all(self, builder, **extra):