• Stars
    star
    533
  • Rank 83,238 (Top 2 %)
  • Language
    Python
  • License
    Other
  • Created over 13 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Painless translations in django, using the regular ORM. Integrates easily into existing projects and apps. Easy convertible from django-multilingual-ng.

django-hvad package coverage build

Model translations made easy.

This project adds support for model translations in Django. It is designed to be unobtrusive, efficient and reliable. On the technical side, it uses an automatically created Translations Model to store translatable fields in arbitrary languages with a foreign key to the main model, enabling fast queries.

Started in 2011, hvad has grown mature and is now used on large scale applications.

Quick links:

Features

  • Simple - only 3 new queryset methods.
  • Natural - use Django ORM as usual, it just became language aware.
  • Fast - no additional queries for reads, just an inner join to an indexed key.
  • Complete - relationships, custom managers and querysets, proxy models, and abstract models.
  • Batteries included - translation-enabled forms and admin are provided.
  • Reliable - more than 300 test cases and counting. coverage build
  • Compatible with Django 1.8 to 1.11, running Python 2.7, 3.4, 3.5 or 3.6.

Django-hvad also features support for Django REST framework 3.1 or newer, including translation-aware serializers.

Example Uses

Declaring a translatable Book model:

class Book(TranslatableModel):
    author = models.ForeignKey(Author)
    release = models.Date()

    translations = TranslatedFields(
        title = models.CharField(max_length=250)
    )

Thus, only the title will vary based on the language. Release date and author are shared among all languages. Let's now create a Book instance:

# The recommended way:
book = Book.objects.language('en').create(
    author = Author.objects.get(name='Antoine de Saint Exupéry'),
    release = datetime.date(1943, 4, 6),
    title = "The Little Prince",
)

# Also works
book = Book(language_code='en')
book.author = Author.objects.get(name='Antoine de Saint Exupéry')
book.release = datetime.date(1943, 4, 6)
book.title = "The Little Prince"
book.save()

Providing some translations:

book.translate('fr')
book.title = "Le Petit Prince"
book.save()
book.translate('de')
book.title = "Der kleine Prinz"
book.save()

Every call to translate() creates a new translation from scratch and switches to that translation; save() only saves the latest translation. Let's now perform some language-aware queries:

Book.objects.all()

Compatible by default: returns all objects, without any translated fields attached. Starting from v1.0, default behavior can be overriden to work like next query:

Book.objects.language().all()

Returns all objects as translated instances, but only the ones that are translated into the currect language. You can also specify which language to get, using e.g.:

Book.objects.language("en").all()

Usual queryset methods work like they always did: let's get all books as translated instances, filtering on the title attribute, returning those that have Petit Prince in their French title, ordered by publication date (in their French edition):

Book.objects.language("fr").filter(title__contains='Petit Prince').order_by('release')

Other random examples:

# last German book published in year 1948
Book.objects.language("de").filter(release__year=1948).latest()

# other books from the same author as mybook. Cache author as well.
Book.objects.language().select_related('author').filter(author__books=mybook)

# books that have "Django" in their title, regardless of the language
Book.objects.language('all').filter(title__icontains='Django')

More examples in the quickstart guide.

Releases

Django-hvad uses the same release pattern as Django. The following versions are thus available:

  • Stable branch 1.7, available through PyPI and git branch releases/1.7.x.
  • Stable branch 1.8, available through PyPI and git branch releases/1.8.x.
  • Development branch 1.9, available through git branch master.

Stable branches have minor bugfix releases as needed, with guaranteed compatibility. See the installation guide for details, or have a look at the release notes.

Thanks to

Jonas Obrist (https://github.com/ojii) for making django-nani and for helping me with this project.

Kristian Øllegaard (https://github.com/KristianOellegaard/) for django-hvad and trusting me to continue the development.

More Repositories

1

python-eureka

Python client for Netflix Eureka https://github.com/Netflix/eureka/
Python
74
star
2

django-saml-service-provider

Easily let users sign in via SAML 2.0 to your django app. Based on python-saml
Python
33
star
3

zsh-plugins

Shell
17
star
4

socket.io-redis

Using redis pubsub to push messages via socket.io to a client
JavaScript
16
star
5

django-multiple-email-backends

Want to send through more than one interface? E.g. SMTP and database? This is your solution
16
star
6

dokku-controller

Python
16
star
7

cmsplugin-text-ng

Python
14
star
8

python-cloudfoundry

Cloud Foundry (or derivative) python library/client. Should also work with Stackato and AppFog.
Python
9
star
9

python-economic-rest

Easy to use and dynamic python interface to the experimental e-conomic REST api
Python
8
star
10

catalog

django powered webshop or online "catalog" - i, however, recommend django-shop (https://github.com/divio/django-shop/)
Python
6
star
11

mcollective-docs

The docs I could not find for Marionette Collective
Shell
4
star
12

django-shop-simple

django-shop doesn't come with design and templates - this is an example on how it can be implemented
Python
4
star
13

django-rick

a plugin for your personal website or link redirector service
Python
4
star
14

docker-service-provisioner

A generic service provisioner, that uses Dockerfiles. Compatible with the heroku provider API.
Python
3
star
15

dokku-controller-agent

Python
3
star
16

puppet-hetzner

hetzner-specific server information
Ruby
3
star
17

plasmatrim-python

Python
2
star
18

python-harvest

Python
2
star
19

puppet-nginx-lb

Puppet manifest for a simple nginx load balancer and SSL terminator
Puppet
2
star
20

snapsekassen

An advanced Schnapps account keeper, written in Django.
Python
2
star
21

cfd

Temporary project for the yet-to-be-named config management system
Python
2
star
22

puppet-dokku

dokku inspired puppet scripts to bootstrap your own PaaS. Currently lacks a cloud controller and appserver worker.
Puppet
2
star
23

django-hostinfo-project

displays a small site with information about the host, you can use it for testing e.g. a loadbalancer setup
Python
2
star
24

homeassistant-stokercloud

Python
1
star
25

stokercloud-client

Python
1
star
26

python-bank-import

a library of bank import formats
Python
1
star
27

fly-lufthansa

a website about my experience with lufthansa
Python
1
star
28

au-skema

Aarhus University student week schedule
Python
1
star
29

matematik-a-noter

College-level Math-A notes in Danish using markdown/latex
Shell
1
star
30

treeio-project

deployment related stuff for tree io and virtualenv
1
star
31

nats-python

Python
1
star
32

packer-storm

Build Apache (Incubator) Storm with packer. Made for AWS - could work with any packer backend.
Shell
1
star
33

harvest-for-mac

An objective c menu bar for Harvest Timer Tracker. (and my personal first touch with objective c)
1
star
34

puppet-puppetagent

A module for managing your puppet agents
Puppet
1
star
35

vagrant-base-boxes

1
star
36

vmbuilder

mirror of https://code.launchpad.net/vmbuilder
1
star
37

facter-country

a fact to check what country the server is in
Ruby
1
star
38

packer-images

1
star
39

puppet-github

Automatically open a github issue, if puppet run fails
Ruby
1
star