As an experienced website operations expert, I am well aware of the importance of a powerful and flexible content management system for a company's globalization strategy.AnQiCMS, with its excellent multilingual support capabilities, has become a powerful assistant for many small and medium-sized enterprises to expand into the international market.In the development of templates for multilingual websites, how to accurately determine the active language of the page being browsed by the current user is the key to personalized content display and optimizing user experience.Today, we will delve deeply into how to useLanguageThe 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' %}