In the template development of AnQi CMS,pluralizeThe filter is a very practical tool that can automatically adjust the singular and plural forms of words according to the number of values.This provides great convenience in the scenario of creating multilingual websites or when dynamic display of corresponding quantity words is required.At times, we might wonder, when only a single number is provided topluralizeWhat are the default rules it follows for pluralization when the filter is specified without an explicit plural suffix?

UnderstandingpluralizeThe principle of the filter.

pluralizeThe core function of the filter is to judge the form of the word based on the provided value. In English, most nouns are pluralized by adding 's' at the end. For example,customer(Customer) It is singular when the quantity is 1, and it becomes plural when the quantity is 0, 2, or more.customers.pluralizeThe filter is designed to automate this process.

The default plural processing rule

When we usepluralizeFilter, but no custom plural suffixes (i.e., just write{{ obj|pluralize }}in this form) when the Anqi CMS template engine will follow a simple and common English pluralization rule:

By default,pluralizeThe filter adds an 's' at the end of a word when the quantity is not equal to 1 to indicate plural. If the quantity is exactly 1, the word remains in its original singular form.

This means that it assumes you are dealing with a common English noun that follows the "add s" rule.

Let's illustrate this rule with examples from the document:

Suppose we have a variable to represent the quantity, such ascount:

  • customer{{ 0|pluralize }}It will outputcustomers.
  • customer{{ 1|pluralize }}It will outputcustomer.
  • customer{{ 2|pluralize }}It will outputcustomers.

From these examples, it is clear that when the quantity is 0 or 2,pluralizeFilter is oncustomeran 's' is automatically added at the end, and when the quantity is 1,customerMaintain unchanged. This is the embodiment of the default rule "add 's' when the number is not equal to 1".

Comparison with custom plural affixes.

To better understand the default rules, we can simply review it.pluralizeThe filter's behavior when providing custom additional words. It allows us to handle some irregular plural forms, or more complex singular and plural variations. For example:

  • cherr{{ 0|pluralize:"y,ies" }}It will outputcherries.
  • cherr{{ 1|pluralize:"y,ies" }}It will outputcherry.
  • cherr{{ 2|pluralize:"y,ies" }}It will outputcherries.

In this example, by providing"y,ies"we tell the filter to replace the suffix 'y' with 'ies' when the quantity is not equal to 1.

Another example is handling words ending with 's' or 'x', which usually need to add 'es':

  • walrus{{ 0|pluralize:"es" }}It will outputwalruses.
  • walrus{{ 1|pluralize:"es" }}It will outputwalrus.

These custom rules allowpluralizeThe filter is very flexible, but it will revert to the simple "add s" default rule mentioned earlier when we do not provide these custom rules.

Application scenarios in practice

In the template development on a daily basis, if your content is mainly for English words that follow the conventional "add s" pluralization rule, then use directly{{ count|pluralize }}This concise writing will be very efficient. For example, in scenarios where the corresponding tags (such as “pieces” or “comments” etc.) need to be followed by the plural form of English words, and these words belong to common types, then the default rules can well meet the needs.It saves the trouble of customizing the plural form for each word, simplifying the template code.

Understand this default behavior, it can help us use it more confidentlypluralizeFilter, whether choosing its simple default mode or providing custom rules for special situations, it can keep you informed, making content display more accurate and professional.


Frequently Asked Questions (FAQ)

  1. pluralizeDoes the filter support irregular plural forms? pluralizeThe filter itself does not directly 'understand' irregular plural forms (such asmantomen,childtochildrenIt will only follow the default "add s" rule or the custom additional word you provide. For irregular plurals, you need to manually specify two additional words, such asman{{ count|pluralize:",men" }}(If count is 1, it will display)manotherwise show)men)

  2. If the quantity is zero,pluralizeHow does the filter handle?In the absence of a custom plural suffix,pluralizeThe filter treats cases with zero quantity as non-1, therefore, it applies the default 'add s' rule. For example,item{{ 0|pluralize }}It will be displayed asitemsIf you want to display specific text when the quantity is zero (such as “No items”), you need to add additional logic or customize plural suffixes.

  3. pluralizeWhat types of data can the filter be applied to? pluralizeThe filter is usually used with the quantity value of the Integer type to determine the singular or plural form of a word.Although it can also accept other types of data as the basis for its internal judgment, but the most common usage in actual template development is to use integer variables representing quantities.