I needed to export some HTML content from a multilingual site, where the HTML is a column in a csv table.
After reaching a "works for me" solution, I have two questions:
- what magic makes this even work?
- and, is there a less magical approach which I could use instead?
The HTML is generated using a TAL template with translatable strings.
<h3 i18n:translate=""> Document Summary </h3> <p tal:content="value"> Value goes here </p>
Calling the view directly on the respective documents, e.g.
would result in the "Content Summary" term being translated correctly, but not when calling the view from a script to create my csv. e.g.
# related items can be in various languages for related_item in related_items: related_object = related_item.from_object # https://docs.plone.org/develop/plone/views/browserviews.html item_html = getView(related_object, request, 'my_view').index()
This would result in the "Content Summary" term always being in the language determined by the context of @@my_summary_view
Eventually, I stumbled on the example given here: https://docs.plone.org/develop/plone/i18n/language.html
# Fake new language for all authenticated users event.request['LANGUAGE'] = language event.request.LANGUAGE_TOOL.LANGUAGE = language
I updated my script and attempted to use the language of the related object
custom_request = request for related_item in related_items: related_object = related_item.from_object custom_request['LANGUAGE'] = related_object.language custom_request.LANGUAGE_TOOL.LANGUAGE = related_object.language # https://docs.plone.org/develop/plone/views/browserviews.html item_html = getView(related_object, custom_request, 'my_view').index()
This would result in a single language being used throughout, depending on the language of the first related object
I finally settled in passing the language as a parameter, filtering the related items, and generating separate csv's for each language.
My knowledge of events and event subscribers is lacking, would this have been the correct approach?
Appreciate any clues...