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