In today's global digital world, website operators all know the importance of content localization.Especially for multilingual websites, it is not only the translation of text content, but also elements such as dates and times that seem trivial need to be localized according to the cultural habits of the target users.AnQiCMS (AnQiCMS) is a content management system tailored for small and medium-sized enterprises and content operation teams, deeply understanding this field, and providing powerful date format localization capabilities through its flexible template engine.stampToDateHow to cleverly implement the localization of date format display in labels.
stampToDateThe bridge between timestamps and date formatting:
In the template system of Anqi CMS,stampToDateis a powerful and commonly used tag, which is responsible for converting Unix timestamps (usually 10-digit integers) into the date and time format that we are familiar with in our daily lives. Whether you are displaying the publish time of an article (CreatedTime)、Update Time(UpdatedTimeOr any other date information stored in the form of a timestampstampToDateAre your reliable assistants.
Its basic usage is very intuitive:{{stampToDate(时间戳, "格式")}}. The 'timestamp' here usually comes from the records stored in the database, such as the document object.item.CreatedTimeAnd the 'format' is the key to localizing the display.
The template engine of AnQi CMS is based on Go language, thereforestampToDateThe format string accepted by the label also follows the unique time formatting rules of the Go language. Go does not use commonY-m-d H:i:sThis PHP or Python style format symbol, instead uses a fixed "reference time"-2006-01-02 15:04:05.999999999 -0700 MSTUse as a template. You just need to use the corresponding number or letter combination in the format string according to your expected output pattern. For example:
- You would write to display the format "Year-Month-Day"
"2006-01-02". - If you need "Month/Day/Year", then write
"01/02/2006". - It would be to display the complete format "Year-Month-Day Time:Minute:Second"
"2006-01-02 15:04:05".
The strength of this mechanism lies in its flexibility: you do not need to memorize complex format codes, just refer to a real date and time example, and you can construct any date format you want.
Implementing date localization in a multilingual environment
One of AnQi CMS' advantages is its native multilingual support, allowing content operators to provide customized experiences for users in different regions. To makestampToDateThe tag implements the localization display of date formats, we need to combine the current language setting of the website with the formatting rules of the Go language.
Anqi CMS passed{% system with name='Language' %}This tag can easily retrieve the language code of the current site. For example, for a Chinese site, it may returnzh-cn; for an English site, it might been-us. With this language code, we can use conditional logic in templates to provide different date format strings for different language environments.
Imagine you have a list of articles that need to display the publication date.For Chinese users, you may want to see 'October 26, 2023', while English users are more accustomed to 'October 26, 2023'.
First, at the beginning of the template or in the public header file (such asbash.htmlIn the middle, obtain the current site's language settings and define a set of date format rules based on the language. To keep the template neat, we can use{% set %}Label to define a variable that stores the current language's date format:
{% system currentLang with name='Language' %}
{% set dateFormat = "2006-01-02" %} {# 默认格式,例如中文 #}
{% if currentLang == "en-us" %}
{% set dateFormat = "Jan 02, 2006" %} {# 英文格式 #}
{% elif currentLang == "ja" %}
{% set dateFormat = "2006年01月02日" %} {# 日文格式 #}
{% elif currentLang == "de-de" %}
{% set dateFormat = "02. Jan 2006" %} {# 德文格式 #}
{% endif %}
OncedateFormatA variable is defined, and you can use it anywhere in the template to format timestamps. For example, display the creation time of an article in an article list:
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<li>
<a href="{{item.Link}}">
<h5>{{item.Title}}</h5>
<div>
<span>发布日期:{{stampToDate(item.CreatedTime, dateFormat)}}</span>
<span>浏览量:{{item.Views}}</span>
</div>
</a>
</li>
{% empty %}
<li>暂无文章内容。</li>
{% endfor %}
{% endarchiveList %}
In this way, when users visit your Chinese site, they will see “Published date: October 26, 2023”; while switching to the English site, it will automatically adapt to “Published on: Oct 26, 2023”.This flexible conditional judgment mechanism makes the localization of date and time display both controllable and efficient.
Insight into Go language time formatting is exquisite
Understanding the underlying principles helps us better utilize it.stampToDateThe internal logic of the tag is actually very simple: it first converts the incoming Unix timestamp to Go language'stime.Timeobject. Then, it calls the Go standard librarytimepackageFormat()methods, and pass in the format string you define. Go language'stime.Format()The method is an extremely intelligent function that can accurately parse and construct the target date string based on the 'reference time' template you provide. For example, when you write in the format string,Janwhen, Go will automatically output the English abbreviation based on the context (i.e.,time.Timethe month represented by the object), write;JanuaryThen output the full name. Although Go originally supports Chinese, Japanese, and other languages for month and date namesFormatThe method itself does not directly handle multilingual names (it always outputs English names, unless you provide a custom format with Chinese month names), but through the strategy of "selecting different format strings based on language codes", localization display can be completely achieved.This means that the core work of localized date display is actually completed through strategic format string selection at the template level.
Conclusion
Anqi CMS providesstampToDateThis powerful and flexible template tag, combined with its complete multilingual support and template conditional judgment capabilities, makes it easy to implement localized date formatting in multilingual websites.The operator only needs to define the corresponding Go language formatting string according to the habits of the target language, thus providing seamless and considerate browsing experience for global users, further enhancing the professionalism and customer satisfaction of the website.
Frequently Asked Questions (FAQ)
Q1:stampToDateThe format string in the label, where can I find all the formatting options supported by the Go language?
A1: The time formatting in Go language is based on a fixed reference time (Mon Jan 2 15:04:05 MST 2006defined by the.You do not need to memorize a complex set of format symbols, just replace the corresponding part of this reference time with the output mode you expect.2006;