LanguageThe code accurately determines the current active language.

AnQiCMS language recognition mechanism: Core and tools

AnQiCMS provides a simple and powerful mechanism to identify the active language of the current page. The core of this mechanism lies in its built-in template tag system, especiallysystemTags andlanguagesTags. They allow us to retrieve the global configuration information of the website, including the language code of the current site, and the list of all configured multilingual sites.

Firstly, to obtain the code of the currently active language, we mainly rely onsystemThe label is specifically used to extract various system parameters configured in the "Global Settings" of the AnQiCMS backend, including the current site'sLanguagecode.

You can use the following method at any location in the template to get the current language code:

{% system currentLangCode with name='Language' %}

Here, currentLangCodeIs a variable name you define to store the language code of the current site, for examplezh-CNrepresenting Simplified Chinese,en-USRepresents American English. The accuracy of this code comes directly from the configuration you made in the AnQiCMS background "Global Settings" -> "Default Language Package".

Useful template application: Make the language intelligent

After mastering the current language code, you can use it to realize various intelligent language judgments and content presentation in the template.

1. Display conditions for specific languages

The most direct application is to conditionally display different content, styles, or scripts based on the language code.This enables your website to provide highly customized experiences for users of different languages.

For example, you may want to display different greetings based on language:

{% set currentLang = '' %}
{% system currentLang with name='Language' %}

{% if currentLang == 'zh-CN' %}
    <p>欢迎访问我们的中文网站!</p>
{% elif currentLang == 'en-US' %}
    <p>Welcome to our English website!</p>
{% else %}
    <p>Welcome!</p> {# 默认或其他语言的欢迎语 #}
{% endif %}

Similarly, you can also load different CSS stylesheets or JavaScript files based on the language to adapt to the layout habits or interaction needs of different languages:

`twig

<meta charset="UTF-8">
<title>{% tdk with name='Title' siteName=true %}</title>
<link rel="stylesheet" href="/static/css/common.css">

{% set currentLang = '' %}
{% system currentLang with name='Language' %}
{% if currentLang == 'zh-CN' %}
    <link rel="stylesheet" href="/static/css/styles-chinese.css"> {# 中文专用样式 #}
{% elif currentLang == 'en-US' %}