When building and maintaining websites, we often need to generate dynamic content, one common requirement being to adjust the singular or plural form of text based on the quantity.This not only concerns the accuracy of the content, but also enhances the user experience, especially in a multilingual environment.pluralizeThe practical filter, which can help us easily solve this problem.

UnderstandpluralizeBasic usage of the filter

pluralizeThe filter is designed to automatically add or select the correct plural form of a word based on the value of the number.The basic principle is to check the associated number; if the number is 1, it is considered singular; otherwise, it is considered plural.

The simplest usage is to add it directly after the variable|pluralizeAt this point, the filter will attempt to handle the English pluralization rules (usually by adding 's'):

{# 假设 count 的值为 1 #}
您有 {{ count }} item{{ count|pluralize }} 在购物车中。
{# 输出:您有 1 item 在购物车中。 #}

{# 假设 count 的值为 3 #}
您有 {{ count }} item{{ count|pluralize }} 在购物车中。
{# 输出:您有 3 items 在购物车中。 #}

However, not all words follow the simple rule of adding 's'.Some words have special plural forms, such as words ending in 'y' becoming 'ies', or requiring the addition of 'es'.pluralizeThe filter also takes this into account, allowing us to pass two parameters, separated by a comma, the first parameter is the singular form of the additional word, and the second parameter is the plural form of the additional word:

{# 假设 count 的值为 1 #}
您有 {{ count }} cherr{{ count|pluralize:"y,ies" }}。
{# 输出:您有 1 cherry。 #}

{# 假设 count 的值为 3 #}
您有 {{ count }} cherr{{ count|pluralize:"y,ies" }}。
{# 输出:您有 3 cherries。 #}

{# 假设 count 的值为 1 #}
您有 {{ count }} box{{ count|pluralize:"es" }}。
{# 输出:您有 1 box。 #}

{# 假设 count 的值为 3 #}
您有 {{ count }} box{{ count|pluralize:"es" }}。
{# 输出:您有 3 boxes。 #}

topluralizeFilter combined with variables

pluralizeThe true power of the filter lies in its ability to be used in combination with dynamic variables. In the templates of the Anqi CMS, we can obtain numbers from the content model, statistics, or custom logic, and then pass them directly topluralizeFilter.

We can usesetLabel a variable to simulate a dynamic variable, so that it is better understood how it works:

{% set user_count = 0 %}
网站有 {{ user_count }} 位用户{{ user_count|pluralize }} 注册。
{# 输出:网站有 0 位用户 注册。 (这里的"用户"是中文,所以pluralize不会改变它,但如果英文,就会变users) #}

{% set user_count = 1 %}
网站有 {{ user_count }} 位用户{{ user_count|pluralize }} 注册。
{# 输出:网站有 1 位用户 注册。 #}

{% set user_count = 5 %}
网站有 {{ user_count }} 位用户{{ user_count|pluralize }} 注册。
{# 输出:网站有 5 位用户 注册。 #}

As can be seen,user_countwhen it is 0 or greater than 1,pluralizeThe filter will try to add an 's' to indicate plural.For Chinese words, due to their linguistic characteristics, there is no change in the plural form.This filter is mainly designed for English words.

used in the actual content,pluralizeFilter

in the Aqy CMS, documents (archive)Categories(category)Data models typically include some numeric fields, such as view counts(Views)or comment counts(CommentCount)。We can directly associate these variables withpluralizeFilter combination, dynamically generate more natural text.

Suppose we are cycling through a list of articles and want to display the number of comments for each article:

{% archiveList archives with type="list" limit="3" %}
    {% for article in archives %}
    <article>
        <h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
        <p>
            这篇文章有 {{ article.CommentCount }} 条评论{{ article.CommentCount|pluralize }}。
            已被浏览 {{ article.Views }} 次{{ article.Views|pluralize }}。
        </p>
    </article>
    {% endfor %}
{% endarchiveList %}

In this code block:

  • Ifarticle.CommentCountIs 1, the text will display as '1 comment'.
  • Ifarticle.CommentCountIs 0 or greater than 1, the text will display as '0 comments' or 'X comments'.

It can be seen that for Chinese words like '评论' or '次', althoughpluralizeThe filter will still execute, but its default behavior will not cause any visible change to Chinese text, as Chinese nouns do not have plural forms. However, if your website is in English content, or you have used English words in the template, thenpluralizeThe function of the filter is very significant.

For example, in an English content website, the same logic will produce the following effect:

{# 假设 article.CommentCount = 1, article.Views = 1 #}
This article has 1 comment{{ 1|pluralize }}. It has been viewed 1 time{{ 1|pluralize:"e,s" }}.
{# 输出:This article has 1 comment. It has been viewed 1 time. #}

{# 假设 article.CommentCount = 5, article.Views = 10 #}
This article has 5 comment{{ 5|pluralize }}. It has been viewed 10 time{{ 10|pluralize:"e,s" }}.
{# 输出:This article has 5 comments. It has been viewed 10 times. #}

Here, we used the word "time".time{{ count|pluralize:"e,s" }}Handle this, ensuring that 'time' is displayed when the quantity is 1 and 'times' is displayed when the quantity is more than 1. This is a very typical special pluralization handling scenario.

By the above method, we can make the text content in the Anqi CMS template more dynamic and linguistically accurate, whether it's the simple rule of adding an 's', or specifying the singular and plural forms of words.pluralizeFilters can provide elegant solutions. This not only improves the efficiency of template writing but also makes the website content presented to the end user more refined and professional.


Common Questions (FAQ)

1.pluralizeDoes the filter support the plural form of Chinese nouns? pluralizeThe filter mainly follows English grammatical rules to handle singular and plural forms, such as adding 's' or 'es' at the end of a word, or converting according to rules like 'y,ies', etc. Since Chinese nouns do not have grammatical singular and plural changes, thereforepluralizeThe filter does not produce actual plural effects on Chinese words. If you want to display different Chinese phrases based on quantity, you may need to implement conditional statements manually.ifConditionally.

2. If my English word has very irregular plural forms (e.g., 'man' becomes 'men'),pluralizecan the filter handle? pluralizeThe filter is mainly used to handle plural forms (adding 's') and a few types that are realized by replacing suffixes (such as 'y' becomes 'ies'). For completely irregular plural forms like 'man' (singular) -> 'men' (plural) or 'child' -> 'children',pluralizeThe filter cannot handle this directly. You need to specify the singular and plural text for these special cases manually through the template.{% if %}Logic judgment to manually specify the singular and plural text for these special cases.

How can I ensure that the variable I use to judge singular and plural has a value, to avoid template rendering errors?Before passing the variable topluralizeIt is usually recommended to use before the filterifCheck if a variable exists or if it is a valid number, or usedefaulta filter to provide a default value. For example,{{ item.CommentCount|default:0|pluralize }}you canitem.CommentCounttreat it as 0 if it does not exist, and then performpluralizeProcess, thus avoiding potential template rendering issues.