如何在模板中合并 'blocktanslate' 和 'humanize' 标签?

How to combine 'blocktanslate' with 'humanize' tag in templates?

blocktranslate 允许我显示本地复数,而 humanize 允许我显示按百分组的数字。

它们中的每一个在独立使用时都可以工作,但我想同时拥有它们。

我试过:

(words_number是视图发送的整数)

{% load humanize %}
{% blocktranslate count counter=words_number %}
1 word
{% plural %}
{{ counter|intcomma }} words
{% endblocktranslate %}

但它显示:words(数字应该是空 space)

编辑:在找到解决方案之前,我使用 Javascript 按百分组:

{% blocktranslate count counter=words_number %}
1 word
{% plural %}
<span id="counter">{{ counter }}</span> words
{% endblocktranslate %}

<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
    let counter = document.getElementById('counter');   
    let counter_loc = parseInt(counter.innerHTML).toLocaleString();
    counter.innerHTML = counter_loc;
});
</script>

blocktranslate template tag 文档中所述,要在变量上使用过滤器或表达式,您需要使用 with 将它们 绑定 到某个局部变量,所以你可以做如下的事情:

{% load humanize %}
{% blocktranslate with count_humanized=words_number|intcomma count counter=words_number %}
1 word
{% plural %}
{{ count_humanized }} words
{% endblocktranslate %}