Skip to content

Commit

Permalink
✨ Multilingualism (#104) (#108)
Browse files Browse the repository at this point in the history
Parent issue: sequentech/meta#122

Co-authored-by: Félix Robles <felix@sequentech.io>
  • Loading branch information
edulix and Findeton authored Sep 6, 2023
1 parent a472e73 commit 4e86008
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions tally_pipes/pipes/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ def _header_footer(canvas, doc, hide_logo):
# Release the canvas
canvas.restoreState()

def custom_i18n(parent, key, langs):
translation = ''
key_i18n = key + '_i18n'
if key_i18n in parent:
for lang in langs:
if lang in parent[key_i18n]:
translation = parent[key_i18n][lang]
break
if not translation and key in parent:
translation = parent[key]
return translation

def pdf_print(election_results, config_folder, election_id):
from reportlab.lib import colors
from reportlab.platypus import (
Expand Down Expand Up @@ -197,6 +209,7 @@ def draw_page_number(self, page_count):
os.path.abspath(os.path.dirname(__file__)),
'locale'
)
languages = election_results.get('pdf', dict()).get('languages', [])
translate = gettext.translation(
'pipes',
localedir,
Expand All @@ -206,7 +219,7 @@ def draw_page_number(self, page_count):
_ = translate.gettext
try:
jsonconfig = get_election_cfg(election_id)
election_title = remove_html(jsonconfig['payload']['configuration']['title'])
election_title = remove_html(custom_i18n(jsonconfig['payload']['configuration'],'title', languages))
except:
election_title = ""

Expand Down Expand Up @@ -338,7 +351,7 @@ def get_percentage(num, base):
gen_text(
_('Question {question_index}: {question_title}').format(
question_index=i+1,
question_title=remove_html(question['title'])
question_title=remove_html(custom_i18n(question, 'title', languages))
),
size=15,
bold=True,
Expand Down Expand Up @@ -685,7 +698,7 @@ def get_percentage(num, base):
]
)
for answer in winners:
answer_text = remove_html(answer['text'])
answer_text = remove_html(custom_i18n(answer, 'text', languages))
if dict(title='isWriteInResult', url='true') in answer.get('urls', []):
answer_text = _('{candidate_text} (Write-in)').format(
candidate_text=answer_text
Expand Down Expand Up @@ -713,7 +726,7 @@ def get_percentage(num, base):
]
)
for loser in losers:
loser_text = remove_html(loser['text'])
loser_text = remove_html(custom_i18n(loser, 'text', languages))
if dict(title='isWriteInResult', url='true') in loser.get('urls', []):
loser_text = _('{candidate_text} (Write-in)').format(
candidate_text=loser_text
Expand Down

0 comments on commit 4e86008

Please sign in to comment.