When building a website, the way content is presented often determines the subtlety of the user experience.A minor detail, such as correctly displaying the singular and plural forms of words according to the number, can make the website content appear more professional and natural.AnQiCMS provides a very practical tool to handle such situations, that ispluralizefilter.

Flexible handling of quantity changes: In AnQiCMSpluralizeDetailed explanation of filter usage

In the dynamic content display on a website, such as showing product inventory, comment quantity, or search result item count, we often need to adjust the plural or singular descriptive text according to specific numbers.For example, when there is '1 item', we expect to display '1 Item', and when there are '0' or '2' items, we need to display '0 Items' or '2 Items'.Manually judging and writing conditional logic can be very cumbersome, andpluralizeThe filter is created to solve this pain point.

This filter can intelligently add the appropriate plural form to the end of words based on the values it receives, making your content output more grammatically correct and enhancing the reading experience of the user.

Basic usage: The default 's' rule

pluralizeThe basic usage of the filter is to directly act on a numeric variable, it follows the most common plural rules in English: when the quantity is 0 or greater than 1, it will add a word ending.'s'When the quantity is exactly 1, the word remains unchanged without adding any suffix.

For example, suppose we have a variable.countWe want to display the singular or plural of 'customer' based on its value:

customer{{ count|pluralize }}

Ifcounthas a value of0The output will becustomersIfcounthas a value of1The output will becustomerIfcounthas a value of2The output will becustomers.

In this way, there is no need for extraifcondition judgment,pluralizeThe filter can automatically handle the singular and plural conversion in most cases.

Custom plural suffix: to deal with irregular changes.

In English, not all nouns follow the simple rule of adding an 's' to form the plural. Some nouns have irregular plural forms, such as 'cherry' becomes 'cherries' and 'walrus' becomes 'walruses'. For these irregular changes,pluralizeThe filter provides flexible custom suffix functionality.

You can specify only the top-level categories by using thepluralizeAfter the filter, provide one or two comma-separated strings to customize the suffix.

1. Provide a custom suffix (for 0 or more, 1 unchanged):

If you provide only a custom suffix, this suffix will be applied to the end of the word when the number is 0 or greater than 1, and the word remains unchanged when the number is 1. This applies to those plural forms that are simply addedesOr words ending with a specific suffix.

walrus{{ count|pluralize:"es" }}

Ifcounthas a value of0The output will bewalrusesIfcounthas a value of1The output will bewalrusIfcounthas a value of2The output will bewalruses.

2. Provide two custom suffixes (one for 1, and 0 or more):

For words with more complex singular and plural form changes, such as words ending in 'y' which become 'ies' in plural, you can provide two custom suffixes. In this case, the first suffix will be used for quantities of1The situation, while the second suffix is used for quantities of0or greater1The situation. It should be noted that the suffix you provide replaces the corresponding part of the word directly

cherr{{ count|pluralize:"y,ies" }}

Ifcounthas a value of0The output will becherriesIfcounthas a value of1The output will becherryIfcounthas a value of2The output will becherries.

This feature allowspluralizeThe filter can cover a wider range of vocabulary, ensuring that your website content displays the correct grammar in any quantity.

Application scenarios in practice

pluralizeThe filter is very useful in a variety of website content operation scenarios:

  • Search results show:“Find {{ result_count }} item{{ result_count|pluralize }}.”
  • Shopping cart item quantity:“There are {{ cart_items_count }} product{{ cart_items_count|pluralize }} in the shopping cart.”
  • Comment Count:“There are {{ comment_count }} comment{{ comment_count|pluralize }}.”
  • Notification message:You have {{ message_count }} new message{{ message_count|pluralize }}.

By flexible applicationpluralizeFilter, the text output of your website will be smoother, more natural, and provide users with a better reading experience.


Frequently Asked Questions (FAQ)

1.pluralizeIs the filter applicable to the singular and plural conversion of all languages?CurrentlypluralizeThe filter is mainly designed for English singular and plural rules. For other languages (such as Chinese, which does not have the concept of singular and plural, or French, German, and other languages with complex plural rules), the filter may not be applicable, or may require additional logic processing.In AnQiCMS, the multilingual feature is usually implemented through language packages (trLabel) or template-level conditional judgment to complete.

2. Can I use forpluralizeSet three or more custom suffixes for the filter to handle more complex plural rules? pluralizeThe filter currently supports two custom modes: not providing a custom suffix (the default adds 's'), providing a single custom suffix (used for 0/many, 1 remains unchanged), or providing two custom suffixes separated by a comma (used for 1, and 0/many, respectively).It does not directly support setting three or more suffixes. If encountering extremely special plural forms, it may be necessary to combineifLogic judgment to handle.

How can I handle not displaying the plural form when the quantity is 0 (e.g., '0 cherry' instead of '0 cherries')? pluralizeThe default behavior of the filter is to apply plural form when the quantity is 0. If you want to not display plural (for examplecherr{{ 0|pluralize:"y,ies" }}still outputcherries),then consider adding one in the templateifcondition judgment to handle speciallycountWith0situation, or adjust the logic of custom suffixes. For example, you can use{% if count == 0 %}0 {{ "cherry" }}{% else %}{{ count }} cherr{{ count|pluralize:"y,ies" }}{% endif %}This way of doing things allows for finer control.