How to use the `replace` filter to replace translation logic in the AnQiCMS multilingual site?

Calendar 👁️ 66

When setting up a multilingual website on AnQiCMS, content replacement is a common operation requirement.It is crucial to be able to correct a common word or dynamically adjust text expressions according to different language environments, and replace strings flexibly and effectively.AnQiCMS provides powerfulreplaceThe filter and the完善的多语言翻译机制,ingeniously combine the two, which can achieve more accurate, more contextually adaptable content replacement strategies.

UnderstandingreplaceBasic usage of filters

replaceThe filter is the core tool used for string replacement in the AnQiCMS template engine.Its basic function is to find the specified "old" word in the string and replace it with the "new" word, and then return the new string after replacement.

Its basic syntax structure is very concise and intuitive:

{{ 目标字符串 | replace:"old,new" }}

Among them,oldandnewComma is used between them,Separated.

For example:

If you have a piece of text"欢迎使用安企CMS", and want to filter out"安企"with"anqi"Here is how to use it:

{{"欢迎使用安企CMS"|replace:"安企,anqi"}}
{# 输出结果:欢迎使用anqiCMS #}

replaceThe filter has some special usage:

  • IfoldThe parameter is an empty string, it will match at the beginning of the target string and after each UTF-8 character sequence, and insert at these positionsnewstring.
    
    {{"欢迎使用安企CMS"|replace:",-"}}
    {# 输出结果:-欢-迎-使-用-安-企-C-M-S- #}
    
  • IfnewThe parameter is an empty string, it will remove itoldstring.
    
    {{"欢迎使用安企CMS"|replace:"安企,"}}
    {# 输出结果:欢迎CMS #}
    

UnderstoodreplaceAfter the basic usage of the filter, we will take a look at the multilingual mechanism of AnQiCMS.

Overview of AnQiCMS's multilingual translation mechanism

AnQiCMS is built-in to{% tr %}Translation label, cooperatelocaleslanguage package files under the directory (usually in YAML format), which realize flexible multilingual content management.

Working principle:

  1. In the template, the text to be translated is not written directly, but is used with a translation key (key). For example{% tr "yourLocation" %}.
  2. InlocalesUnder the directory, create a folder for each supported language and place the language package files (such asdefault.yml)
  3. Language package files store translation content in the form of key-value pairs.
    • zh-cn/default.yml“`yml

Related articles

How to verify that the `replace` filter is working as expected in AnQiCMS template debugging?

During the template development process of AnQiCMS, we often encounter scenarios where we need to process string content, and the `replace` filter is one of the very practical tools.It can help us easily replace the specific part of the string.However, in practice, ensuring that this filter works in the way we expect is an indispensable part of debugging the template.### Understanding the `replace` filter AnQiCMS provides a series of powerful template filters

2025-11-08

Can the `replace` filter be used to standardize product units or currency symbols on the AnQiCMS website?

In the operation of the AnQiCMS website, maintaining the standardization of product units and currency symbols is a key factor in improving user experience and professionalism.We often need to unify the formats of data from different sources and different input habits.Then, can the `replace` filter provided by AnQiCMS effectively achieve this goal?The answer is affirmative, by cleverly using the `replace` filter, we can flexibly standardize the displayed content at the template level without modifying the data in the original database.

2025-11-08

Does the AnQiCMS `replace` filter support case-sensitive keyword replacement?

In the daily content operation of AnQiCMS, we often need to dynamically adjust the text content displayed in the website template.At this time, the powerful and flexible template engine of AnQiCMS provides a series of practical filters (filters), among which the `replace` filter is a powerful tool for string replacement.However, when using such tools, a common issue may arise: does it differentiate between uppercase and lowercase when performing keyword replacement?###

2025-11-08

How to prevent the `replace` filter from accidentally replacing parts of the template that do not need to be modified?

In website template development and content management, we often need to dynamically process page content, among which the 'replacement' operation is one of the commonly used functions.AnQi CMS provides a powerful template engine and various filters to help us meet these needs.Among many filters, the `replace` filter is favored for its直观 and convenient nature, allowing us to replace specific "old words" with "new words" in a string.

2025-11-08

How to use the `replace` filter to dynamically modify the `alt` attribute keyword of an image?

In website operation, images not only enrich the content but also can significantly improve search engine optimization (SEO) and user experience (especially for users who use screen readers) through their `alt` attribute (alternative text).AnQiCMS (AnQiCMS) with its flexible template engine, provides us with powerful tools to finely control these details.Today, let's discuss how to cleverly use the `replace` filter to dynamically modify the keywords in the `alt` attribute of images, making your website content more competitive.

2025-11-08

Can the AnQiCMS `replace` filter be used to clean up sensitive words in user comments or reviews?

How to effectively clean up sensitive words in managing website content, especially user-generated content (such as comments or reviews), is a problem that plagues many operators.AnQiCMS (AnQiCMS) is a feature-rich enterprise-level content management system that provides various ways to handle content, including the `replace` filter.Whether this `replace` filter can be used to clean up sensitive words in user comments or reviews?Let's delve into it.

2025-11-08

In AnQiCMS `archiveDetail` tag, how to use the `replace` filter for article content (`Content`)?

In the flexible and versatile content management of AnQiCMS, we often need to display content on the article detail page, and the main content of the article is usually obtained by combining the `archiveDetail` tag with the `Content` field.But sometimes, due to various operational needs, we may need to make some subtle adjustments to the content of these articles, such as unify brand names, correct specific words, or temporarily hide certain information, at this time, directly modifying the original text in the database may be inconvenient, or may require a more flexible display strategy.luckily, AnQiCMS

2025-11-08

How to unify the external link prefix displayed on the AnQiCMS page using the `replace` filter?

In website operation, the standardization of external links is often overlooked but is also very important.The prefix displayed on the unified page for external links, which not only helps enhance the professional image of the website, but also to some extent optimizes SEO performance, even facilitates subsequent data analysis or link management.AnQiCMS as a flexible content management system provides powerful template engine features, among which the `replace` filter is the tool to achieve this goal.

2025-11-08