Skip to main content

Django implementation for Fluent, the localization system for today's world.

Project description

https://badge.fury.io/py/django-ftl.svg https://readthedocs.org/projects/django-ftl/badge/?version=latest&style=flat https://github.com/django-ftl/django-ftl/workflows/Python%20package/badge.svg

django-ftl is a Django package for using for Fluent, a localization system for today’s world. (It would have been called django-fluent but that was already taken).

This package builds upon fluent-compiler and provides:

  • A structure for setting up and managing your .ftl files.

  • Methods for switching/setting the current language.

  • Integration into Django templates.

Why would I use this?

The defacto standard in Django world is GNU Gettext. See this Fluent vs gettext page for a comparison. In brief, here are some advantages:

  • Fluent makes concerns like plural rules the job of the translator.

  • Fluent gives translators the power to obey language specific rules (gender, case, plurals) that the developer may not be aware of, and shouldn’t have to build into the software.

  • Fluent integrates number and date formatting, and gives both developer and translators control over these, instead of these having to be handled separately, and only controlled by the developer.

To give an example, in GNU Gettext there is support for plural rules. However, this is the only language specific feature Gettext supports, and it is kind of bolted on afterwards. The developer also has to partially hard code the English rules (that is, the fact that there are two variants in English), as per the Django docs:

msg = ngettext(
     'there is %(count)d object.',
     'there are %(count)d objects.',
 count) % {
     'count': count,
 }

Finally, this still doesn’t work very well, because often you want to special case zero anyway - “there are no objects” (or “your inbox is empty” etc.) instead of “there are 0 objects”.

In Fluent, plural rules are one example of a more generic mechanism for selecting variants, and the translator is in control. The equivalent with fluent/django-ftl, with special handling of the zero case included, looks like this in an English .ftl file:

there-are-some-objects = { $count ->
    [0]     There are no objects.
    [1]     There is one object.
    [other] There are { $count } objects.
 }

The Python code referencing this will only need to use the ID (there-are-some-objects) and pass the $count argument.

Another problem that comes up is gender - for example, in French adjectives must agree in gender with the person being described. This can be solved in Fluent by passing the gender of the person as an argument, and allowing the translator to use the variant mechanism to write the correct language. This contrasts with GNU Gettext where the developer would have to create separate message strings for each case, because the message format is not powerful enough to allow the translator to add variant selection. Also, these different message strings will be identical in languages which don’t have that feature — in other words, the grammatical features of all languages end up either having a disproportionate effect on the source code and on other translators, or being neglected entirely.

Documentation

The documentation for how to use django-ftl is in the docs/folder and online at https://django-ftl.readthedocs.io.

Status

The library is now pretty stable, with a full test suite, no major missing features and no major breaking changes planned.

It has seen real-world usage in:

Credits

Tools used in rendering this package:

History

0.13 (2021-09-16)

  • Dropped support for Python 2.7

  • Added support for Django 3.2

  • Added support for custom functions to the Bundle constructor

  • Dropped useless mandatory configuration of mode parameter for template tags - it now defaults to 'server' which is the only allowed option anyway.

0.12.1 (2020-05-09)

  • Fixed broken (and undocumented) check_all method.

0.12 (2020-04-02)

  • Switch to the new APIs available in fluent_compiler 0.2.

  • Performance improvements - large reduction in the percentage overhead introduced by django-ftl (compared to raw fluent_compiler performance).

  • Undocumented MessageFinderBase class has changed slightly: its load method now returns a fluent_compiler.resource.FtlResource object instead of a string. If you used a custom finder for Bundle you may need to update it for this change.

0.11 (2020-03-24)

  • Switched to using fluent_compiler as backend instead of experimental branch in fluent.runtime. This means import changes are required:

    • fluent_number and fluent_date, if you are using them, should be imported from fluent_compiler.types instead of fluent.runtime.types

  • Added Bundle.check_all method.

  • Django 3.0 support

  • Dropped support for Python 3.4 (it may work, but recent versions of lxml do not install on it, which made running tests harder).

0.10 (2019-05-23)

  • Upgraded to more recent version of fluent.runtime (0.1 with modifications)

  • Fixed use_isolating behavior (BDI characters are now inserted for HTML messages)

  • Thread-safety fixes for loading bundles.

  • Corrected order of using ‘locales’ directories found via INSTALLED_APPS to be consistent with normal Django convention.

0.9.1 (2019-03-02)

  • Changed development autoreload mechanism to not interfere with Django’s development server autoreload.

  • Bug fix for case when invalid mode is specified in template tag.

  • Various fixes and improvements to middlewares (plus tests)

  • Thread-safe Bundle

  • Method for configuring ftlmsg via context processor.

0.9 (2018-09-10)

  • Working version

  • Depends on our version of python-fluent

0.0.1 (2018-05-19)

  • First release on PyPI - empty placeholder package

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

django-ftl-0.13.tar.gz (47.9 kB view details)

Uploaded Source

Built Distribution

django_ftl-0.13-py2.py3-none-any.whl (13.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-ftl-0.13.tar.gz.

File metadata

  • Download URL: django-ftl-0.13.tar.gz
  • Upload date:
  • Size: 47.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for django-ftl-0.13.tar.gz
Algorithm Hash digest
SHA256 15b079038e7f09cc65bcb617451b8803e07bb8b125927df0cf6c9c01488266c8
MD5 ea4b5d98dd54b54f7338a841e07e1a71
BLAKE2b-256 1ac30ea14322908fb2f128a7afe0cff558cd73f6f0e335fd57a23b5b8ec233ca

See more details on using hashes here.

Provenance

File details

Details for the file django_ftl-0.13-py2.py3-none-any.whl.

File metadata

  • Download URL: django_ftl-0.13-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for django_ftl-0.13-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cfdb4fbd422807f4f6cbccd9606e7585ddcd290f7b627ac78bd66c506337bb86
MD5 a26be91752f3ef9e7a00eedca2e605ff
BLAKE2b-256 4a8980c3530490ba8ed406ae5278f82921c0ff5ac6cf9019b072f3bf921bf188

See more details on using hashes here.

Provenance

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