Skip to main content

Internationalization for Python Liquid

Project description

Python Liquid Babel

Internationalization and localization for Liquid templates.

Project Status

This project is in its infancy. Documentation is currently limited to doc comments in source files. I'll update this readme with links to docs and PyPi as and when they become available.

Currency

Currency (aka money) formatting (source, tests)

Default options

Instances of the Currency class default to looking for a locale in a render context variable called locale, and a currency code in a render context variable called currency_code. It uses the locale's standard format and falls back to en_US and USD if those context variables don't exist.

from liquid import Environment
from liquid_babel.filters import Currency

env = Environment()
env.add_filter("currency", Currency())

template = env.from_string("{{ 100457.99 | currency }}")
print(template.render())
print(template.render(currency_code="GBP"))

Output

$100,457.99
£100,457.99

Money

For convenience, some "money" filters are defined that mimic Shopify's money filter behavior.

from liquid import Environment
from liquid_babel.filters import money
from liquid_babel.filters import money_with_currency
from liquid_babel.filters import money_without_currency
from liquid_babel.filters import money_without_trailing_zeros

env = Environment()
env.add_filter("money", money)
env.add_filter("money_with_currency", money_with_currency)
env.add_filter("money_without_currency", money_without_currency)
env.add_filter("money_without_trailing_zeros", money_without_trailing_zeros)

template = env.from_string("""\
{% assign amount = 10 %}
{{ amount | money }}
{{ amount | money_with_currency }}
{{ amount | money_without_currency }}
{{ amount | money_without_trailing_zeros }}
""")

print(template.render(currency_code="CAD", locale="en_CA"))

Output

$10.00
$10.00 CAD
10.00
$10

Number / Decimal

Decimal number formatting. (source, tests)

Instances of the Number class default to looking for a locale in a render context variable called locale. It uses the locale's standard format and falls back to en_US if those context variables don't exist.

from liquid import Environment
from liquid_babel.filters import Number

env = Environment()
env.add_filter("decimal", Number())

# parse a number from a string in the default (en_US) input locale.
template = env.from_string("{{ '10,000.23' | decimal }}")
print(template.render(locale="de"))
print(template.render(locale="en_GB"))

Output

10.000,23
10,000.23

Filters on the to-do list

  • Date and time formatting
  • List formatting
  • Pluralization
  • Inline translation

Tags on the to-do list

  • Translation block tag

Python Liquid Babel Change Log

Version 0.0.1

Features

  • New currency formatting filter.
  • New convenience "money" filters. money, money_with_currency, money_without_currency and money_without_trailing_zeros behave similarly to Shopify's equivalents.
  • New decimal formatting filter.

Fixes

  • Currency string parsing. Allow an input locale to be set independently from the output locale.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

liquid_babel-0.0.1.tar.gz (28.9 kB view hashes)

Uploaded Source

Built Distribution

liquid_babel-0.0.1-py3-none-any.whl (7.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page