When setting up a multilingual website with AnQiCMS, content replacement is a common operation requirement.It is crucial to be able to modify a general vocabulary or adjust the text expression dynamically according to different language environments, and to perform string replacement flexibly and effectively.replaceFilter and improve the multi-language translation mechanism, cleverly combining the two to achieve more accurate and context-adaptive content replacement strategies.
UnderstandingreplaceBasic usage of the filter
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" (old) in the string and replace it with the "new word" (new), 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:,.
举例来说:
If you have a piece of text:"欢迎使用安企CMS"And you want to extract:"安企"Replace it with"anqi"You can use it like this:
{{"欢迎使用安企CMS"|replace:"安企,anqi"}}
{# 输出结果:欢迎使用anqiCMS #}
replaceFilter has some special usages:
- If
oldThe 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- #} - If
newThe parameter is an empty string, it will be removed.oldString.{{"欢迎使用安企CMS"|replace:"安企,"}} {# 输出结果:欢迎CMS #}
UnderstoodreplaceAfter the basic usage of the filter, let's take a look at the multi-language mechanism of AnQiCMS.
AnQiCMS's multi-language translation mechanism overview
AnQiCMS through the built-in{% tr %}Translate label,配合localesThe language package file under the directory (usually in YAML format), which implements flexible multilingual content management.
Working principle:
- In the template, the text to be translated is not written directly, but is instead used with a translation key (key). For example
{% tr "yourLocation" %}. - In
localesThe directory contains, create a folder for each supported language, and place the language package files (such asdefault.yml). - Language package files store translation content in the form of key-value pairs.
- en/default.ymlEnglish`yml