How to display the multi-language content switch button on the website front end?

Calendar 👁️ 90

On AnQiCMS, it is crucial to display a multilingual content switch button on the website front-end to expand the global user base and improve user experience.AnQiCMS with its powerful multilingual support and flexible template mechanism makes it intuitive and efficient to implement this feature.

Overview of the multilingual mechanism in AnQiCMS

AnQiCMS was designed from the outset to consider the global content publishing needs.Its multilingual support is centered around the "multi-site management" feature.This means, you can create different sites to represent different language versions.For example, you can have ayourwebsite.comAnother as a Chinese siteen.yourwebsite.comoryourwebsite.com/enAs an English site. Each site can have its own domain, content, and even default language package settings, thereby achieving true multi-language content isolation and management.

In the background, under the "System Settings", you will find the "Default Language Pack" option.Although it can be set to Chinese or English and other default languages, this mainly affects the AnQiCMS backend management interface and some built-in system prompts.For the articles, products, categories, and other content displayed on your website's front end, they are bound to specific sites and will not be automatically translated or switched due to changes made here to the 'default language package'.Therefore, to implement the front-end multi-language switching, the key is to create and manage each site's content for different language versions properly.

Deploy multilingual site: Basic preparation

Before displaying the language switch button on the front end, you need to set up the corresponding multilingual site in the AnQiCMS backend. This usually involves:

  1. Plan the domain name or subdirectory:Determine the URL structure your multilingual site will use, for example:

    • Independent domain: yourwebsite.com(Chinese),yourwebsite.de(German)
    • Subdomain: zh.yourwebsite.com(Chinese),en.yourwebsite.com(English)
    • Subdirectory: yourwebsite.com(Chinese),yourwebsite.com/en(English) AnQiCMS's multi-site management is very flexible and supports these patterns well.
  2. Create a new site:Log in to your main site backend and go to the 'Multi-site Management' feature.Here, you can “add a new site”, configure independent site information for each language version, including site name, website address, database name (used to store the data of the independent language site), and administrator account password, etc.To ensure content isolation, each language site should use an independent database.

By such settings, each language version has a complete, independently operated AnQiCMS instance, and you can target the publication and management of content in different languages.

Core: Add a language switch button in the template

After your multilingual site is configured and running, the next step is to add actual language switch buttons to the front-end template. AnQiCMS provides a very convenient template tag.languagesGet the list of all configured multilingual sites.

This tag uses a very concise method, it does not require any parameters, and it will automatically traverse all the multi-language site information you have configured in your background.It will return an array containing detailed information for each language site, and you can iterate over this array to generate corresponding language switch links.

Usually, you would place this code in the public part of the website, such as the header, footer, or sidebar, so that users can switch languages at any time. These public parts in the AnQiCMS template are usually throughbash.htmlorpartialManage the files under the directory.

Below is a segment applied in the template.languagesLabel example:

{%- languages websites %}
{%- if websites %}
<div class="language-switcher">
    <span>选择语言:</span>
    {%- for item in websites %}
    <a href="{{item.Link}}" class="language-option">
        {%- if item.LanguageIcon %} {# 如果配置了语言图标,则显示图标 #}
        <img src="{{item.LanguageIcon}}" alt="{{item.LanguageName}}">
        {%- else %} {# 否则显示Emoji或语言名称 #}
        {{item.LanguageEmoji}}
        {% endif %}
        {{item.LanguageName}}
    </a>
    {%- endfor %}
</div>
{%- endif %}
{%- endLanguages %}

This code first checks if there is a multi-language site configuration ({% if websites %}If it exists, it will loop through each site.itemIt is the current language site object in the loop, you can access it throughitem.Linkto get the link address of the language site,item.LanguageNameGet the language name (such as "Chinese"),item.LanguageEmojiGet the Emoji表情 corresponding to the language (such as🇨🇳), as well asitem.LanguageIconGet the URL of the language icon. You can choose to display the icon, Emoji, or plain text according to your design preference.

Insert this code snippet into your template.bash.htmlIn the file (usually responsible for rendering the top navigation or bottom copyright information of the page), or create a separatepartial/language_switcher.htmlfile, then use{% include "partial/language_switcher.html" %}to include it inbash.htmlChinese. In this way, your website front-end will appear a switch area containing all language options.

Optimization and details: improving user experience

In order to make the multilingual switching function more perfect and user-friendly, some details are worth paying attention to:

  • Hreflang tag settings:For search engine optimization (SEO),hreflangTags are an important way to tell search engines that your website has different language versions. They can help search engines display the correct language content to users of the corresponding language. In your template's<head>section, you can make use oflanguagesLabels to dynamically generate thesehreflang:

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

    This code will generate a label for each language sitelinkwhereitem.LinkIs the URL of the corresponding language site,item.LanguageIs the ISO 639-1 language code for the language (such as “zh-CN” or “en-US”).

  • Style and Position:The style and position of the language switch button greatly affect the user experience. You can use CSS to.language-switcherand.language-optionClass beautification to keep consistent with the overall design style of the website.The common practice is to place it at the top right corner of the website, in the footer area, or integrate it as a dropdown menu into the main navigation.

  • Highlight the current language (advanced):AlthoughlanguagesThe tag itself does not directly provide a way to determine the language of the current site.IsCurrentattribute, but you can compare it using frontend JavaScript or backend template logic,item.Linkor with the URL of the current page or

Related articles

How to configure URL rewrite rules in Anqi CMS to optimize search engine crawling?

In website operation, Search Engine Optimization (SEO) plays a crucial role, and a friendly URL structure is a key factor in improving the performance of a website in search engines.A clear and meaningful URL not only helps search engines better understand the content of the page, but also allows visitors to see the theme of the page at a glance, enhancing the user experience.Traditional dynamic URLs often contain parameters that are difficult to understand, which is not friendly to SEO and users.Fortunately, AnQi CMS provides a powerful static rule configuration function, allowing you to easily optimize the URL structure of your website.

2025-11-07

How to display a custom SEO title on the article detail page instead of the article title?

In content operation, setting the title of the website article detail page is a key link.We often encounter such situations: the content title of the article itself needs to be eye-catching and creative, in order to attract readers' clicks;And the page title required for search engine optimization (SEO) (i.e., the content of the `<title>` tag displayed on the browser tab), is more focused on keyword layout, length control, and search engine friendliness.In AnQiCMS, you can fully flexibly manage these two different titles to ensure both user experience and SEO effectiveness.

2025-11-07

How to use the `length` filter to get the length of a string, array, or key-value pair for display or judgment

In website content operation, we often encounter scenarios where we need to obtain or judge the length of data.For example, displaying the number of words in an article, the number of items in a list, or deciding whether to display an element based on the content length.AnQiCMS's template engine provides a series of practical filters to help us meet these needs, among which the `length` and `length_is` filters are the best.They can help us easily achieve these functions, making the dynamic display and interaction logic of the website more flexible.

2025-11-07

How to use the `contain` filter in AnQiCMS template to determine whether a string or an array contains a specific keyword?

During the process of building a website on AnQiCMS, flexibly displaying information according to the characteristics of the content is the key to improving user experience and operational efficiency.To achieve this, the system has built-in many practical template filters, among which the `contain` filter is a powerful tool for determining whether the content contains specific keywords.It can help you match content directly at the template level without modifying the backend code, thereby making your website content display more intelligent and personalized.

2025-11-07

Where is the TDK (Title, Keywords, Description) information of the home page set and displayed?

The TDK (Title, Keywords, Description) information on the homepage is crucial for the performance of the website in search engines.It is not only the key for search engines to understand the core content of a website, but also an important basis for users to decide whether to click on a website in the search results page.AnQi CMS is an efficient content management system that provides an intuitive and convenient TDK management function, allowing us to easily optimize the visibility of the website homepage in search engines.

2025-11-07

How to customize the template layout of article, product, and category pages?

In AnQi CMS, flexibly customizing the page template layout is a key step to enhancing website personalization and user experience.Whether it is an article, product detail page, or category list page, AnQi CMS provides powerful and intuitive tools to allow you to create unique and stylish pages without delving into programming.Let's explore how to bring your design concept to life in Anqi CMS.

2025-11-07

How to get and display the thumbnail or cover image of the article in the template?

The Anqi CMS has always been highly praised for its flexibility in content display, where images act as the core elements to attract users and convey information. How to efficiently and accurately obtain and display the thumbnail or cover image of the article in the template is a focus for many website operators.This article will thoroughly explain the Anqi CMS template mechanism and guide you step by step to master the skills of image calls.How does AnQi CMS intelligently handle images

2025-11-07

How to use the `archiveList` tag to sort and display the most popular article list by views?

How to display the popular article list by using the `archiveList` tag in AnQi CMS?In content operations, clearly displaying popular articles on the website to visitors can effectively improve user experience, guide users to discover more exciting content, and indirectly promote search engine optimization (SEO) through effective content distribution.AnQi CMS provides a powerful and flexible template tag feature, where the `archiveList` tag is the core tool to achieve this goal.

2025-11-07