How to display multi-language switch links and the current language identifier in Anqi CMS?

Calendar 👁️ 58

In AnQi CMS, implementing multi-language switch links and current language identification is a very practical feature that can help your website easily face global users and enhance the internationalization level of your website.AnQi CMS provides intuitive and powerful template tags, allowing you to flexibly display these elements on the website front end.

1. Understand the multi-language capabilities of Anqi CMS

The AnQi CMS was designed from the beginning to consider the needs of multilingual sites and provides comprehensive multilingual content management support.This means you can create and manage independent content for different languages in the background, such as articles, products, pages, and so on.In the system settings, you can configure the default language pack, and each independent site can also have its own language settings.This feature not only makes content operation more efficient, but also provides a solid foundation for front-end multi-language switching.

Second, show the multilingual switch link in the template

To display a multilingual switch dropdown menu or link group on the front end of your website, you need to use the services provided by Anqí CMS.{% languages %}Template tag. This tag retrieves all the multilingual site information configured, for you to iterate through the template and generate the corresponding links.

This tag does not accept any parameters, it will directly return a collection containing information about all language sites. You can use it like a normal list, usingforLoop through this collection and extract the relevant data for each language site.

In each language site's data item, you can obtain the following key information:

  • LanguageNameLanguage display name (e.g., 'Simplified Chinese', 'English').
  • LanguageLanguage code (e.g., 'zh-CN', 'en-US'), often used forhreflang.
  • LanguageEmoji: Language corresponding emoji (e.g. 🇨🇳, 🇬🇧), convenient for direct display.
  • LanguageIconIf the language icon is uploaded on the backend, the URL of the icon will be provided here.
  • Name: The name of the site.
  • Link: The access link of the language site, which is the key to language switching.

With this information, you can add the code to display the language switch function in any location you want on the website's header, footer, sidebar, or any other place.

This is a simple example showing how to build a language switch menu:

{% languages websites %}
    {% if websites %}
        <ul class="language-switcher">
            {% for item in websites %}
                <li>
                    <a href="{{item.Link}}">
                        {% if item.LanguageIcon %}
                            <img src="{{item.LanguageIcon}}" alt="{{item.LanguageName}}" style="width: 20px; height: 20px; vertical-align: middle; margin-right: 5px;"/>
                        {% else %}
                            <span style="margin-right: 5px;">{{item.LanguageEmoji}}</span>
                        {% endif %}
                        {{item.LanguageName}}
                    </a>
                </li>
            {% endfor %}
        </ul>
    {% endif %}
{% endlanguages %}

In this code, we first go through{% languages websites %}Get the list of all language sites. Then, use{% for item in websites %}Loop through each language site. Inside the loop, we created a<a>tag, itshrefProperties are used directly{{item.Link}}to point to the site corresponding to the language. For aesthetics, we also checked whether it exists.LanguageIconIf an icon exists, display it, otherwise displayLanguageEmojiDisplay lastLanguageName.

Three, indicate the current language

In addition to providing a switch link, it is also important to clearly identify the language version the current user is browsing. Anqi CMS is in{% languages %}Each language site data returned by the tag contains oneIsCurrentField. When a language site is the language of the current page,IsCurrentThe value istrueOtherwisefalse.

Utilize thisIsCurrentField, you can add special styles to the link for switching the current language, such as highlighting, bold font, or adding an 'active' class name to visually distinguish it through CSS.

We will slightly modify the above language switching code to include the current language identifier:

{% languages websites %}
    {% if websites %}
        <ul class="language-switcher">
            {% for item in websites %}
                <li {% if item.IsCurrent %}class="active"{% endif %}> {# 如果是当前语言,添加 active 类 #}
                    <a href="{{item.Link}}">
                        {% if item.LanguageIcon %}
                            <img src="{{item.LanguageIcon}}" alt="{{item.LanguageName}}" style="width: 20px; height: 20px; vertical-align: middle; margin-right: 5px;"/>
                        {% else %}
                            <span style="margin-right: 5px;">{{item.LanguageEmoji}}</span>
                        {% endif %}
                        {{item.LanguageName}}
                    </a>
                </li>
            {% endfor %}
        </ul>
    {% endif %}
{% endlanguages %}

In your CSS file, you can define.activethe style of the class, for example:

.language-switcher li.active a {
    color: #007bff; /* 蓝色字体 */
    font-weight: bold; /* 字体加粗 */
    border-bottom: 2px solid #007bff; /* 下划线 */
}

Additionally, for the HTML document's<html>Label, usually addedlangThe attribute is used to declare the primary language of the document, which is very important for search engine optimization (SEO) and accessibility (Accessibility). You can use{% system %}Tag to retrieve the language code of the current site:

<html lang="{% system with name='Language' %}">

This ensures that your HTML document always correctly declares its language.

4. Optimizing User Experience: Hreflang Tag

For multilingual websites, in addition to the front-end switch links, you can also<head>Partially addhreflangTags are the key to SEO.hreflangThe tag tells the search engine which language versions are available for a specific page, as well as which region or language these versions are targeted at.This helps search engines display the most suitable language or regional version to users.

AnQi CMS also provides convenience for this, you can use it directly.{% languages %}to generate these tagshreflangTags:

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

Place this code in your template.<head>Part, it will automatically generate one for each language sitehreflangLink, greatly enhancing the international SEO performance of the website

V, Practical Suggestions and Flexible Application

  • Location selection:Language switchers are usually placed at the top, bottom, or easily accessible sidebar of a website to ensure users can quickly find and switch languages.
  • Customize the style:The code provides a basic structure, you can combine it with your website design, and use CSS and JavaScript to beautify the language switcher, making it consistent with the overall style.
  • Extracted as a common segment:Considering that the language switcher will appear repeatedly on multiple pages, it is recommended to encapsulate its code inpartialan independent template file in the folder (for example_language_switcher.html)中,然后通过 `{% include “partial/_language

Related articles

How to display a custom prompt message during the website shutdown maintenance of AnQi CMS?

During the operation of the website, we sometimes have to temporarily close the website for maintenance in order to upgrade the system, optimize data, or adjust functions.How to clearly and friendly convey this status to visitors, avoiding confusion or loss of users, is particularly important.Safe CMS provides a set of built-in solutions that allow you to easily customize the prompt information for your website during maintenance shutdown. ### Understanding the shutdown mechanism of AnQi CMS The shutdown feature of AnQi CMS is designed to be very intuitive and flexible.When you enable the closed site mode, the system will automatically intercept all access requests to the website content

2025-11-08

How to reference common code snippets (such as headers and footers) in Anqi CMS templates to unify page display?

It is crucial to maintain consistency in the display of web pages during website operations.A unified website appearance can not only enhance brand professionalism but also optimize user experience.AnqiCMS provides a powerful and flexible template mechanism, allowing us to efficiently manage common website elements such as headers and footers, thus avoiding repetitive code writing and ensuring the consistency of the entire site style.The AnqiCMS template system draws on the syntax of the Django template engine, with the core idea being that developers can define reusable code snippets and page skeletons

2025-11-08

How to configure content automatic external link filtering in Anqi CMS and how it affects the display of front-end links?

In website content operation, the management of external links is a vital aspect that cannot be ignored.It not only relates to user experience, but also directly affects the website's search engine optimization (SEO) performance.AnQi CMS knows this and therefore provides a flexible and practical external link handling mechanism to help content operators efficiently manage external links in articles.Where is the AnQi CMS external link configuration? AnQi CMS provides a very convenient function to handle external link issues, and you can easily configure it in the system background.The specific path is: log in to your AnQi CMS backend

2025-11-08

How to set up automatic image compression in Anqi CMS to speed up page loading?

Optimize images, speed up the website: AnqiCMS Automatic Compression Settings Guide Website access speed is one of the key factors in user experience and an important consideration for search engine optimization (SEO).And large images are often the culprit that slows down website loading speed.Fortunately, AnqiCMS, as a fully functional content management system, is built with a series of powerful image optimization features, which help us easily deal with this issue, while keeping the website smooth in providing rich visual content.Today, we will take a detailed look at how to use AnqiCMS

2025-11-08

How to automatically add hyperlinks to URLs and email addresses in article content of AnQi CMS?

In content management and website operations, automatically converting URLs and email addresses in the text to clickable hyperlinks can greatly enhance user experience and optimize the internal link structure of the website to some extent.For AnQiCMS users, implementing this feature is not through simple backend settings, but through the specific "filter" provided by its powerful template engine.This design approach gives website owners great flexibility and control, allowing them to finely manage the generation of links according to the needs of different content areas.

2025-11-08

How to truncate a long string and display an ellipsis in AnQi CMS templates?

The flexibility of content display is crucial in the template making process of Anqi CMS.When it is necessary to display a long text within a limited space, such as an article abstract, product description, or brief introduction, truncating the strings with an ellipsis is a common optimization method. It can effectively maintain the neat layout of the page and guide users to click and view the full content.AnQi CMS is developed based on Go language, its template engine syntax is similar to Django, and it provides powerful filter (filters) functions that can easily meet this requirement.### Skillfully Utilize Filters

2025-11-08

How can AnQi CMS templates inherit parent templates and rewrite specific block content to customize display?

In Anqi CMS, the template system provides a powerful and flexible mechanism that allows users to efficiently customize the layout and content display of the website.Among them, "inheriting the parent template and rewriting specific block content" is a key function to achieve personalized design and maintain consistency with the overall style of the website. ### Core Concept: Say goodbye to repetition, embrace inheritance Imagine that the header, footer, sidebar, and other elements of a website are fixed and unchanged on almost every page. If there is no template inheritance mechanism, we may need to repeat this common code in each page template, which increases the difficulty of maintenance

2025-11-08

How can AnQi CMS use the 'time factor' feature to display future published content?

How to efficiently and strategically publish content in today's fast-paced content environment is a question that every content operator is thinking about.Maintain the consistency and foresight of content publication, which can not only enhance the user experience but also effectively promote brand communication and search engine optimization.AnQi CMS deeply understands this, and its 'Time Factor' feature was born for this very purpose, providing content creators and operation teams with a precise tool to control the timing of content release.## Precise Planning: The Foundation of Content Operation We have all faced such a situation: writing an精心 prepared article

2025-11-08