0

我正在尝试使用 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.pozh.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部分放入评论中#~

你能帮我找到一种更好的方法来处理这个特定的用例吗?非常感谢和提前拥抱!

4

1 回答 1

0

尝试这个

po目录条目:

msgid = 'Buying this will cost you %s USD'

Python:

result += {{ _("Buying this will cost you (%s) USD" % usd) }}
于 2014-11-20T16:55:40.483 回答