Under the impetus of the wave of globalization, the multilingual capability of websites is no longer an embellishment, but a necessity for reaching a wider user base.As an experienced operator of AnQiCMS, I am well aware of the importance of an efficient and maintainable multilingual template for a company to expand into the international market.Today, let's delve into the development conventions and **practices to be followed when developing AnQiCMS multilingual templates, so that your website can shine on the global stage.

AnQiCMS is a modern content management system developed based on the Go language, with its lightweight, efficient, and extensible features, it provides a solid foundation for building multilingual websites.It is built-in with a powerful multilingual support mechanism, allowing you to flexibly manage and display content in different languages.

One, the core principle: clear language structure and content separation

The primary principle of developing multilingual templates is to achieve a complete separation of template structure and language content.This means that your template code should maintain generality and not embed any specific language text directly.On the contrary, all translatable text should be managed and called through the AnQiCMS language package mechanism.

AnQiCMS through its 'Multi-language Support' feature, allows you to configure different language versions for the website, and through the 'Default Language Package' feature in the system backend, set the main language of the website.And at the template level, we use special tags to associate static text with language packages and dynamically load the content of the required language.

Two, the art of organizing template files and directories

Good file organization is the foundation for the success of multilingual templates.AnQiCMS has clear conventions for storing template files and static resources. Adhering to these conventions can make your multilingual template structure clear and easy to maintain.

Firstly, all template files are.htmlAs a suffix, store uniformly in/templateUnder the directory. Each independent template topic should be/templatehave its own dedicated directory, and place one in it.config.jsonA file used to describe the basic information of the template, such as the template name, version, description, and crucialtemplate_type.template_typeDetermines the adaptation mode of the template:0For adaptive,1For code adaptation,2For computer + mobile independent mode.

A key practice for multilingual templates is to create in the template directory.localesFolder. Inside this folder, you need to create a subdirectory for each supported language, such asen-usRepresents American English,zh-CNrepresenting Simplified Chinese. Usually, there will be adefault.yml(or any named.ymlfile) stores the static text translation of this language version in the form of key-value pairs. For example,"yourLocation": "您的位置"This structure allows the template to dynamically load the correct translation text based on the current website language setting, thereby achieving true separation of language content.

In addition, if your website needs to provide an independent template for mobile devices rather than simple adaptation, AnQiCMS supports creating in the template theme directorymobile/subdirectory. When you areconfig.jsonthe middle oftemplate_typeis set to1or2At this time, the system will attempt to loadmobile/Directory template.In a multilingual scenario, this means that you may need to provide independent templates or at least independent language package configurations for each language's mobile page to ensure that users can have a smooth and localized experience regardless of the device they access.

Three, flexibly use template tags to dynamically switch content

The template tags of AnQiCMS are the core tools for implementing multilingual functionality. They allow you to dynamically display content in different languages without modifying the code logic.

1. Global language pack application ({% tr %})What was mentioned earlierlocalesThe catalog and its.ymlfile, is exactly through{% tr %}tags to call. When you encounter static text that needs to be translated in the template, just use{% tr "键名" %}The way to refer. The system will automatically search and display the corresponding translated text based on the current language setting of the website. For example,{% tr "yourLocation" %}In Chinese environment, it may display "Your locationThis method greatly facilitates the unified management and quick switching of static text.

2. Multi-site and cross-language data calls (siteIdparameters)AnQiCMS's powerful multi-site management function plays a crucial role in multilingual template development. Many core template tags, such asarchiveList(Document list),categoryList(Category list),pageList(Single-page list),system(System settings),contact(Contact information)etc., supports onesiteIdparameter. This means you can specify in the same templatesiteIdto easily retrieve and display specific content from different language sites.

For example, if your main site is in Chinese but you want to display the latest product information of an English site in a certain block, then you can{% archiveList %}specify the English site in the tagsiteIdTo get the data. This provides great flexibility for building complex cross-language content aggregation or sharing, avoiding the need to enter and manage a large amount of content repeatedly.

3. Dynamic language switching and hreflang ({% languages %},{% system %})For easy language switching for users and better multilingual SEO in search engines, {% languages %}Labels are indispensable.It can dynamically retrieve the list of all configured multilingual sites, including their names, language codes, links, and other information.

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

Furthermore,{% languages %}Tags also provide crucial support for SEOhreflangInformation. You can use it in the HTML area to generate:<head>:“

{% languages websites %}
{% for item in websites %}
<link rel="alternate" href="{{item.Link}}" hreflang="{{item.Language}}">
{% endfor %}
{% endlanguages %}

This can effectively inform search engines that your website has content in different languages or regions, avoiding duplicate content issues and guiding users to the page best suited to their language preferences. At the same time, through{% system with name='Language' %}Label, you can obtain the language code of the current website and apply it to<html>label'slangproperties to enhance the semantics and accessibility of the page.

4. Localizing TDK and standard links ({% tdk %})Search engine optimization (SEO) is also important in multilingual websites. AnQiCMS{% tdk %}Tags allow you to label each page'stitle/keywordsanddescriptionSet localization content. In multilingual templates, you should ensure that each language version provides independent TDK information to better match local user search habits.

Furthermore,{% tdk with name="CanonicalUrl" %}Labels can retrieve the standardized link of the page. On multilingual websites, especially between pages with highly similar content but different languages, it is important to set correctlycanonicalLinks are crucial for avoiding search engine penalties. For example, you can ensure that English pagescanonicallink to their English versions, and Chinese pages link to their Chinese versions.

Section 4: Encoding and Compatibility: Ensure accessibility display

In multilingual template development, encoding issues are common pitfalls.AnQiCMS explicitly requires that template files be uniformly encoded in UTF-8.This is a universally used character encoding standard that can ensure your template can correctly display various language characters including Chinese and English, and avoid garbled code problems.When editing template files, especially for Windows users, please make sure to save it in UTF-8 format.

In addition, the template engine of AnQiCMS is similar to Django syntax, and the variables are enclosed in double curly braces{{变量}}Define, conditional judgment and loop control tags use single curly braces and the percent sign `{% tag %}`