In the operation of a website, especially when facing global users, the localization experience of content is crucial.Among them, dynamically adjusting the output format of the calculation results, such as displaying different currency formats based on the user's country or region, is a common requirement.AnQiCMS (AnQiCMS) is a flexible content management system that provides a powerful template engine and a rich set of tags/filters, allowing us to elegantly implement this feature.
To achieve this goal, we mainly rely on the core capabilities of the Anqi CMS template engine:Get the current site or user's language/region information/Conditional judgmentandNumber formatting.
Get the current site or user's language/region information
AnQi CMS excels in multilingual support, which provides a foundation for us to dynamically adjust content.The system automatically identifies the language settings of the current visited site or the language selected by the user through the language switcher.systemTranslate the content of value to [en]: Get the language code of the current site using the label.
For example, to get the language setting of the current site, you can do this:
{% set current_lang = "" %}
{% system current_lang with name="Language" %}
{# 此时,current_lang 变量将包含当前站点的语言代码,例如 "en-US", "zh-CN", "ja-JP" 等 #}
Here, we first define an empty variablecurrent_lang, and then usesystemLabel assigns the current site's language code to it. This language code will be the key basis for our subsequent judgments.
The core idea of dynamically adjusting the output format of the calculation results
With the current language/region information, the next step is to apply this information to the output of the calculation results (such as product prices). The core idea is:
- Obtain the original value:For example, get the product price from the document details.
- Determine the current language/region:Use
ifLogical judgment tag, based oncurrent_langthe value for branching. - Apply formatting:Formatting currency symbols and number formats in combination with language/region, outputting a currency format that conforms to local customs.
Practice: Currency Formatting of Product Prices
Assuming we have a product details page, where we need to display the price of the product. The price of the product is stored inarchiveObjects'Pricefield in.
1. Display the basic price
The simplest way to display is to directly output the price, but usually without currency symbols and proper formatting:
产品价格:{{ archive.Price }}
2. Format dynamically based on language/region
Now, we combine the obtained language information to add dynamic currency formatting to the product price. We will usefloatformata filter to control the number of decimal places and manually add the currency symbol.
{% set current_lang = "" %}
{% system current_lang with name="Language" %}
<div>
产品价格:
{% if current_lang == "en-US" %}
$ {{ archive.Price|floatformat:2 }} {# 美国,显示美元符号,保留两位小数 #}
{% elif current_lang == "fr-FR" or current_lang == "de-DE" %}
{{ archive.Price|floatformat:2 }} € {# 欧洲,显示欧元符号,保留两位小数,符号在数字后 #}
{% elif current_lang == "ja-JP" %}
¥ {{ archive.Price|floatformat:0 }} {# 日本,显示日元符号,不保留小数 #}
{% elif current_lang == "zh-CN" %}
¥ {{ archive.Price|floatformat:2 }} {# 中国大陆,显示人民币符号,保留两位小数 #}
{% else %}
$ {{ archive.Price|floatformat:2 }} {# 默认格式,例如国际通用美元格式 #}
{% endif %}
</div>
In the above code:
- We first retrieve the language code of the current site and store it in
current_langthe variable. - Then, use
if...elif...elseStructure, based oncurrent_langto make the judgment. - For different languages, we adjust the position, type, and decimal place display of currency symbols. For example, Japan usually does not display decimal places, while the European and American regions are accustomed to retaining two decimal places.
floatformat:2The value indicates formatting a floating-point number to two decimal places.floatformat:0It means not retaining any decimal places.- We have also set up a
elsebranch, as the default display format for all languages that are not explicitly specified.
3. Wrap the formatting logic into auxiliary macro (Macro)
If a website has many places where currency formatting is needed, we can encapsulate the above logic into a macro (macro) to improve code reusability and maintainability.
Firstly, define a macro in a separate template file (for example),partial/currency_macros.htmlthen
{# partial/currency_macros.html #}
{% macro format_price(price_value, lang_code) %}
{% if lang_code == "en-US" %}
$ {{ price_value|floatformat:2 }}
{% elif lang_code == "fr-FR" or lang_code == "de-DE" %}
{{ price_value|floatformat:2 }} €
{% elif lang_code == "ja-JP" %}
¥ {{ price_value|floatformat:0 }}
{% elif lang_code == "zh-CN" %}
¥ {{ price_value|floatformat:2 }}
{% else %}
$ {{ price_value|floatformat:2 }} {# 默认格式 #}
{% endif %}
{% endmacro %}
import and call this macro where needed:
{% import "partial/currency_macros.html" as currency_helpers %}
{% set current_lang = "" %}
{% system current_lang with name="Language" %}
<div>
产品价格:{{ currency_helpers.format_price(archive.Price, current_lang) }}
</div>
So that when we need to display the price elsewhere, we just need to callcurrency_helpers.format_pricethe macro, the code will