I'm trying to use babel to extract and update a constructed string but I still haven't found a good way to do it (without any hassle)
My current approach to construct the string:
def calculate_money(amount):
(usd, cent) = calculate(amount)
if not (usd or cent):
return ''
params = {}
result = 'Buying this will cost you '
if usd:
result += '%(usd) USD'
params['usd'] = usd
if cent:
if params:
result += ' and '
result += '%(cent) cent'
params['cent'] = cent
result += '.'
return gettext(result, **params)
I know that pybabel
won't extract the dynamic string, so I put this into the en.po
, de.po
, zh.po
etc. files
msgid "Buying this will cost you %(usd) USD."
msgstr ""
msgid "Buying this will cost you %(cent) cent."
msgstr ""
msgid "Buying this will cost you %(usd) USD and %(cent) cent."
msgstr ""
But when I run
pybabel update -i messages.pot -d translations --previous
It put my precious msgid
parts into comments with #~
!
Could you help me to find a better way to handle this specific usecase? Many thanks and hugs in advance!