How to display the AnQiCMS built-in system language package switch feature on the front page?

Calendar 👁️ 65

AnQi CMS provides a flexible mechanism that allows your website to better serve users of different languages around the world.When you want to implement language switching on the front page, AnQiCMS built-in tags and backend configuration can well meet this need.

Understand the AnQiCMS language package mechanism

Firstly, we need to clarify the concept of the language package in AnQiCMS. The built-in system language package of AnQiCMS is mainly responsible for switching the website backend interface as well as the front-end page ofBuilt-in prompt information, tags, etc.language. For example, date format, system messages, or certain pre-set text in templates that is unrelated to the content of the article.

It should be noted that this feature does nottranslate automaticallyThe specific content edited and created by users, such as articles, product descriptions, and category names published on your website.This content's multilingual support needs to be implemented through other means (such as creating different language versions during content editing or using template translation tags).AnQiCMS isv2.1.1The version has added support for the default language package, which lays a foundation for the international operation of the website.

Enable and configure the default language in the background

To display the language switch feature on the front end, you first need to make some basic settings in the AnQiCMS background.

  1. Enter the global function settings:Log in to your AnQiCMS backend, find the "Backend Settings" menu in the left navigation bar, and then click "Global Function Settings".
  2. Set the default language package:In the global feature settings page, you will see an option named "Default language package".AnQiCMS currently has built-in Chinese and English language packs.You can choose the default language for your website. This setting will affect the system built-in language loaded by default.

Although this setting itself does not directly display the language switch button on the front end, it ensures the basic environment of the website for handling different languages.

Display the language switch function on the front-end page.

AnQiCMS provides a dedicated template tag{% languages %}To get all the multilingual site information that has been configured, you can use this tag to build the front-end language switcher.

Use{% languages %}Tag to get the language list

You can add the following code snippet to any location in the website template (usually in the header, footer, or sidebar) to display a simple language switch menu:

{%- languages websites %}
{%- if websites %}
<div class="language-switcher">
    <span>切换语言:</span>
    {%- for item in websites %}
    <a href="{{item.Link}}">
        {%- if item.LanguageIcon %}
        <img src="{{item.LanguageIcon}}" alt="{{item.LanguageName}}"/>
        {%- else %}
        {{item.LanguageEmoji}}
        {% endif %}
        {{item.LanguageName}}
    </a>
    {%- endfor %}
</div>
{%- endif %}
{%- endLanguages %}

This code will traversewebsitesEach language site information in the variable, and generate a link for each language.

Code analysis:

  • {% languages websites %}This is the core tag, it will load all available language site information towebsitesthe variable.
  • {%- if websites %}Check if there is a language site, and if it exists, continue rendering.{%-and-%}Used to remove blank lines generated by tags, keeping the HTML output tidy.
  • {%- for item in websites %}: Loop through.websitesarray,itemThe variable represents a language site in each loop.
  • {{item.Link}}This is the access link to the language site. Clicking it will redirect the user to the corresponding language version page.
  • {{item.LanguageIcon}}If your language configuration has an icon (such as a national flag icon), it will be displayed here.
  • {{item.LanguageEmoji}}If there is no icon, you can choose to display the Emoji corresponding to the language.
  • {{item.LanguageName}}: Display the name of the language (such as "Chinese", "English").

You can flexibly style these elements according to your website design, such as placing them in a dropdown menu, pop-up window, or a simple horizontal list.

Add Hreflang tag for SEO optimization

For multilingual websites, it is strongly recommended to place the page in the correct version for different language users so that search engines can better understand the content of different language versions of the page and display the correct version in search results.<head>Partially addhreflangLabel. This helps to avoid duplicate content issues and improve user experience.

You can use it in thebase.htmlThe file (usually the common template inherited by all pages) of<head>Enter the code inside the tag:

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

Code analysis:

  • <link rel="alternate" ...>This ishreflangThe standard format of the label.
  • href="{{item.Link}}": Points to another language version of the current page URL.
  • hreflang="{{item.Language}}": Specifies the language code corresponding to the URL (for examplezh-CNrepresenting Simplified Chinese,enrepresents English).

Deep-level template content multilingual processing

The system language package and switcher mentioned earlier, mainly for displaying language at the system and site levels. If you want to customize the text content in the website templateCustom text contentCan also achieve multilingual display, AnQiCMS also provides{% tr %}Tags andlocalesdirectory mechanism.

  1. Create a language file:Under your template directory, create a folder namedlocales. Then in thelocalesIn the folder, create a subfolder for each supported language (for exampleen-us/zh-cnCreate a subfolder in eachdefault.ymlfile.
    • For example,locales/zh-cn/default.ymlCan includeyourLocation: "您的位置".
    • locales/en-us/default.ymlthen containsyourLocation: "Your Location".
  2. Use{% tr %}Tags:In the template, you can use{% tr "yourLocation" %}In this way to call the translated text. The system will automatically load the correspondingdefault.ymltranslations.

This allows you to finely control all hard-coded text versions in the template, combined with the front-end language switcher, providing users with a comprehensive localization experience.

By following these steps, your AnQiCMS website will be able to provide a user-friendly language switching feature, offering a browsing experience that is more in line with the habits of different language users, while also taking into account the needs of search engine optimization.


Frequently Asked Questions (FAQ)

  1. Q: Will the language package switch feature automatically translate the content of my website articles?A: Will not. The built-in system language package switching function of AnQiCMS is mainly aimed at the background interface of the website and the system built-in prompts, navigation tags, etc. on the front-end page.The specific content published on your website, such as articles and product descriptions, requires you to manually create content in different languages or integrate third-party translation tools.

  2. Q: If I want to support languages other than Chinese and English, such as French or German, does AnQiCMS support it?A: The AnQiCMS built-in system language package currently mainly provides Chinese and English options. But you can create customlocaleslanguage files (such aslocales/fr/default.ymlfor French), and combine{% tr %}Translate the custom text in your template. For the system's core interface and labels, deeper secondary development may be required to expand language support.

  3. Q:hreflangWhat specific help do tags provide for website SEO?A:hreflangTags inform search engines that a specific page is an alternative version of another page's content, tailored for different languages or regions. This helps:

    • Avoid the penalty for duplicate content:Even if the content is similar, but the language or region is different, the search engine will correctly identify it and avoid treating it as duplicate content.
    • Improve localized search rankings:Ensure that users can find the content version most suitable for their language and region when searching.
    • Improve user experience:The search engine can directly guide users to their preferred language version, reducing the bounce rate.

Related articles

How to display the unique content and images of a custom page (single page)?

In website operation, we often need to create some unique content and personalized page layouts, such as 'About Us', 'Contact Information', or a specific event detail page.AnQiCMS (AnQiCMS) has provided us with a powerful custom page (single page) feature, which not only allows us to easily manage the text content of these pages, but also flexibly display their exclusive images and customized layouts.How to specifically operate, so that the custom page has its own unique content and image display method?

2025-11-07

How to batch modify keywords in the article content in the background and synchronize them to the front display?

In website content operation, the accuracy and timeliness of keywords are crucial.With market changes or SEO strategy adjustments, we often need to make batch modifications to the keywords of a large number of articles on the website.If done manually, it will undoubtedly consume a lot of time and effort.幸运的是,AnQiCMS(AnQiCMS)提供了高效的批量关键词替换功能,让这一process变得得心应手,并能确保修改后的内容无缝同步到前台显示。

2025-11-07

How to use the `tagList` tag to display the associated tag (Tag) cloud of the article?

AnqiCMS provides a series of powerful and flexible tags to help us manage and display website content more efficiently.Among many features, the article tag (Tag) is a very practical feature that can effectively organize content, enhance the internal link structure of the website, and make it convenient for visitors to quickly find related topics of interest.Today, let's delve into how to use the `tagList` tag to elegantly display related tags in the AnqiCMS template, and even build a dynamic 'tag cloud'.

2025-11-07

How to use the `archiveFilters` tag to implement multiple condition combination filtering on the front end?

AnQiCMS provides flexible and powerful content management capabilities, one very practical feature is to use the `archiveFilters` tag to implement multi-condition combination filtering on the front-end.This undoubtedly greatly enhances the user experience and the discoverability of content for websites that are rich in content and require users to search for information based on different dimensions, such as product displays, real estate rentals, recruitment information, and so on.Let's delve deeper into how to use the `archiveFilters` tag to build an intuitive and efficient filtering system for your website.

2025-11-07

How to call and display the independent domain of the website backend configured in the template?

In the flexible content management system of AnQiCMS, site operators often need to display some key background configuration information in the website template according to business needs.Among the settings such as 'Background Independent Domain', it not only concerns the security of the system, but may also need to be displayed on the website front-end in certain scenarios, such as being used as a quick access entry for internal personnel, or for information explanation on specific pages. Today, let's delve into how to cleverly call and display the independent domain you have configured in the AnQiCMS template.###

2025-11-07

How to ensure that the website displays a custom prompt to users when it is in "shutdown" status?

In website operation, we sometimes encounter situations where we need to temporarily close the website, such as for system maintenance, major upgrades, data migration, or dealing with unexpected security events.For any reason, directly allowing users to access a blank page or an error page will severely affect user experience and even damage the brand image.At this time, it is particularly important to clearly display a friendly prompt message to visitors.

2025-11-07

How to display the number of comments for each article on the article list page?

In website operation, the article list page is not only the entrance of content, but also a window for users to understand the activity level of the website.When users see that every article is accompanied by a comment count, this not only effectively enhances the interactivity and attractiveness of the article, but also brings positive social proof to the website, encouraging more readers to participate in discussions.For users who use AnQiCMS, displaying the number of comments for each article on the article list page is a very direct and practical feature.

2025-11-07

How to use the `pagination` tag to build standard pagination navigation in the template?

When the content of a website becomes richer, a clear and efficient pagination navigation system is crucial for improving user experience.The AnQiCMS template system provides a powerful `pagination` tag that allows us to easily build standard, beautiful pagination navigation.Next, we will delve deeper into how to use this tag in templates.

2025-11-07