How to implement quick multilingual switching of template content with translation tags in AnQiCMS?

Calendar 👁️ 55

As a senior operator of AnQiCMS (AnQiCMS) website, I am well aware of the core status of content in website operation, especially under the wave of globalization, the rapid switching of multilingual content is crucial for user experience and market expansion. AnQiCMS provides a powerful and concise multilingual support mechanism, the core of which lies in the flexible use of translation tags{% tr %}To implement the rapid multilingual switching of template content.

The foundation of AnQiCMS multilingual support

One of AnQiCMS's design concepts is to support multi-language content switching and display, aimed at helping enterprises seamlessly expand into international markets and allowing content to directly reach users of different languages.The ability to implement this global layout is reflected in the exquisite design of the template engine and the flexible configuration of the backend management system.By linking the fixed text in the template with an independent language package, AnQiCMS achieves fast dynamic translation of front-end content, greatly enhancing the adaptability and user experience of the website.

Core mechanism: translation tags{% tr %}

In AnQiCMS, the core tool for implementing template content multilingual switching is the translation tag{% tr %}. This tag is used to replace the hard-coded text in the template with the translated text defined in the language package. Its usage is intuitive and efficient:{% tr "你的翻译键" %}.

When the system parses to a{% tr "someKey" %}When labeling, it will check the language setting of the current website, look up the entry named "someKey" in the corresponding language package file, and output its value.This mechanism decouples the template structure from specific language content, making it exceptionally simple to maintain and update content for multiple language versions.

Build multilingual template file: Locales directory structure

In order to{% tr %}The label works normally, we first need to prepare translation files for different languages.AnQiCMS uses a clear directory structure to manage these language files.localesfolder. In thislocalesInside the folder, create a subfolder for each supported language, the folder name is usually the language code (for example,en-usRepresents American English,zh-cnRepresents Simplified Chinese). One can create one or more.ymllanguage package files in the format ofdefault.yml.

A typical multilingual file structure may look like this:

./locales
├── en-us
│   └── default.yml
├── ja
│   └── default.yml
└── zh-cn
    └── default.yml

default.ymlThe translation content is defined in the form of key-value pairs within the file. For example, inzh-cn/default.ymlwe may have:"yourLocation": "您的位置", and inen-us/default.ymlcorrespondingly written as:"yourLocation": "Your location". This way, when the template calls{% tr "yourLocation" %}When, the system will automatically load and display "Your location" or "Your location" based on the current language settings.

Backend language package settings and frontend switching

AnQiCMS's backend provides a 'default language package' setting, located in the 'Global Function Settings'.The administrator can select the default language of the website here, such as Chinese or English.This setting will affect the built-in display information of the system, ensuring that the background interface and some system-level prompts are displayed in the selected language.{% tr %}Content defined by tags, its switching mechanism is more dynamic.

To allow visitors to choose the language independently, we need to add language switching functionality in the frontend template.{% languages %}The tag provides convenience. By using this tag, we can retrieve the list of all configured multilingual sites and generate a switch link for each language. For example:

{%- languages websites %}
{%- if websites %}
<div>
    <span>切换语言:</span>
    {%- for item in websites %}
    <a href="{{item.Link}}">
        {%- if item.LanguageIcon %}
        <img src="{{item.LanguageIcon}}" />
        {%- else %}
        {{item.LanguageEmoji}}
        {% endif %}
        {{item.LanguageName}}
    </a>
    {%- endfor %}
</div>
{%- endif %}
{%- endlanguages %}

When the user clicks on these links, the website will load the corresponding language version of the page, at this time{% tr %}The label will be based on the new language setting, extracted and displayed from the corresponding language package file, thus realizing the rapid multi-language switching of template content. Moreover,{% languages %}Labels also support generationhreflangProperties, this is crucial for international SEO, as it helps search engines understand the content of different language versions of web pages and improve the visibility of the site globally.

Summary

AnQiCMS passes{% tr %}Translate tags and structured language package files are combined with the background language settings and{% languages %}The front-end switch function provides a complete and efficient template content multilingual switch solution.This design not only achieves the separation of template and content, simplifies the maintenance work of multilingual websites, but also ensures that users can obtain a smooth and consistent experience when switching between different language versions, laying a solid foundation for the globalization of website operations.


Frequently Asked Questions (FAQ)

1.{% tr %}Can tags only translate fixed text in templates? How can article content be displayed in multiple languages? {% tr %}The label is mainly used to translate the fixed text that is hard-coded in template files and does not change with the content of the article, such as navigation menu items, button text, auxiliary information of page titles, etc.For multi-language display of dynamic content such as articles or products, AnQiCMS usually achieves this through the multi-site management feature.{% tr %}Tags handle the translation of fixed text in templates.

How to ensure that the page URL also changes when the multilingual switch is made, for the benefit of SEO?AnQiCMS's multilingual site management and pseudo-static rules are well integrated. When you go through{% languages %}When creating language switch links, each link will point to the URL of the corresponding language site. At the same time, the system supports configurationhreflangTags({% languages websites %}{% for item in websites %}<link rel="alternate" href="{{item.Link}}" hreflang="{{item.Language}}">{% endfor %}{% endlanguages %}This helps search engines understand the relationship between different language versions of pages, thus optimizing international SEO performance.

3. If the translation key is missing in the language package file,{% tr %}How will the tag perform?If{% tr "yourKey" %}The translation key corresponding to the tagyourKeyIn the current language,.ymlIf the file does not exist, AnQiCMS will usually default to displaying the key name itself (i.e.,yourKey), or reverting to the default language translation in specific configurations.This reminds us that when adding new translation keys, we must ensure that all target language language package files contain the corresponding translations to avoid untranslated key names.

Related articles

How to use conditional judgment (if) and loop traversal (for) tags for data rendering in AnQiCMS templates?

As an experienced Anqi CMS website operations personnel, I know that flexible content presentation is crucial for attracting and retaining users.AnQiCMS is a powerful template engine, especially its conditional judgment (`if`) and loop traversal (`for`) tags, which are the core tools for realizing dynamic content rendering.They allow websites to display information flexibly according to data status and business logic while maintaining consistency in content structure.

2025-11-06

How to get the previous, next article and related documents with AnQiCMS template tags?

As a senior CMS website operation personnel in the security industry, I fully understand the importance of content in attracting and retaining users.Efficient content management and elegant user experience are the foundation of a successful website.In AnQiCMS, the flexible template tag system is the key to achieving this goal.Today, I will give you a detailed introduction on how to use AnQiCMS template tags to cleverly obtain the previous and next articles and related documents, thus optimizing your content layout, improving user experience and the website's SEO performance.### Optimize User Navigation: Get Previous Document On Article Detail Page

2025-11-06

How to obtain the current page's TDK information in AnQiCMS template and perform SEO optimization?

As an experienced website operator who deeply understands the operation of AnQiCMS, I have a very good understanding of the core value of TDK (Title, Description, Keywords) information for website search engine optimization (SEO).High-quality TDK is not only the key for search engines to understand the content of the page, but also the foundation for attracting users to click, improving website traffic and conversion rates.AnQiCMS as an enterprise-level content management system took full consideration of SEO requirements from the very beginning, providing a powerful and flexible TDK management and template calling mechanism.###

2025-11-06

How to implement flexible calls for document lists, category lists, and single page details with AnQiCMS template tags?

As a website operator who is deeply familiar with the operation of AnQiCMS, I know that content is the cornerstone of the website, and flexible and efficient content calls are the key to improving user experience and optimizing operational efficiency.AnQiCMS's powerful template tag system is the core tool we use to achieve this goal.It allows us to precisely control the display of document lists, category lists, and single-page details, thereby building website pages that are both beautiful and functional.

2025-11-06

How to enhance the display of structured data on the page with AnQiCMS's Json-LD custom call tag?

As an experienced CMS website operation person in the digital world, I know the power of content.High-quality content is the foundation for attracting and retaining users, and how to make these contents better understood and displayed by search engines is one of the core issues for our operation staff.Today, I want to delve deeply into the Anqi CMS Json-LD custom call tag, and how it has become a powerful tool for us to enhance the display of structured data on web pages.

2025-11-06

What specific SEO elements does the "Home TDK" in AnQiCMS refer to?

As an experienced website operator, I know that Search Engine Optimization (SEO) is the key to the success of a website, and the setting of the TDK (Title, Description, Keywords) on the homepage is the foundation for our SEO work.AnQiCMS is a system focused on efficient content management, which fully considers SEO requirements from the beginning of its design, providing us with a simple and powerful TDK configuration function.I will elaborate in detail on the specific SEO elements and their operational significance referred to by the home page TDK in AnQiCMS

2025-11-06

How to find and enter the 'Home TDK Settings' area in the AnQiCMS backend?

In the AnQiCMS backend management, finding and entering the "Homepage TDK Settings" area is a key step for website operators to perform basic SEO optimization.These settings directly affect the display effect of the homepage in search engine results and are an important factor in attracting potential users to visit.As an experienced website operator proficient in AnQiCMS, I will guide you through this operation effortlessly.First, make sure you have successfully installed and run the AnQiCMS website and have the login credentials for the administrator account.To access the AnQiCMS admin interface

2025-11-06

How to fill in the Title on AnQiCMS homepage to achieve **SEO effects?

As an experienced website operator who understands the operation of AnQiCMS, I am well aware of the importance of the homepage title (Title) for achieving ideal rankings in search engines.A meticulously optimized homepage title, not only attracting potential users to click, but also clearly conveying the core value of the website to search engines.In AnQiCMS, we have a dedicated module to manage this core SEO element, filling it out properly can lay a solid foundation for the long-term development of the website.

2025-11-06