Skip to main content

Multilingual support for django-rest-framework

Project description

django-parler-rest

Adding translation support to django-rest-framework.

Tests PyPI PyPI version License Coverage

This package adds support for TranslatableModels from django-parler to django-rest-framework.

Installation

pip install django-parler-rest

Usage

  • First make sure you have django-parler_ installed and configured.
  • Use the serializers as demonstrated below to expose the translations.

First configure a model, following the django-parler documentation:

from django.db import models
from django.utils.translation import gettext_lazy as _

from parler.models import TranslatableModel, TranslatedFields


class Country(TranslatableModel):
    """
    Country database model.
    """

    country_code = models.CharField(_("Country code"), unique=True, db_index=True)

    translations = TranslatedFields(
        name = models.CharField(_("Name"), max_length=200)
        url = models.URLField(_("Webpage"), max_length=200, blank=True)
    )

    class Meta:
        verbose_name = _("Country")
        verbose_name_plural = _("Countries")

    def __str__(self):
        return self.name

The model translations can be exposed as a separate serializer:

from rest_framework import serializers
from parler_rest.serializers import TranslatableModelSerializer, TranslatedFieldsField
from .models import Country  # Example model


class CountrySerializer(TranslatableModelSerializer):
    translations = TranslatedFieldsField(shared_model=Country)

    class Meta:
        model = Country
        fields = ('id', 'country_code', 'translations')

Note: The TranslatedFieldsField can only be used in a serializer that inherits from TranslatableModelSerializer.

This will expose the fields as a separate dictionary in the JSON output:

{
    "id": 528,
    "country_code": "NL",
    "translations": {
        "nl": {
            "name": "Nederland",
            "url": "http://nl.wikipedia.org/wiki/Nederland"
        },
        "en": {
            "name": "Netherlands",
            "url": "http://en.wikipedia.org/wiki/Netherlands"
        },
        "de": {
            "name": "Niederlande",
            "url": "http://de.wikipedia.org/wiki/Niederlande"
        }
    }
}

Contributing

This module is designed to be generic. In case there is anything you didn't like about it, or think it's not flexible enough, please let us know. We'd love to improve it!

If you have any other valuable contribution, suggestion or idea, please let us know as well because we will look into it. Pull requests are welcome too. :-)

Running tests

Tests are run with py.test:

python setup.py test  # install dependencies and run tests with coverage

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

jst_parler_rest-1.0.0.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jst_parler_rest-1.0.0-py2.py3-none-any.whl (11.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file jst_parler_rest-1.0.0.tar.gz.

File metadata

  • Download URL: jst_parler_rest-1.0.0.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for jst_parler_rest-1.0.0.tar.gz
Algorithm Hash digest
SHA256 23716c53dc0ac4a102b396683b455bb44d0092024cea921698c4f4f57a3b0b6d
MD5 8ed67aedd80c47c73cd51ba7ea99021d
BLAKE2b-256 bd437a354274115ead2a3e783d90ae79918707ebfebb6c0dd232556bb4c26e18

See more details on using hashes here.

File details

Details for the file jst_parler_rest-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for jst_parler_rest-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d483521f42099204e7ac499f2c55ea47181b5a8eabc4be267e92793a38ff79db
MD5 af071943e32850d5385c12002332721c
BLAKE2b-256 dc1da4af3bf5cdd9638d17a25aa34842f8daa8c82825952ac6f9ab8620a26efa

See more details on using hashes here.

Supported by

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