How to determine if the current page is in a multi-language site environment in the AnQiCMS template?

As an experienced website operations expert, I am well aware of the importance of how templates intelligently adapt to different language versions in a multilingual site environment.AnQiCMS as an enterprise-level content management system developed based on Go language, its powerful multilingual and multisite management functions are the key tools to achieve our goal.Accurately judge the current page's language environment in the template can not only optimize the user experience, but also greatly enhance the website's SEO performance and content operation efficiency.

Today, let's delve into how to cleverly and accurately judge whether the current page is in a multilingual site environment in AnQiCMS templates, and combine these technical points with practical application scenarios to transform them into practical content operation strategies.


Flexible adaptation to global audiences: The way AnQiCMS determines multilingual sites

AnQiCMS was designed with the needs of global content promotion in mind, and one of its core highlights is the seamless integration of 'multi-language support' and 'multi-site management'.This means you can easily create independent sites for different language audiences or different language versions of the same site, and manage them统一 through a single system.But having these features is not enough; the key lies in how our templates intelligently 'perceive' which language version the current user is accessing, so as to dynamically adjust the displayed content, load specific resources, and even provide appropriate language switching options.

In AnQiCMS template system, the main way to determine the current page's language environment can be achieved with the following two core tags:systemTags andlanguagesTags. They are like the 'eyes' of a template, able to perceive the language status of the current site and the available multilingual options.

Core Tool One:systemTag - Get language information of the current site

systemLabels are the universal keys to obtain system global configuration information in AnQiCMS templates, including the current site's language settings.Through this tag, we can directly obtain the language code used by the current page, which is usually the most direct and effective way to judge the current language environment.

When you call a tag in a template and specify a variable name (such as){% system with name="Language" %}It returns the language code configured in the background “Global Feature Settings” of the current site, for examplezh-cn(Simplified Chinese),en-us(American English) orja(Japanese) etc.

Core Tool Two:languagesLabel - Insight all multilingual site summaries

In addition to getting the current page's language, sometimes we also need to understand which multilingual sites are configured in the entire AnQiCMS system, for example, by providing a language switcher at the bottom of the page.languagesLabels come in handy.

{% languages websites %}This tag can get the list of all configured multi-language sites under the current AnQiCMS project.It returns an array object, where each element represents a language site and includes the name of the language, language code, link address, and can also be configured with corresponding emojis or icons.

Through iteration of thiswebsitesArray, we can not only know which languages the current system supports, but also dynamically generate language switch links.For example, you can create a dropdown menu at the top or bottom of the page, allowing visitors to easily switch to other language versions according to their needs.This is crucial for improving user experience and website accessibility.

Transforming technology into practical strategies: multilingual application practice in templates

Understood the working principle of these two tags, we can build flexible and variable multi-language adaptation logic in the AnQiCMS template.

1. Load different resources or display specific content based on the current language code:

The website you assume has both Chinese and English versions, you may wish to load a special font file on the Chinese site, or load another on the English site, or display promotional images in different languages at certain ad positions.

{# 获取当前站点的语言代码 #}
{%- system currentLang with name="Language" %}

<head>
    <meta charset="UTF-8">
    <title>{% tdk with name="Title" siteName=true %}</title>
    {# ... 其他通用head内容 ... #}

    {%- if currentLang == "en-us" %}
        {# 英文站专属样式或JS #}
        <link rel="stylesheet" href="{{ system.TemplateUrl }}/css/en-specific.css">
        <script src="{{ system.TemplateUrl }}/js/en-analytics.js"></script>
    {%- elif currentLang == "zh-cn" %}
        {# 中文站专属样式或JS #}
        <link rel="stylesheet" href="{{ system.TemplateUrl }}/css/zh-specific.css">
        <script src="{{ system.TemplateUrl }}/js/zh-analytics.js"></script>
    {%- else %}
        {# 默认或Fallback样式/JS #}
        <link rel="stylesheet" href="{{ system.TemplateUrl }}/css/default.css">
    {%- endif %}
</head>
<body>
    {# ... 页面主体内容 ... #}

    {%- if currentLang == "en-us" %}
        <p>This content is specifically for our English audience.</p>
    {%- elif currentLang == "zh-cn" %}
        <p>此内容专为我们的中文读者呈现。</p>
    {%- endif %}

    {# ... 其他页面元素 ... #}
</body>

2. English dynamic building multilingual switcher:

Provide an intuitive language switch option, which is a basic requirement for multilingual websites. Uselanguagestags, we can easily achieve this.

{# 获取当前站点的语言代码,用于判断当前激活状态 #}
{%- system currentLang with name="Language" %}

{%- languages websites %}
{%- if websites|length > 1 %} {# 只有当存在多于一个语言站点时,才显示切换器 #}
<div class="language-switcher">
    <span>切换语言:</span>
    {%- for item in websites %}
    <a href="{{item.Link}}" class="language-option {% if currentLang == item.Language %}active{% endif %}">
        {%- if item.LanguageIcon %} {# 优先显示后台配置的语言图标 #}
            <img src="{{item.LanguageIcon}}" alt="{{item.LanguageName}}">
        {%- else %} {# 如果没有图标,则显示语言表情符号 #}
            {{item.LanguageEmoji}}
        {% endif %}
        {{item.LanguageName}}
    </a>
    {%- endfor %}
</div>
{%- endif %}
{%- endlanguages %}

3. Add the SEO crucialhreflangTags:

hreflangTags can tell search engines that your website has content in different languages or regional versions, which is crucial for avoiding duplicate content penalties and ensuring that search engines display the correct content to the right users.

`twig

{# ... 其他head内容 ... #}
{%- languages websites %}