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 is an enterprise-level content management system developed based on the Go language, and its powerful multilingual and multi-site management features are the key tools for us to achieve this goal.Judging the current language environment of the page accurately in the template can not only optimize the user experience but also greatly improve the SEO performance and content operation efficiency of the website.
Today, let's delve deeply into how to cleverly and accurately judge whether the current page is in a multilingual site environment in the AnQiCMS template, and how to combine practical application scenarios to transform these technical points into practical content operation strategies.
Adaptable to a global audience: The way AnQiCMS determines its multilingual site
AnQiCMS was designed with the need for global content promotion in mind, one of its core highlights being the seamless integration of 'multilingual 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 uniformly through a single system.But having these features is not enough, the key is how our template can intelligently 'perceive' which language version the current user is visiting, thereby dynamically adjusting the displayed content, loading specific resources, and even providing appropriate language switching options.
In the AnQiCMS template system, the language environment of the current page can mainly be determined by 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 the language information of the current site
systemThe tag is the universal key to obtain the global system configuration information in AnQiCMS templates, which includes the language settings of the current site.By 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 in the template{% system with name="Language" %}When it returns, it will return the language code configured in the background "Global Function Settings" of the current site, for examplezh-cn(Simplified Chinese),en-us(American English) orja(Japanese) and others.
With this language code, we can make conditional judgments in the template and display differentiated content according to different language codes.For example, if the current language is English, we can load an English version of the JS library;If it is in Chinese, then display Chinese-specific promotional phrases. This approach greatly enhances the adaptability of the template, avoiding the need to create a completely different template for each language version, greatly simplifying maintenance work.
Core Tool Two:languagesTag - Insight into all multilingual site overviews
In addition to getting the language of the current page, 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. At this time,languagesthe label comes into play.
{% languages websites %}This tag can get the list of all multilingual sites configured under the current AnQiCMS project.It returns an array object, where each element represents a language site and includes the site's language name, language code, link address, and even the corresponding emoji or icon.
By traversing thiswebsitesArray, we can not only know which languages the current system supports, but also dynamically generate language switching links.For example, you can create a dropdown menu at the top or bottom of the page so that visitors can easily switch to other language versions according to their needs.This is crucial for enhancing user experience and website accessibility.
Transform technology into practical strategies: Multilingual application practice in templates
Understood the working principle of these two tags, we can build flexible and versatile multilingual adaptation logic in the AnQiCMS template.
1. Load different resources or display specific content based on the current language code:
Assuming your website has two versions in Chinese and English, you may want to load a special font file on the Chinese site, or load another one on the English site, or display promotional images in different languages at some 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. Dynamically build a multilingual switcher:
Provide an intuitive language switch option, which is a basic requirement for a multilingual website. 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 SEO crucially importanthreflangTags:
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 %}