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