In website content operation, ensuring the accuracy and professionalism of the text is crucial, especially when dealing with multilingual content.Most of the time, we need to display the singular or plural form of a word based on the change in quantity, which not only concerns grammatical correctness but also directly affects the user's reading experience.pluralizeThe filter is the tool to solve this problem, it can intelligently handle the singular and plural conversion of English words, even likewalrussuch words that require special suffixes are not a problem.

pluralizeFilter: Smartly handle the plural form of words

pluralizeThe filter is a very practical feature in the AnQiCMS template engine.The core function is to automatically judge and add the correct plural suffix to the word based on the given number.This is especially convenient for dynamic display scenarios such as product inventory, comment count, and user count, without the need for us to manually write complex conditional judgment logic.

Its basic usage is to pass a number or a numeric variable through a pipe|pass topluralizeFilter, and then append the singular form of the word after it. However,pluralizeThe filter does not directly receive the word itself as a parameter, but receives numbers, and adds appropriate suffixes based on the size of the number inprefixand append them at the end.

For example, let's say we have a numbercountWe want to display the singular and plural forms of the word “customer”:

customer{{ count|pluralize }}
  • IfcountIs1The output will becustomer(usually without a suffix to indicate the singular).
  • IfcountIs0or greater1The output will becustomers(default adds 's' suffix).

By default,pluralizeThe filter will add to non-singular numbers (0 or greater than 1).'s'As a plural suffix. But this clearly cannot meet all the plural rules of English words, because some words have more special plural forms, such as those ending ins,x,z,ch,shusually addesAnd some words have irregular changes (such aschild-u003echildren)

How to deal with words likewalrusWords that need specific suffixes like this?

For words likewalrusThis, the plural form needs to be added at the end of the wordesThe word,pluralizeThe filter provides flexible parameter configuration. We canpluralizeSpecify a custom plural suffix in string form after the filter.

For example, we want to display correctly.walrusthe plural form:

walrus{{ simple.number|pluralize:"es" }}

Heresimple.numberrepresents the number you want to use to determine singular and plural. Ifsimple.numberIs1, the result would bewalrus; ifsimple.numberIs0or greater1, the result would bewalruses. By passing"es"As a parameter, we tell the filter to add when the number is not 1esinstead of the defaults.

Furthermore, for those words with greater differences between singular and plural forms, such ascherryThe plural of (cherry) ischerries,its variation involves the suffixychanges toies. At this point,pluralizeThe filter supports passing in two suffixes separated by commas:the first is when the quantity is1,the word ending ofsingular special characters, the second is when the quantity is not1then, the replacement is requiredplural special characters.

cherr{{ simple.number|pluralize:"y,ies" }}
  • Ifsimple.numberIs1The output will becherry.
  • Ifsimple.numberIs0or greater1The output will becherries.

here,"y,ies"Tell the filter: If the number is1then the word isyending (implying the original word ischerr+y); if the number is not1, then it willyReplaceies.

Application scenarios and precautions

pluralizeFilters are widely used in building dynamic website content. For example:

  • Product inventory display:您有 {{ product.stock }} {{ "item"|pluralize:"s" }} 在购物车中。
  • Number of comments or messages:{{ comment.count }} {{ "comment"|pluralize:"s" }} 未读。
  • Statistics of user activity:今天有 {{ user.active_count }} {{ "user"|pluralize:"s" }} 访问了您的网站。

While usingpluralizeThere are a few things to note when using the filter:

  1. The parameter is a number:pluralizeThe filter depends on the number passed in to determine singular or plural, please make sure that the variable passed in is indeed a number type.
  2. Mainly for English: AlthoughpluralizeProvided flexible suffix configuration, but its design concept and common usage are mainly focused on the singular and plural transformation of English words.The plural rules of other languages may be more complex and require more customized processing.
  3. Do not handle irregular verbs::Likeman(men),mouse(mice) such irregularly changing words,pluralizeThe filter cannot be directly resolved through suffix parameters because its root has also changed. For such words, it may be necessary to combineifstatements for conditional judgment to manually handle.

MasterpluralizeThe use of filters can greatly enhance the automation level and user experience of template content, making your website content display more authentic and professional.


Frequently Asked Questions (FAQ)

Q1:pluralizeCan the filter be used for the conversion of Chinese words between singular and plural?A1: No support.pluralizeThe filter is mainly used to process the singular and plural forms of English words. Chinese language does not have the concept of word singular and plural, so the filter is not applicable to Chinese content.

Q2: If a word's singular and plural forms are completely irregular, for example “man” (singular) and “men” (plural),pluralizeCan the filter handle it?A2:pluralizeThe filter handles plural forms by adding or replacing suffixes. For completely irregular plurals where the root changes like “man” -> “men”,pluralizeThe filter cannot process directly. In this case, you need to combineiflogical judgment of quantity, then manually output the correct singular or plural form, for example{% if count == 1 %}man{% else %}men{% endif %}.

Q3: I can customizepluralizeThe filter is for quantities of0should I add plural suffixes? For example, I want “0 item” instead of “0 items”.A3:pluralizeThe filter defaults to0Considered as plural (i.e., add a suffix). If you want not to display the plural suffix when the quantity is0you can slightly adjust your template logic, for example:

{% if count == 0 %}{{ count }} {{ "item" }}{% else %}{{ count }} {{ "item"|pluralize:"s" }}{% endif %}

or, if your word has a custom suffix, you can replace it with"s"replace it with your custom suffix, such as"es"or"y,ies".