上QQ阅读APP看书,第一时间看更新
How to do it...
First, let's create a filter to format a currency based on the current local language. Add the following code to my_app/__init__.py:
import ccy from flask import request @app.template_filter('format_currency') def format_currency_filter(amount): currency_code = ccy.countryccy(request.accept_languages.best[-
2:]) return '{0} {1}'.format(currency_code, amount)
request.accept_languages might not work in cases where a request does not have the ACCEPT-LANGUAGES header.
The preceding snippet will require the installation of a new package, ccy, as follows:
$ pip3 install ccy
The filter created in this example takes the language that best matches the current browser locale (which, in my case, is en-US), takes the last two characters from the locale string, and then generates the currency as per the ISO country code, which is represented by the two characters.
An interesting point to note in this recipe is that the Jinja2 filter can be created at the blueprint level as well as at the application level. If the filter is at the blueprint level, the decorator would be app_template_filter; otherwise, at the application level, the decorator would be template_filter.