In website operation, especially when facing global users, the localization experience of content is crucial.Among them, dynamically adjusting the output format of the calculated 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 rich tags / filters, allowing us to beautifully implement this function.

To achieve this goal, we mainly rely on several 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

The AnQi CMS excels in multilingual support, which provides the foundation for us to dynamically adjust content.The system will automatically identify the language settings of the current visited site or the language selected by the user through the language switcher.In the template, we can go throughsystemLabel to get the current site's language code.

For example, to get the current site's language settings, you can do it like 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_langThen usesystemLabel it as the language code of the current site. This language code will be the key basis for our subsequent judgment.

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 result (such as product prices). The core idea is:

  1. Get the original value:For example, get the product price from the document details.
  2. Determine the current language/region:UseifLogic judgment label, according tocurrent_langto branch based on the value.
  3. Apply formatting:For different languages/regions, combine currency symbols and number formatting filters to output a currency format that conforms to local customs.

Practice: Formatting product prices in currency format

Assuming we have a product detail page where we need to display the product price. The product price is stored inarchivethe object'sPricein the field.

1. Basic price display

The simplest way to display the price is to output it directly, but usually without the currency symbol and proper format:

产品价格:{{ archive.Price }}

2. Format dynamically according to language/region

Now, we combine the language information we have obtained to add dynamic currency formatting to product prices. 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 obtained the language code of the current site and stored it incurrent_langthe variable.
  • Next, useif...elif...elsestructure based oncurrent_langand judged its value.
  • For different languages, we adjust the position, type, and display of currency symbols. For example, Japan usually does not display decimal places, while the Western world tends to keep two decimal places.
  • floatformat:2Represents formatting a floating-point number to two decimal places.floatformat:0Means not to retain decimals.
  • We have also set aelsebranch as the default display format for all languages that are not explicitly specified.

3. Wrap the formatting logic into a helper macro (Macro)

If a website needs to use currency formatting in many places, we can encapsulate the above logic into a macro (macro) to improve code reusability and maintainability.

First, define a macro in a separate template file (for examplepartial/currency_macros.htmlThen, import and call this macro where needed:

{# 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 %}

Then, in the place where needed, import and call this macro:

{% 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>

This, when we need to display the price elsewhere, just callcurrency_helpers.format_pricemacro, the code will