mirror of
https://github.com/torproject/lego.git
synced 2024-11-23 09:39:44 +00:00
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:
parent
237afb9bfa
commit
a27d8b77d3
@ -362,38 +362,34 @@ 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:
|
for language in self.translations_languages:
|
||||||
translator = gettext.translation("contents",
|
translator = gettext.translation("contents", join(self.i18npath, '_compiled'), languages=[language], fallback=True)
|
||||||
join(self.i18npath,'_compiled'), languages=[language], fallback = True)
|
translated_filename = os.path.join(root, "contents+%s.lr" % language)
|
||||||
translated_filename = join(dirname(source.source_filename),
|
|
||||||
"contents+%s.lr"%language)
|
|
||||||
with contents.open(encoding='utf-8') as file:
|
with contents.open(encoding='utf-8') as file:
|
||||||
chunks = self.__parse_source_structure(file.readlines())
|
chunks = self.__parse_source_structure(file.readlines())
|
||||||
with open(translated_filename,"w") as f:
|
with open(translated_filename, "w") as f:
|
||||||
|
reporter.report_generic("writing to " + translated_filename)
|
||||||
for type, content in chunks: # see __parse_source_structure
|
for type, content in chunks: # see __parse_source_structure
|
||||||
if type == 'raw':
|
if type == 'raw':
|
||||||
f.write(content)
|
f.write(content)
|
||||||
elif type == 'translatable':
|
elif type == 'translatable':
|
||||||
if self.trans_parwise: # translate per paragraph
|
if self.trans_parwise: # translate per paragraph
|
||||||
f.write(self.__trans_parwise(content,
|
f.write(self.__trans_parwise(content, translator))
|
||||||
translator))
|
|
||||||
else:
|
else:
|
||||||
f.write(self.__trans_linewise(content,
|
f.write(self.__trans_linewise(content, translator))
|
||||||
translator))
|
|
||||||
else:
|
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):
|
def __trans_linewise(self, content, translator):
|
||||||
"""Translate the chunk linewise."""
|
"""Translate the chunk linewise."""
|
||||||
lines = []
|
lines = []
|
||||||
@ -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):
|
||||||
|
Loading…
Reference in New Issue
Block a user