How to dynamically generate a multilingual site navigation switcher in AnQiCMS template?

Calendar 👁️ 82

As an experienced website operations expert, I am well aware that building a multilingual website that can reach global users is an important strategy for enterprises to expand their international market and enhance their brand influence.While AnQiCMS, with its efficient, flexible architecture and powerful multi-site, multi-language support capabilities, is undoubtedly the preferred tool to achieve this goal.Today, let's delve into how to cleverly dynamically generate a practical and user-friendly multilingual site navigation switcher in the AnQiCMS template.

Overview of the multilingual mechanism in AnQiCMS

In AnQiCMS, building a multilingual site is not just simple text translation. It provides a more powerful and flexible 'multi-site management' capability, which means you can configure an independent site for each language (such as, en.yourdomain.comFor English sites,fr.yourdomain.comUsed for the French site), each site has its own URL structure, content, and even specific template configurations.This design not only ensures the clarity of content management, but also provides great convenience for SEO localization strategies.

In the "Global Function Settings" in the background, although there is an option for the "Default Language Pack", it mainly affects the language display of the background interface and some built-in prompts.For our front-end multi-language site, AnQiCMS provides a set of dedicated template tags that can dynamically retrieve and display all the language site information you have configured, which is the core of building the language switcher.

Step 1: Plan and configure multilingual sites

Before starting the template design, you need to plan and set up the multilingual site in the AnQiCMS backend. Typically, this involves the following aspects:

  1. Multi-site creation and binding:Utilize the "Multi-site Management" feature of AnQiCMS to create an independent site for each target language. Each site will be associated with a unique domain name or subdomain (such asen.yourdomain.com,fr.yourdomain.com)。In the process of creation, you can specify the default language for each site (for example, the default language for English stations is set to English).This ensures that the content of each language version is stored and managed independently.
  2. Content localization:Under each language site, you need to create or translate content for the target language, including articles, products, single pages, and so on.AnQiCMS is a powerful content model with flexible content editing features, which can well support the fine management of content in different languages.
  3. Translation file preparation (for static text):For static text in the UI interface of the website that does not belong to dynamic content (such as navigation names, button text, footer copyright information, etc.), AnQiCMS supports throughlocalesTranslate the YAML files in the directory. You need to create a folder in the template directorylocalesfor each language (for exampleen-us/zh-cn) createdefault.ymlFile, stores the corresponding key-value translation. For example,zh-cn/default.ymlmay contain"yourLocation": "您的位置"whileen-us/default.ymlIn Chinese it is"yourLocation": "Your Location". In the template, you can call the translated text through{% tr "yourLocation" %}such tags.

The second step: build the language switcher in the template

Now, we come to the most critical part - how to dynamically generate a multi-language navigation switcher in AnQiCMS. AnQiCMS provideslanguagesThe label that can help us get all the multi-language site information configured, thereby generating a switch link.

Suppose you want to place a language switcher in the header of the website, so that users can click to jump to the corresponding language site. You can place it in the template'sbash.html(This usually includes a file containing the public header of the website) or add the following code to the header area of a specific page:

{%- languages websites %}
{%- if websites %}
<div class="language-switcher">
    <span>{% tr "switchLanguage" %}:</span> {# 假设 "switchLanguage" 在您的翻译文件中 #}
    <ul>
        {%- for item in websites %}
        <li {% if item.IsCurrent %}class="active"{% endif %}>
            <a href="{{item.Link}}">
                {%- if item.LanguageIcon %}
                <img src="{{item.LanguageIcon}}" alt="{{item.LanguageName}}" class="language-icon" />
                {%- else %}
                {{item.LanguageEmoji}} {# 如果没有设置图标,显示emoji,例如 🇺🇸 🇨🇳 #}
                {% endif %}
                {{item.LanguageName}} {# 显示语言名称,例如 English, 中文 #}
            </a>
        </li>
        {%- endfor %}
    </ul>
</div>
{%- endif %}
{%- endlanguages %}

This code first uses{% languages websites %}The tag retrieves the list of all configured language sites and assigns it towebsitesVariable. Next, it will traversewebsitesarrayitemRepresenting a language site.

Inside the loop:

  • item.LinkThis is the complete URL generated by AnQiCMS for each language site, clicking it will directly jump to the homepage of the corresponding language site.
  • item.LanguageIcon: If your backend is configured with language icons, they will be displayed here.
  • item.LanguageEmoji: A convenient alternative, displaying the flag Emoji of the corresponding language.
  • item.LanguageNameDisplay the full name of the language, such as "English", "Chinese".
  • {% if item.IsCurrent %}class="active"{% endif %}This is a very practical conditional judgment.item.IsCurrentIt will judge whether the site being visited is this language site, and if it is, it will add it foractiveA class, which is convenient for you to highlight through CSS, allowing users to clearly know the current language environment.
  • {% tr "switchLanguage" %}Here, we usedtrTranslate label, ensure that the static text of the switch itself is also localized correctly.

Step 3: Optimize SEO: Add Hreflang tags

For multilingual websites,hreflangTags are an essential part of international SEO. They inform search engines about the language versions of your pages and their relationships, helping to avoid duplicate content issues and ensuring that search engines display the correct language version of the page to users.

You need tohreflangTags are placed in HTML's<head>block. Similarly, we can uselanguagestags to dynamically generate these links:

<head>
    {# 其他head内容,如meta标签、title等 #}

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

here,item.Languagewill output the language code of the language site (for exampleen-us/zh-cn)item.LinkOutput the URL of the corresponding language site. This way, search engines can accurately understand the structure of your multilingual content.

Practical Tips and Advanced Considerations

  • Customize the style:You can beautify your language switcher with CSS. For example, add a style to.language-switcher/ul/liand.activethe class to make it consistent with the overall design style of your website.
  • Highlight the current language:As shown above, by usingitem.IsCurrentattributes can easily highlight the current language.
  • Linking to dynamic content:In AnQiCMS, since each language site is relatively independent, when you create content within a language site and link to other content, these links are defaults to point to the internal pages of the current language site.For example, in English articles on the English site, the 'related reading' or 'category' links will default to pointing to the relevant content within the English site.This design greatly simplifies the link issues in multilingual content management.
  • Combined with navigation list tags:If your multilingual site navigation menu is also multilingual, you can combinenavListtags to configure different navigation menus on different language sites.navListTags also support.siteIdParameters can be flexibly called to navigate different sites in a template.

By following the above steps, you can dynamically use the AnQiCMS template.

Related articles

Does AnQiCMS navigation menu's `Title` field support HTML content rendering?

As an experienced website operations expert, I know that every detail in a content management system can affect the security, user experience, and even SEO effect of a website.AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, which has taken into account efficiency, customization, and security from the beginning of its design.Today

2025-11-07

How to add the link attribute `target="_blank"` in AnQiCMS navigation menu?

In today's age of increasingly important digital marketing and user experience, every detail of a website's navigation menu may affect the overall perception and staying time of users on the website.As experienced website operation experts, we know how to enhance the operation efficiency and user experience of a website through clever technological applications.

2025-11-07

How would the change in AnQiCMS's static page rules affect the generation of navigation links?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system, the configuration of its pseudo-static rules is an important part of website SEO and user experience optimization.Many website operators pay attention to a core issue when adjusting their sites: How will the change in AnQiCMS's static rules affect the generation of navigation links?** Indeed, navigation links are the main path for users to access website content and are also the key basis for search engines to crawl website structure.As an experienced website operations expert, I am happy to delve deeply into this mechanism.

2025-11-07

How is the data loading performance of the AnQiCMS navigation list tag, especially when there are many menu items?

## SecureCMS Navigation List: Can menu items be lightning fast even if there are many?An in-depth analysis of its data loading performance In modern website operations, the navigation menu is like the skeleton of a website, not only carrying the responsibility of guiding users and enhancing the experience, but also an indispensable part of search engine optimization (SEO).A responsive and smooth navigation system that allows users to easily find the content they need, and also helps search engine spiders to efficiently crawl, thereby bringing better visibility to the website.

2025-11-07

How to accurately highlight the parent menu of the current page in the AnQiCMS navigation menu using the `IsCurrent` property?

## Smart Use of `IsCurrent`: The Art of Smart Highlighting in AnQiCMS Navigation Menu Navigation menus play a crucial role in building any website.It is not only a guide for users to explore the content of the website, but also a direct manifestation of the website structure and user experience.A user-friendly and responsive navigation system that can significantly enhance user satisfaction and effectively guide them to the information they need.AnQiCMS (AnQiCMS) is an efficient and customizable enterprise-level content management system that is well-versed in this field

2025-11-07

How does the `config.` file in the AnQiCMS template directory structure affect the selection of navigation templates?

As an experienced website operation expert, I deeply understand the importance of an efficient and flexible content management system (CMS) for website operation.AnQiCMS (AnQiCMS) stands out among many CMS systems with its lightweight, efficient Go language and powerful customizability.Today, let's delve into a seemingly insignificant but actually crucial file in the Anqi CMS template directory structure, namely `config.`, and how it subtly influences the selection of the website navigation template.

2025-11-07

If the AnQiCMS website is in "shutdown" status, will the navigation menu still display normally?

As an experienced website operations expert, I know that in the life cycle of a website, there will always be situations where it is necessary to temporarily 'close the site'.Whether it is system upgrade, major content adjustment, or dealing with emergencies, shutting down is an important management function.For those familiar with AnQiCMS (AnQiCMS)Today, let's delve into the performance of AnQiCMS during the shutdown status

2025-11-07

How to create a hidden navigation link in AnQiCMS that does not display in the main navigation but is still used for SEO purposes?

As an experienced website operation expert, I know that skillfully utilizing SEO strategies is crucial in today's competitive online environment.AnQiCMS (AnQiCMS) is an efficient and flexible content management system that provides us with many tools for implementing advanced SEO operations.One of the very practical tips is to create hidden navigation links that are not displayed in the main navigation but still contribute effectively to SEO value.This strategy is not to deceive search engines, but to optimize user experience at the same time

2025-11-07