[英]How to use Python Babel with dynamically constructed string
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)我正在尝试使用 babel 来提取和更新构造的字符串,但我仍然没有找到一个好的方法(没有任何麻烦)
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我知道
pybabel
不会提取动态字符串,所以我把它放到en.po
、 de.po
、 zh.po
等文件中
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 #~
!它将我宝贵的
msgid
部分放入带有#~
评论中!
Could you help me to find a better way to handle this specific usecase?你能帮我找到一种更好的方法来处理这个特定的用例吗? Many thanks and hugs in advance!
非常感谢并提前拥抱!
Try this尝试这个
po catalog entry: po 目录条目:
msgid = 'Buying this will cost you %s USD'
python: Python:
result += {{ _("Buying this will cost you (%s) USD" % usd) }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.