July 2019
Beginner to intermediate
302 pages
9h 38m
English
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)
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 ...