How to set the internationalized text for three states in the `yesno` filter of AnQiCMS template for a multilingual website?

Calendar 👁️ 57

How to ensure that the dynamic text in the template of a multilingual website can be displayed correctly according to the visitor's language setting is a challenge often faced by website operators.AnQiCMS (AnQiCMS) provides an elegant solution with its flexible template engine and powerful multilingual support.Today, let's delve deeply into the AnQiCMS template in a multilingual website,yesnoHow does the filter combine with the internationalization mechanism to accurately present the three status text.

Get to knowyesnoFilter and its three states

When dealing with boolean or boolean-like data, we often need to convert it into user-friendly text. In the AnqiCMS template,yesnoThe filter is born for this purpose. Its basic function is to output one of the three preset texts based on the truth value or null status of the input value.

To be specific,yesnoThe filter will judge and output according to the following rules:

  • If the input value is judged astrue(for example, a non-zero number, a non-empty string, a boolean valuetrue), it will output the text corresponding to the first parameter.
  • If the input value is judged asfalse(for example, zero, an empty string, a boolean valuefalseIt will output the second parameter's text.
  • If the input value isnil(empty value) or an uncertain state, it will output the third parameter's text.

If there is no foryesnoThe filter provides custom parameters, it will default output 'yes', 'no', and 'maybe'. For example, suppose we have aarchive.Statusfield, its value may betrue/falseornil:

{{ archive.Status|yesno }}

Ifarchive.StatusWithtrueOutput 'yes'; forfalseOutput 'no'; fornilOutput 'maybe'.

The challenge of internationalized text.

ThoughyesnoThe filter can conveniently display three states, but its default output of 'yes', 'no', 'maybe' is in English, which is obviously insufficient for multilingual websites.We hope to display "Yes", "No", "Unknown" in the Chinese environment, and "Oui", "Non", "Peut-être" in the French environment, etc.Directly hardcoding these translations in the template not only makes the template code long and difficult to maintain, but also cannot achieve automatic language switching.

At this point, the internationalization translation mechanism of AnQi CMS comes into play.

The internationalization translation mechanism of AnQi CMS (trLabel)

AnQi CMS provides atrThe tag (translate) is used to retrieve translated text from a predefined language package. To use it, we need to be in the root directory of the template.localesCreate a YAML file corresponding to the language in the folder. For example, for Chinese, you can createzh-cn/default.yml; for English, createen-us/default.yml.

zh-cn/default.ymlExample:

"status_active": "已启用"
"status_inactive": "已禁用"
"status_unknown": "状态未知"
"approved": "已通过"
"rejected": "已拒绝"
"pending": "待处理"

en-us/default.ymlExample:

"status_active": "Active"
"status_inactive": "Inactive"
"status_unknown": "Unknown Status"
"approved": "Approved"
"rejected": "Rejected"
"pending": "Pending"

In the template, we can go through{% tr "翻译键名" %}The way to call the corresponding translation text. Anqi CMS will automatically load the correct language file based on the current website language setting (or the language selected by the user) and return the translated string.

<p>当前状态:{% tr "status_active" %}</p>

If the current website language is set to Chinese, it will output 'Current status: Enabled'; if it is set to English, it will output 'Current status: Active'.

yesnothe filter meetstrThe perfect combination of labels

Now, let's takeyesnoThree states parameters with filterstrCombined with labels, achieving true internationalization.yesnoFilters allow us to customize the three states of the output text,trThe label can exactly provide multilingual text for these states.

Suppose ourarchive.IsPublishedThe field indicates whether the article has been published (trueMeans published,falseMeans not published,nilIndicates draft or pending review. We can define the following translation keys:

zh-cn/default.ymlIn:

"published": "已发布"
"unpublished": "未发布"
"draft_or_pending": "草稿/待审核"

en-us/default.ymlIn:

"published": "Published"
"unpublished": "Unpublished"
"draft_or_pending": "Draft/Pending"

In the template, we can use it like this:

<p>发布状态:{{ archive.IsPublished|yesno:"{% tr 'published' %},{% tr 'unpublished' %},{% tr 'draft_or_pending' %}" }}</p>

Here, "published"/"unpublished"and"draft_or_pending"IstrThe parameters of the tag, while the whole{% tr '...' %}The calculation result of the expression will be used asyesnoA state parameter of the filter.

  • Whenarchive.IsPublishedWithtruethen,yesnoThe filter will call.{% tr 'published' %}The returned text.
  • Whenarchive.IsPublishedWithfalsethen,yesnoThe filter will call.{% tr 'unpublished' %}The returned text.
  • Whenarchive.IsPublishedWithnilthen,yesnoThe filter will call.{% tr 'draft_or_pending' %}The returned text.

This way, no matter which language the website is currently displaying, these three states of text can be accurately translated and presented to the user, greatly enhancing the user experience and maintainability of the multilingual website.

Tips in practice

  • Translate key names uniformly: Keep the translation of key names (such aspublished/unpublished) consistent in all language files, which is the foundation of internationalization.
  • Default language package settingsIn the global function settings of AnQi CMS backend, you can set the default language package for the website. At the same time, throughtag-/anqiapi-other/10537.htmlThe method mentioned in the document can provide language switching functionality on the front end, allowing users to choose freely.
  • Test multilingual environmentDuring development and deployment, it is imperative to switch between different languages for testing to ensure that allyesnoFilter combinationtrtags are displayed with the correct translation text.
  • are handled for missing translationsIf a translation key does not exist in the current language file, the AnQi CMS willtrtags will usually output the key name itself (for examplepublishedThis helps us discover and supplement missing translations.

In this way, Anqi CMS not only simplifies multilingual content management, but also provides powerful and intuitive tools for template developers to deal with various complex international display requirements.


Frequently Asked Questions (FAQ)

  1. How does AnQiCMS know which language package to use for translation?AnQi CMS will load the corresponding language package according to the current language setting of the website.This setting can be configured in the "Global Function Settings" on the backend, and can also be dynamically changed through the language switch feature provided by the frontend (if the template supports it).When a request is received, the system will recognize the language identifier in the current session or URL parameters and choose the correctlocalesMatching the language files in the folder.

  2. If I only need the two states of "yes" and "no",yesnoCan the third parameter in the filter be omitted? yesnoThe filter is designed to expect three parameters to correspond to three states

Related articles

What state does the `yesno` filter return by default when handling null values in AnQiCMS templates?

In Anqi CMS template development, flexibly handling various data states is the key to building dynamic pages.We often encounter situations where we need to display different content based on the true or false state of a variable.This is when the `yesno` filter becomes a very practical tool.It can help us elegantly convert boolean logic into more readable text output.### Deep understanding of `yesno` filter The `yesno` filter is a small but powerful template tool, its core function is to judge the status of a variable

2025-11-09

How to use the `yesno` filter in AnQiCMS templates to indicate whether content has been published or is in draft status?

In website content management, clearly identifying the publishing status of content is a crucial link, which can help operators and visitors quickly understand the timeliness of the information.AnQiCMS provides a flexible template system, allowing us to conveniently meet this need.Today, we will discuss how to skillfully use the `yesno` filter built into the AnQiCMS template to elegantly present the published or draft status of content.Understanding content publication status in AnQiCMS

2025-11-09

How to use the AnQiCMS template `yesno` filter to elegantly display the user's membership status (such as: member, non-member, pending review)?

In website operations, clearly displaying a user's membership status is a key element in improving user experience and guiding user behavior.We often encounter situations where we need to display different texts or styles based on the different states of users (such as whether they are members, their membership level, whether their account is active, etc.)AnQiCMS provides rich and powerful template tags and filters, among which the `yesno` filter is a very practical tool that can help us handle conditional judgments in a concise and elegant way, making the template code cleaner and easier to read.### Get to know AnQiCMS's

2025-11-09

What are the main differences and application scenarios between the `yesno` filter of AnQiCMS and the `if` statement when handling boolean values?

In the practice of template development for Anqi CMS, flexible handling of Boolean data is a part of daily work.When facing a state value representing "true" or "false", we usually use two main template syntaxes to deal with it: one is the conditional judgment `if` statement, and the other is the `yesno` formatting filter.Although they are all related to boolean values, their original intention, mechanism of action, and applicable scenarios are obviously different.Understanding these differences can help us build website content more accurately and efficiently.### `if` statement

2025-11-09

In AnQiCMS template, can the `yesno` filter determine the true or false state of a numeric or string variable?

In AnQiCMS template development, in order to better control the display logic of content, we often need to judge the 'true or false' status of a variable.This is a very practical tool, the `yesno` filter.It can help us output different texts in a concise way based on the status of variables, and it can even handle numeric and string type variables.The `yesno` filter is designed to provide a straightforward way to handle ternary states: yes, no, and uncertain (or no value).The basic working principle is to parse the input variable into a boolean value (true or false)

2025-11-09

How does the `yesno` filter combine with the data list in AnQiCMS templates to display the specific status of each piece of data?

During the process of building a website with AnQiCMS, we often need to display various data on the front-end page, which often has different states.For example, is an article published or a draft, a product listed or not, or is a user active or not.How to clearly and intuitively present these states to the user while keeping the template code concise and readable is a common concern for content operations and template developers.Today, let's delve into a very practical tool in AnQiCMS——the `yesno` filter

2025-11-09

How to implement multi-condition logical judgment with the `if` tag in AnQiCMS template (OR/&&, AND/||, NOT/!)?

In AnQiCMS template development, mastering conditional logic judgment is the key to building dynamic and intelligent web pages.Among them, the `if` tag is the core of controlling content display, not only supporting simple boolean judgments, but also being able to flexibly implement logical combinations of multiple conditions, such as "and" (`&&`), "or" (`||`), and "not" (`!`)`etc. Understanding and skillfully using these logical operators can help us control the display and hiding of template elements more finely, thereby creating a more interactive and customizable user experience.### Basic Conditional Judgment: `if`

2025-11-09

How to use the `if` tag in AnQiCMS templates to determine if a variable is empty, exists, or has a specific value?

In AnQiCMS template development, the `if` tag is the core tool for building dynamic page content.It allows us to flexibly control the display of content based on different conditions, thereby providing users with a more intelligent and personalized browsing experience.Whether you want to judge whether a data exists, whether it is empty, or hope to adjust the layout according to a specific value, the `if` tag can help you easily achieve it.AnQiCMS's template engine syntax is very similar to the Django template engine, therefore, developers familiar with this syntax will feel very亲切。`if`

2025-11-09