2014-10-12 37 views
0

我試圖用巴貝爾提取和更新一個結構化的字符串,但我還沒有找到一個很好的辦法做到這一點(沒有任何麻煩)如何使用Python巴貝爾與動態構造字符串

我現在方法來構造字符串:

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.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 "" 

但是當我運行

pybabel update -i messages.pot -d translations --previous 

它把我的寶貝msgid部分爲註釋與#~

你能幫我找到一個更好的方法來處理這個特定的用例嗎?非常感謝並提前擁抱!

回答

0

試試這個

PO目錄條目:

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

蟒蛇:

result += {{ _("Buying this will cost you (%s) USD" % usd) }}