In the template development of Anqi CMS,pluralizeFilter is a very practical tool, it can automatically adjust the singular and plural forms of words according to the number of values.This provides great convenience in the scenarios of creating multilingual websites or dynamically displaying the corresponding words for quantities.pluralizeWhat are the default rules it follows for pluralization when the filter is specified without an explicit plural noun?
UnderstandingpluralizeThe working principle of the filter
pluralizeThe core function of the filter is to determine the form of the word based on the provided value. In English, the plural form of most nouns is formed by adding 's' at the end. For example,customerThe customer is singular when the quantity is 1, and it becomes plural when the quantity is 0, 2, or morecustomers.pluralizeThe filter is designed to automate this process.
The default pluralization rule
When we usepluralizeFilter, but no custom plural appendages (i.e., just{{ obj|pluralize }}this form) when, the Aqiyi CMS template engine will follow a set of simple and common English pluralization rules:
By default,pluralizeThe filter will add an 's' at the end of the word to indicate plural when the quantity is not equal to 1. If the quantity is exactly 1, the word will remain 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:
假设我们有一个变量来表示数量,比如Englishcount:
customer{{ 0|pluralize }}it will outputcustomers.customer{{ 1|pluralize }}it will outputcustomer.customer{{ 2|pluralize }}it will outputcustomers.
从这些示例中可以清晰地看到,当数量是0或2时,EnglishpluralizeFilter iscustomer后面自动添加了“s”,而当数量是1时,EnglishcustomerMaintain unchanged. This is the embodiment of the default rule 'add an 's' when the number is not equal to 1.'
Comparison with custom plural adjectives
In order to better understand the default rules, we can simply review thempluralizeThe behavior of the filter when providing custom additional words. It allows us to handle some irregular plural forms, or more complex singular and plural changes. 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 number is not equal to 1.
Another example is handling words ending with 's' or 'x', which usually require adding 'es':
walrus{{ 0|pluralize:"es" }}it will outputwalruses.walrus{{ 1|pluralize:"es" }}it will outputwalrus.
These custom rules makepluralizeThe filter is very flexible, but it will revert to the simple default rule of "adding 's" mentioned earlier when we do not provide these custom rules.
Actual application scenarios
In daily template development, if your content is mainly aimed at English words that follow the conventional "add s" pluralization rule, then you can directly use{{ count|pluralize }}This concise writing will be very efficient.For example, in scenarios such as displaying the number of products, user comments, etc., if the corresponding labels (such as "个It saves the trouble of customizing the plural form for each word, simplifying the template code.
Understand this default behavior can help us be more confident in its usepluralizeFilter, whether you choose its concise default mode or provide custom rules for special cases, you can always keep it in mind and make the content display more accurate and professional.
Common Questions (FAQ)
pluralizeFilter supports irregular plural forms?pluralizeThe filter itself does not directly “understand” irregular plural forms (such asmantomen,childtochildren),it will only follow the default "add 's' " rule or the custom additional words you provide. For irregular plurals, you need to manually specify two additional words, for example,man{{ count|pluralize:",men" }}If count is 1, it will displaymanotherwise, it will displaymen).If the number is zero,
pluralizeHow does the filter handle it?In the absence of custom plural affixes,pluralizeThe filter will treat zero as non-1, so the default "add s" rule will be applied. For example,item{{ 0|pluralize }}it will be displayed asitems。If you want to display specific text when the quantity is zero (for example, “No items”), you need to add additional logical judgment or customize the plural affixes.pluralizeFilter can be applied to what types of data?pluralizeThe filter is usually used with integer (Integer) quantity values 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, the most common usage in actual template development is to use it with integer variables representing quantities.