Django templates: Best practice for translating text block with HTML in it

后端 未结 3 552
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 02:54

In Django templates, how would I translate a block that contains HTML? For example:

{% trans \"Please\" %}
    

        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-29 03:23

    I can offer a convenient solution for only partial fragments which are constant for any translation.

    In this case you can avoid to use any HTML or CSS inside of your .po file when using custom template tag such as the next:

    @register.filter( name='safewrap' )
    def safewrap( val, arg ):
        return val.format( arg )
    safewrap.is_safe = True
    
    ...
    
    {% blocktrans with sum2="{0}"|safewrap:sum %}
        It costs {{sum2}} dollars.
    {% endblocktrans %}
    

    So, in your .po file you have:

    It costs %(sum2)s dollars.
    

    But it's a difficult question - what to do with partial fragments which require translation (like in your case).

提交回复
热议问题