In the daily operation of the website, we often need to display date and time information in a user-friendly manner.Especially when facing users from different regions, it is particularly important to output date and time formats that comply with local customs, which is what we often refer to as 'internationalized format output'.stringformatLook at whether it supports this customized internationalized format output in the handling of the date-time object.
Deeply UnderstandstringformatFilter
First, let's take a look back atstringformatThe core function of the filter. According to the document description of Anqi CMS,stringformatThe filter is mainly used to format any value such as numbers, strings, arrays, etc. into a string according to a specified format. Its usage is similar to that in the Go language.fmt.Sprintf()Functions that allow us to flexibly combine and format text for different types of data. For example, we can use it to control the number of decimal places for floating-point numbers, or embed numbers into a text block:
{{ 3.141592653|stringformat:"%.2f" }} {# 输出 3.14 #}
{{ 888|stringformat:"库存数量:%d" }} {# 输出 库存数量:888 #}
It is not difficult to find through these examples,stringformatThe filter performs excellently in general data formatting.However, when we talk about the Date-Time object, the situation is different.time.Timeformatting (type) is usually handled by special tags or filters, rather thanstringformat.stringformatThe design principle is more for string conversion of general data types, it itself does not contain the specific logic required for date and time formatting, such as parsing date components (year, month, day, hour, minute, second) and outputting them according to a specific layout string.
Auto CMS Date and Time Formatting Tool
In Auto CMS, handling date and time format output, we mainly rely on two powerful tools:stampToDateTags anddateFilter.
stampToDatetagsThis tag is specifically used for formatting 10-digit Unix timestamps.It requires a timestamp and a time format string that conforms to the Go language standard.CreatedTimeThe field stores the timestamp:{# 格式化为 年-月-日 #} {{ stampToDate(item.CreatedTime, "2006-01-02") }} {# 例如输出 2023-10-27 #} {# 格式化为 年-月-日 时:分:秒 #} {{ stampToDate(item.CreatedTime, "2006-01-02 15:04:05") }} {# 例如输出 2023-10-27 10:30:00 #}Here are the
"2006-01-02"It is not a literal date, but a specific layout (Layout) template defined in the Go language, representing the 'year-month-day' format.dateFilter: withstampToDateis similar,dateThe filter is also used for date and time formatting, but it expects atime.Timetype of object (not a timestamp). Its usage is similar tostampToDateAlmost identical, using the time layout string of Go language in English:{# 假设 someTimeVariable 是一个 time.Time 对象 #} {{ someTimeVariable|date:"Jan 02, 2006" }} {# 例如输出 Oct 27, 2023 #}
WhetherstampToDateOrdateFilters that all provide powerful custom format output capabilities, but this "custom" is based on the predefined layout string of the Go language, that is, you must explicitly specify what to represent "year" with, what to represent "month" with, and so on.
Consideration for internationalized format output
Now we come back to the core issue of "Internationalized Format Output". The Anqi CMS provides multilingual support on the content level, you can set the default language package of the website, even throughlanguagesLabel to get links to different language sites and throughtrTranslate the text in the template label. However, when it comes to date and time formatting,stampToDateanddateThe filter itself does not have a built-in function to automatically recognize the current user's language environment and output the corresponding date format.
This means, if you want to implement date format output for different language environments (for example, Chinese users see2023年10月27日, English users seeOctober 27, 2023, German users see27. Oktober 2023), it is not enough to rely onstampToDateordateIt is not possible to implement a generic format by directly passing a label. The layout string you pass"2006年01月02日"Always output the format with Chinese characters “year”, “month”, “day”, regardless of the current language environment the user is using.
Thoughts on implementing internationalized date formats.
To implement the internationalized format output of date and time, we usually need to combine the other functions of the Anqi CMS, and judge and process through template logic:
Get the current language environmentYou can specify a Tag's ID explicitly to retrieve its documents, or
{% system with name='Language' %}Label gets the language code of the current site (for examplezh-CN,en-US,deetc.).Conditional judgment applies different layouts: In the template, use the language code obtained,
{% if %}Select different Go language date layout strings to pass in,stampToDateordateLabel.{% set currentLang = system.Language %} {# 假设system.Language能获取到当前语言代码,具体字段需查阅系统标签 #} {% set myTimestamp = archive.CreatedTime %} {# 假设从archive获取时间戳 #} {% if currentLang == "zh-CN" %} {{ stampToDate(myTimestamp, "2006年01月02日") }} {% elif currentLang == "en-US" %} {{ stampToDate(myTimestamp, "January 02, 2006") }} {% elif currentLang == "de" %} {{ stampToDate(myTimestamp, "02. Januar 2006") }} {% else %} {# 默认或备用格式 #} {{ stampToDate(myTimestamp, "2006-01-02") }} {% endif %}
This method, although it requires some manual configuration, is an effective way to output date and time in an internationalized format under the current functional system of Secure CMS.
Summary
stringformatThe filter in the AnQi CMS is a generic string formatting tool, it does not directly support the formatting of date-time objects, nor does it have the functionality to automatically internationalize date formats. For handling date-time objects, we should use a specialstampToDateLabel (for timestamp) ordateFilter (fortime.Timeobject), and pass the Go language standard time format string.To implement the internationalization of date and time formats, you need to manually apply different date layout strings based on the language environment in the template.
Common Questions (FAQ)
1.stringformatfilters andstampToDateWhat are the core differences between labels?
stringformatFilter is a generic string formatting tool, suitable for various data types such as numbers, strings, arrays, etc., usingfmt.Sprintf()style-based format definitions.stampToDateThe label is specifically designed for Unix timestamps to convert them into date-time strings of a specific format. The format definition follows the Go language's time layout rules. In simple terms,stringformatDon't understand dates,stampToDateThen you are the expert in date handling.
2. Can the date format of the AnQi CMS be automatically switched according to the visitor's browser language?
The built-in date and time formatting tags of Anqi CMS (such asstampToDateanddateCurrently, it does not directly support adjusting the date format based on the visitor's browser language automatically.They depend on hard-coded Go language layout strings in the template.system), then manually select and apply the corresponding date format layout according to the language code.
3. Go Language's time formatting layout (Layout) string does not seem very intuitive. Is there a more convenient way to remember it?
The time layout of Go language is indeed quite special, it does not use the commonYYYY-MM-DDSuch placeholders, but uses a fixed reference time2006年01月02日 15时04分05秒 -0700 MSTDefine the layout. Although it may seem inconvenient at first, remember the meaning of each number and symbol in this reference time (for example2006represents the year,