yesnoHow to combine the filter with the internationalization mechanism to accurately present the three states of text.

KnowyesnoFilter and its three states

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

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

  • If the input value is judged to betrue(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 to befalse(for example, zero, an empty string, a boolean valuefalse),it will output the text corresponding to the second parameter.
  • If the input value isnil(empty value) or an uncertain state, it will output the text corresponding to the third parameter.

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

{{ archive.Status|yesno }}

Ifarchive.Statusresponse fortrue,output 'yes'; isfalse,output 'no'; isnil,output 'maybe'.

The challenge of internationalized text.

Althoughyesno过滤器能方便地展示三种状态,但其默认输出的 “yes”、”no”、”maybe” 是英文,这对于多语言网站显然不够。We hope to display "YesWriting translations directly in the template makes the template code long and difficult to maintain, and it also cannot achieve automatic language switching.

This is where the internationalization translation mechanism of Anqi CMS comes into play.

The internationalization translation mechanism of Anqi CMS (trTag)

Anqi CMS provides atr(translate) Tag used to retrieve translated text from a predefined language package. To use it, we need to place it in the root directory of the template.localesCreate a corresponding YAML file for 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 use{% tr "翻译键名" %}to call the corresponding translation text.The CMS will automatically load the correct language file based on the current website's language settings (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”.

yesnoFilter is related totrThe perfect combination of labels

Now, let's takeyesnoThree states parameters of the filter andtrCombine the tags to achieve true internationalization.yesnoThe filter allows us to customize the three states text of the output,trLabel can exactly provide multilingual text for these states.

Suppose ourarchive.IsPublishedfield indicates whether the article has been published(truemeans published,falsemeans not published,nilRepresents a 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"YestrThe parameters of the tag, and the whole{% tr '...' %}The calculation result of the expression will be asyesnoA status parameter of a filter.

  • Whenarchive.IsPublishedresponse fortruewhenyesnoThe filter will call{% tr 'published' %}The returned text.
  • Whenarchive.IsPublishedresponse forfalsewhenyesnoThe filter will call{% tr 'unpublished' %}The returned text.
  • Whenarchive.IsPublishedresponse fornilwhenyesnoThe filter will call{% tr 'draft_or_pending' %}The returned text.

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

Tips for practice

  • Translate key names uniformly: Maintain translation key names (such aspublished/unpublished) consistent across all language files. This is the foundation of internationalization.
  • Default language package settingsIn the "Global Function Settings" of the Anqi CMS backend, you can set the default language package for the website. At the same time,tag-/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 environmentEnglish: During development and deployment, make sure to test with different languages to ensure allyesnofilter combined withtrEnglish: The tags where the translation text should be displayed correctly.
  • English: Handle missing translations:If the translation key does not exist in the current language file, AnQi CMS'strtags usually output the key name itself (for examplepublishedThis helps us find and supplement missing translations.

Through this method, Anqi CMS not only simplifies multilingual content management, but also provides powerful and intuitive tools for template developers to meet various complex internationalization display requirements.


Common Questions (FAQ)

  1. How does Anqi CMS know which language package to use for translation?The CMS will load the corresponding language package according to the current language setting of the website.This setting can be configured in the "Global Feature Settings" in the background, or dynamically changed via the language switch function provided by the front-end (if the template supports it).localesmatch with the language files under 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