[英]How to use Python Babel with dynamically constructed string
我正在嘗試使用 babel 來提取和更新構造的字符串,但我仍然沒有找到一個好的方法(沒有任何麻煩)
我目前構造字符串的方法:
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)
我知道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 ""
但是當我跑
pybabel update -i messages.pot -d translations --previous
它將我寶貴的msgid
部分放入帶有#~
評論中!
你能幫我找到一種更好的方法來處理這個特定的用例嗎? 非常感謝並提前擁抱!
嘗試這個
po 目錄條目:
msgid = 'Buying this will cost you %s USD'
Python:
result += {{ _("Buying this will cost you (%s) USD" % usd) }}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.