简体   繁体   English

如何将 Python Babel 与动态构造的字符串一起使用

[英]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.pode.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 ""

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.

相关问题 如何在 python 中构建 label 行? - How to label constructed lines in python? 如何在python中构造ICMP数据包 - How is an ICMP packet constructed in python 如何在 gekko 中动态构建约束? - How could constraints be dynamically constructed in gekko? Python-断言记录器输出的子字符串,无论消息如何构造 - Python - assert sub-string of logger output regardless of how the message is constructed 在 Python 中(没有 Pickle),可以动态构造任意 JSON 字符串吗? - In Python (without Pickle), Can Arbitrary JSON strings be Constructed Dynamically? Python:1)如何引用两个字符串变量并在其间插入一个列表元素? 2) 我怎样才能将 append 的字符串构造成一个列表? - Python: 1) How to reference two string variables and insert a list element in between? 2) How can I append the string constructed to a list? Django 模板 - 如何检查动态构造的键是否在对象中具有值 - Django Template - How to check if dynamically constructed key has value in Object 用 python babel 翻译字符串不起作用 - Translate string with python babel doesn't work Python:将由多行构成的字符串追加到列表中 - Python: Appending string constructed out of multiple lines to list 如何使用字符串格式来动态分配变量 - How to use string formatting to dynamically assign variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM