From a27d8b77d39c09edd2e67532db064b9e69449e3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Charaoui?= Date: Mon, 28 Oct 2024 09:18:22 -0400 Subject: [PATCH] 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 --- packages/i18n/lektor_i18n.py | 57 ++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/packages/i18n/lektor_i18n.py b/packages/i18n/lektor_i18n.py index 8bf65e2..f3bde8b 100644 --- a/packages/i18n/lektor_i18n.py +++ b/packages/i18n/lektor_i18n.py @@ -362,37 +362,33 @@ class I18NPlugin(Plugin): return newblocks - def on_before_build(self, builder, build_state, source, prog, **extra): - """Before building a page, produce all its alternatives (=translated pages) + def process_contents(self): + """Produce all content file alternatives (=translated pages) using the gettext translations available.""" - if self.enabled and isinstance(source,Page) and source.alt in (PRIMARY_ALT, self.content_language): - contents = None - for fn in source.iter_source_filenames(): - try: - contents=FileContents(fn) - except IOError: - pass # next - - for language in self.translations_languages: - translator = gettext.translation("contents", - join(self.i18npath,'_compiled'), languages=[language], fallback = True) - translated_filename = join(dirname(source.source_filename), - "contents+%s.lr"%language) - with contents.open(encoding='utf-8') as file: - chunks = self.__parse_source_structure(file.readlines()) - with open(translated_filename,"w") as f: - for type, content in chunks: # see __parse_source_structure - if type == 'raw': - f.write(content) - elif type == 'translatable': - if self.trans_parwise: # translate per paragraph - f.write(self.__trans_parwise(content, - translator)) + for root, _, files in os.walk(os.path.join(self.env.root_path, 'content')): + if re.match('content$', root): + continue + if 'contents.lr' in files: + fn = os.path.join(root, 'contents.lr') + contents = FileContents(fn) + for language in self.translations_languages: + translator = gettext.translation("contents", join(self.i18npath, '_compiled'), languages=[language], fallback=True) + translated_filename = os.path.join(root, "contents+%s.lr" % language) + with contents.open(encoding='utf-8') as file: + chunks = self.__parse_source_structure(file.readlines()) + with open(translated_filename, "w") as f: + reporter.report_generic("writing to " + translated_filename) + for type, content in chunks: # see __parse_source_structure + if type == 'raw': + f.write(content) + elif type == 'translatable': + if self.trans_parwise: # translate per paragraph + f.write(self.__trans_parwise(content, translator)) + else: + f.write(self.__trans_linewise(content, translator)) else: - f.write(self.__trans_linewise(content, - translator)) - else: - raise RuntimeError("Unknown chunk type detected, this is a bug") + raise RuntimeError("Unknown chunk type detected, this is a bug") + def __trans_linewise(self, content, translator): """Translate the chunk linewise.""" @@ -447,6 +443,9 @@ class I18NPlugin(Plugin): reporter.report_generic("Parsing templates for i18n into %s" \ % relpath(templates_pot_filename, builder.env.root_path)) 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):