How to display multi-language switch options for the current site?

Calendar 👁️ 74

How to conveniently add multilingual switch options to the website in Anqi CMS?

As the trend of globalization continues to deepen, many businesses and content operators find that having a website that supports multilingual is crucial for expanding the market and serving users in different regions.AnQi CMS knows this need, therefore it built-in powerful multilingual support function, allowing you to easily manage and display content in different languages.

After your website content is prepared in multiple languages, the next step naturally is to provide visitors with an intuitive language switcher.This not only improves the user experience, but also ensures that your content can reach a wider audience.This article will guide you on how to elegantly implement the multi-language switch option display in the Anqi CMS template.

Understand the multilingual site mechanism of AnQi CMS

Before delving into the template code, let's briefly review how AnQi CMS handles multilingualism.

The AnQi CMS supports multi-site management, which means you can create and manage an independent site for each language.For example, a main site is used for Chinese content, and another sub-site is used for English content. They can be managed统一 in the background, but have independent domain names or sub-domains in the front.

In the background "Global Feature Settings", you can set the "default language package" for each site.This setting mainly affects the language display of default text such as system built-in prompts, background interface, etc.However, it will not automatically translate the specific content of your website such as articles, products, categories, etc.This content usually requires you to manually fill in different language versions when creating or editing.

Based on the foundation of multi-site configuration, Anqi CMS provides a simple template tag that allows you to conveniently display all configured language versions on the website front end and provide switch links.

How to display language switch options in front-end templates?

In the AnQi CMS template system, to display language switch options, you need to uselanguagesTemplate tag. This tag can retrieve information about all the multi-language sites configured under the current Anqi CMS instance.

The basic calling method is as follows:

{%- languages websites %}
    {%- if websites %}
    <div>
        <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 %}

Let's break down this code:

  1. {%- languages websites %}This is the core tag for calling the multilingual site. It retrieves all configured language sites and assigns them as an array object towebsitesVariable.
  2. {%- if websites %}This is a conditional judgment to ensure that the switch option will be rendered only when there are multiple language sites, avoiding the display of an empty switcher on single-language websites.
  3. {%- for item in websites %}:websitesis an array containing multiple site information, we traverse this array throughforloop through the arrayitemthe variable represents the information of a language site in each loop
  4. itemthe fields available in the variable:
    • item.LanguageNameThe full name of the current language, for example, 'Chinese', 'English'.
    • item.LanguageThe current language code, such as 'zh-CN', 'en-US', is very useful for SEO inhreflangtags.
    • item.LanguageEmoji: The Emoji expression corresponding to the current language, for example 🌐.
    • item.LanguageIcon: If you have set a custom icon for the language in the background, the URL of the icon will be displayed here.
    • item.Name: The name of the site.
    • item.LinkThe most important field, this is the access link to the current language site, click to directly switch to the website of the language version.
    • item.Remarkanditem.NofollowOther auxiliary information, usually not displayed directly in the language switcher.
  5. Display logic:
    • {%- if item.LanguageIcon %}We first determine if there is a custom language icon. If there is, it will display the icon (<img src="{{item.LanguageIcon}}" />).
    • {%- else %}If there is no custom icon, the language's Emoji expression will be displayed ({{item.LanguageEmoji}}).
    • Finally, whether there is an icon/Emoji or not, the language name will be displayed ({{item.LanguageName}}Please provide a clear text prompt.

By this code, you can flexibly combine the display of language names, icons, or emojis, providing your website visitors with a beautiful and practical language switch entry.

Optimize SEO: Add hreflang tag

In addition to displaying the language switcher on the page, it is recommended to guide search engines to understand your multilingual content by using the website's<head>Partially addhreflangTags. This helps avoid the repetition of multilingual content and ensures that the correct language version is displayed to users in the corresponding regions.

You can find the public header template file of the website (such asbash.htmlorheader.htmlUse it inlanguagestag generationhreflangInformation:

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

This code will generate a tag for each language version<link>indicating its correspondinghref(link address) andhreflang(Language code),help search engines better index your multilingual sites.

Actual operation:integrate the switcher into your template.

To add a multilingual switcher to your website, you can choose from several common template locations:

  • Website Header (Header): Usually locatedtemplate/default/partial/header.htmlIn a similar public header file. This is the most common location, allowing users to quickly find the toggle option upon entering the website.
  • Website Footer (Footer)Intemplate/default/partial/footer.htmlIn other files, providing a secondary toggle entry.
  • Sidebar (Sidebar): If your website has a sidebar, you can also place the toggle here.
  • Public Base TemplateFor exampletemplate/default/bash.htmlIf all your pages inherit from a basic template, placing it here ensures that all pages display the switcher.

You can customize the CSS style of the above code according to your website design, for example, making it into a dropdown menu, flag icon list, and other forms so that it can better integrate into the overall interface. Anqi CMS providesLanguageIconandLanguageEmojiField, it provides great flexibility for visual design.

Summary

Provided by Anqi CMSlanguagesTemplate label, you can conveniently and quickly implement the display of multilingual switching options on the website front end.Whether it is a simple text link or a visual switcher combined with icons and Emoji, it can be easily realized.At the same time, do not forget to addhreflangTags, provide good search engine optimization support for your multilingual site. Make good use of these features, and your website will be able to go international and serve global users better.


Frequently Asked Questions (FAQ)

1. Why did the front-end content not automatically translate even though the "default language package" was set in the background?

The 'default language package' setting of AnQi CMS (located in the background 'Global Function Settings') is mainly for localizing the built-in interface text, prompts, and other information.It does not automatically translate the specific content you create in the background, such as articles, products, categories, etc.For this content, you need to manually create and fill in the corresponding content for each language version.For example, you can post an article in Chinese on the Chinese site, and then publish the corresponding English translation on the English site.

2. How to configure independent domains for different language versions of websites? For examplewww.example.comUsed for Chinese,en.example.comFor English?

AnQi CMS supports multi-site management. You can create multiple sites in the "Multi-site Management" backend, each of which can be bound to an independent domain (or subdomain) and configured with its corresponding language, template, and content.When the user accesses different domain names, the corresponding language version of the website will be loaded.Use in front-end templatelanguagesWhen a label is selected, it will automatically retrieve and generate the correct link for each site (item.Link) to enable language switching between different domain names.

3. Can I use custom flag icons or brand icons for language switching options?

Yes, you can. Inlanguagesthe tags returned byitemthe object, it includesLanguageIconfield. This means you can upload a custom icon for each language in the background (such as a flag image), and then use it in the template.{{item.LanguageIcon}}Display these icons. If no icons are uploaded, you can choose to use{{item.LanguageEmoji}}Display Emoji icons, or just display{{item.LanguageName}}Text name. This flexibility allows you to freely choose the most suitable icon display form according to the design style of the website.

Related articles

How to design a basic template and allow other pages to inherit it?

Building websites in AnQi CMS, efficient and flexible template design is the key to improving development efficiency and maintaining website consistency.Among them, designing a basic template and allowing other pages to inherit it is the core strategy to achieve this goal.AnQi CMS adopts a syntax similar to the Django template engine, making template inheritance intuitive and powerful. ### Lay the Foundation: Create Your Basic Template First, let's create the basic template skeleton of a website. In Anqi CMS, all template files are stored in the `/template` directory

2025-11-09

How to create reusable template code snippets (macros)?

In the daily content operation of Anqi CMS, we often encounter some interface elements or code structures that need to be used repeatedly.For example, a standardized article list item, a product display card with a unified style, or a footer contact information with specific layout information.If writing this code manually every time, it is not only inefficient but also prone to inconsistent formatting issues.This is where the "Macro" feature provided by Anqi CMS becomes particularly important, as it helps us easily create reusable template code snippets, greatly enhancing the efficiency and quality of template development.###

2025-11-09

How to include a common header, footer, or sidebar template file?

When building a website on Anqi CMS, we often encounter the need: the header (Header), footer (Footer), or sidebar (Sidebar) appear repeatedly on multiple pages.If you copy code every time, not only is it inefficient, but maintenance and updates will also become extremely complex. 幸运的是,AnQi CMS is developed based on Go language, its template engine supports syntax similar to Django, providing a very flexible and powerful way to introduce these public template files, helping us to achieve efficient content management and maintenance.

2025-11-09

How to define and use custom variables in a template?

In website operation, we often need to display diverse content, which may include system preset data, as well as customized personalized information based on business needs.AnQiCMS (AnQiCMS) fully understands this need, providing flexible and diverse mechanisms, allowing you to easily define and use custom variables in templates, thus achieving precise control and personalized display of content. This article will introduce you to the various methods of defining and using custom variables in AnQiCMS templates, helping you to fully utilize these features to create websites with more expressiveness and practicality.###

2025-11-09

How to add hreflang tags in templates to support multi-language SEO?

With the increasing trend of globalization, many websites are facing the challenge of providing content to users of different languages or regions.To ensure that your multilingual website performs well in search engines and accurately guides users to their preferred language version, adding the `hreflang` tag is crucial.AnQiCMS (AnQiCMS) is an enterprise-level content management system that deeply supports multilingualism and provides a convenient way to implement this feature in templates.### Understand the role of the hreflang tag `hreflang`

2025-11-09

How to display the list of dynamic Banner images configured in the website?

The website's banner area, like a dynamic promotional window, its visual appeal and content delivery efficiency directly affect the first impression of visitors to the website.A well-designed Banner that can quickly capture the user's attention and effectively showcase the brand image, latest activities, or core products.In AnQiCMS (AnQi CMS), by utilizing its powerful content management features, you can easily configure and display a dynamic banner image list, keeping your website fresh and attractive.###

2025-11-09

How to display or customize the error pages (such as 404, 500) and shutdown notifications of a website?

In the operation of the website, visitors may encounter pages that cannot be found for various reasons or temporary issues with the server, and may even be unable to access the website during maintenance and upgrades.In these situations, a well-designed error page (such as 404, 500) and a friendly shutdown prompt can not only greatly improve user experience but also maintain the professional image of the website to a certain extent and search engine optimization (SEO).AnQi CMS is an efficient content management system that provides a flexible way to display and customize these important pages.###

2025-11-09

How to use AnQiCMS to customize the display layout of the article detail page?

AnQiCMS provides a very flexible and powerful content management system, allowing us to easily customize the display layout of article detail pages.In order to enhance user experience, optimize SEO, or better showcase the brand characteristics, AnQiCMS can help us achieve these goals through its intuitive template system and rich tag features.How can we proceed specifically? We can delve into these aspects step by step.### 1. Understanding AnQiCMS template mechanism First

2025-11-09