In today's globalized digital world, whether a website can overcome language barriers and directly reach users from different cultural backgrounds often determines its market breadth and influence.AnQi CMS deeply understands this, providing users with powerful and flexible multilingual support features.As an experienced website operations expert, I am well aware of the importance of an efficient multilingual management system for enhancing user experience and expanding global business.In AnQiCMS,.ymlThe file is the core tool to achieve this goal.

This article will take you to a deeper understanding of the AnQiCMS multilingual translation in.ymlwriting and management strategies of files, helping you to transform technical details into practical operational wisdom.

The core of AnQiCMS multilingual system:localesDirectory and.ymlFile

AnQiCMS multilingual support is not just a simple text replacement, but a well-structured, easy-to-maintain system. Its core lies in the template root directory.localesThe folder and what it contains.ymlLanguage package file.

Imagine that your website needs to support Chinese (Simplified), English, even Japanese. AnQiCMS will suggest that you create a folder namedlocalesThe folder. In this folder, you need to create a separate subdirectory for each target language, such as:

  • zh-cn(Representing Simplified Chinese)
  • en-us(Representing American English)
  • ja(Representing Japanese)

These language codes follow international standards, ensuring system identification and compatibility.

In each language subdirectory, there will usually be one or more.ymlfiles, the most common and recommended naming isdefault.yml.YAML (Yet Another Markup Language) files are suitable for storing static text content in various languages due to their concise and intuitive key-value pair structure.

Let's look at a simplified example to understanddefault.ymlThe structure is:

# 文件路径: ./locales/zh-cn/default.yml
welcomeMessage: "欢迎来到我们的网站!"
yourLocation: "您的位置"
readMore: "阅读更多"
contactUs: "联系我们"
submitButton: "提交"

# 文件路径: ./locales/en-us/default.yml
welcomeMessage: "Welcome to our website!"
yourLocation: "Your location"
readMore: "Read More"
contactUs: "Contact Us"
submitButton: "Submit"

In this structure,welcomeMessage/yourLocationetc. are unique key names (or translation IDs), which are in all languagesdefault.ymlThe content in the file should be consistent. The text after the colon is the translation of the corresponding language.This design completely separates the interface text from the code logic of the website, greatly improving the maintenance efficiency of multilingual websites.

to refer to the translation content in the template:{% tr %}The clever use of tags

When your multilingual dictionary (i.e..ymlfile) is ready, how can you elegantly apply it in the website template? AnQiCMS provides a simple and powerful{% tr %}Translation label.

In your AnQiCMS template file (usually.htmlsuffix), you do not need to write text in various languages directly. Instead, you just need to use{% tr %}tags and pass in the.ymlThe key name defined in the file. For example:

<nav>
    <a href="/">{% tr "yourLocation" %}</a>
    <a href="/contact">{% tr "contactUs" %}</a>
</nav>

<h1>{% tr "welcomeMessage" %}</h1>
<button>{% tr "submitButton" %}</button>

When a user visits the website, AnQiCMS will automatically load the corresponding language package file based on the current site settings (or the language selected by the user). For example, if the user visits a Chinese site, the system will loadzh-cn/default.ymlthen{% tr "welcomeMessage" %}It will display "Welcome to our website!" and if the user switches to the English site, the system will loaden-us/default.ymlThe same label will display as 'Welcome to our website!'.

This dynamic loading mechanism allows you to write a single template that can easily support multiple language displays, greatly reducing the redundancy and maintenance costs of the template.

Careful management and efficient practice

To fully utilize the advantages of AnQiCMS multilingual translation, the following management and **practical strategies are crucial:

  1. Key names should be unified and semantically rich: Ensure that in all languages,.ymlThe key used in the file should be the same. The key name should be concise and clear, expressing the meaning of the text it represents (for example,pageTitleHomeinstead ofpt1This not only facilitates team collaboration, but also makes future maintenance and expansion easier.

  2. Distinguish between static text and dynamic contentThis is a common misconception in multilingual operation.

    • .ymlFile: Mainly used for website managementStatic, interface textFor example, navigation menu items, button text, footer statements, form prompts, system prompt information (such as "Operation successful", "Please fill in required items") and so on.This text is usually a fixed part of the website interface.
    • Background content management system: For article content, product descriptions, category names, single page content, etc.Dynamic, business content,AnQiCMS usually creates and manages content for different language versions through the back-end content management interface. This means you don't have to write every word of every article in.ymlFiles, instead, take advantage of the powerful content model and multi-site features of CMS to achieve content localization. This is also one of the core advantages of AnQiCMS multi-language support.
  3. Reasonable organization.ymlFileWhen the translation text of the website is large, placing all key-value pairsdefault.ymlin it may seem bulky. You can create multiple.ymlfiles to organize the translation content, for example:

    • messages.yml(System prompt, welcome message)
    • forms.yml(Form field, button text)
    • navigation.yml(Navigation menu)
    • errors.yml(Error message) This modular management approach makes it more efficient to search and update translation content. When referencing in the template, it is only necessary to add the filename before the key name, for example{% tr "navigation.home" %}.
  4. Make good use of the language switcher withhreflangTag:

    • Language switcher: AnQiCMS provides{% languages websites %}The label helps you build an intuitive language switcher on the front end. Users can easily select their preferred language version by clicking the switch button or the dropdown menu.
    • hreflangTagFor search engine optimization (SEO), it is correct to usehreflangLabels are crucial. They can tell search engines that your website has multiple language versions and guide them to show the most relevant pages to users in different language regions. You can use{% languages websites %}Label generation in a loophreflangLink it and place it in the HTML's<head>Block, for example:
      
      {%- languages websites %}
      {%- for item in websites %}
      <link rel="alternate" href="{{item.Link}}" hreflang="{{item.Language}}">
      {%- endfor %}
      {%- endlanguages %}
      
      This will help your multi