All Projects → django-parler → django-parler-rest

django-parler / django-parler-rest

Licence: Apache-2.0 license
Translatable model support for django-rest-framework

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to django-parler-rest

Eslint Plugin I18n Json
Fully extendable eslint plugin for JSON i18n translation files.
Stars: ✭ 101 (+110.42%)
Mutual labels:  translations, internationalization
Django Parler
Easily translate "cheese omelet" into "omelette au fromage".
Stars: ✭ 459 (+856.25%)
Mutual labels:  translations, internationalization
Mojito
An automation platform that enables continuous localization.
Stars: ✭ 256 (+433.33%)
Mutual labels:  translations, internationalization
pH7-Internationalization
🎌 pH7CMS Internationalization (I18N) package 🙊 Get new languages for your pH7CMS website!
Stars: ✭ 17 (-64.58%)
Mutual labels:  translations, internationalization
Flutter translate
Flutter Translate is a fully featured localization / internationalization (i18n) library for Flutter.
Stars: ✭ 245 (+410.42%)
Mutual labels:  translations, internationalization
Django Translations
Django model translation for perfectionists with deadlines.
Stars: ✭ 109 (+127.08%)
Mutual labels:  translations, internationalization
I18n Editor
GUI for editing your i18n translation files
Stars: ✭ 290 (+504.17%)
Mutual labels:  translations, internationalization
Attranslate
Semi-automated Text Translator for Websites and Apps
Stars: ✭ 178 (+270.83%)
Mutual labels:  translations, internationalization
I18N-Portable
Simple and cross platform internationalization/translations for Xamarin and .NET
Stars: ✭ 102 (+112.5%)
Mutual labels:  translations, internationalization
mini i18n
🌐 Minimalistic I18n library for Ruby
Stars: ✭ 93 (+93.75%)
Mutual labels:  translations, internationalization
python-web-dev-21-2
Material for "Web Development in Python with Django" using Django 2.1, published as a Pearson LiveLesson on Safari Books Online
Stars: ✭ 38 (-20.83%)
Mutual labels:  django-rest-framework
precis i18n
Python3 implementation of PRECIS framework (RFC 8264, RFC 8265, RFC 8266)
Stars: ✭ 16 (-66.67%)
Mutual labels:  internationalization
laravel-localized-routes
A convenient way to set up and use localized routes in a Laravel app.
Stars: ✭ 257 (+435.42%)
Mutual labels:  translations
django-final
source code for django-final course
Stars: ✭ 28 (-41.67%)
Mutual labels:  django-rest-framework
django-rest-framework-datatables-editor
Seamless integration between Django REST framework, Datatables and Datatables Editor.
Stars: ✭ 25 (-47.92%)
Mutual labels:  django-rest-framework
dr scaffold
scaffold django rest apis like a champion 🚀
Stars: ✭ 116 (+141.67%)
Mutual labels:  django-rest-framework
django-i18nfield
Store internationalized strings in Django models with full forms support
Stars: ✭ 32 (-33.33%)
Mutual labels:  internationalization
ai web RISKOUT BTS
국방 리스크 관리 플랫폼 (🏅 국방부장관상/Minister of National Defense Award)
Stars: ✭ 18 (-62.5%)
Mutual labels:  django-rest-framework
drf ujson2
JSON parser and renderer using ujson for Django Rest Framework
Stars: ✭ 29 (-39.58%)
Mutual labels:  django-rest-framework
rest-framework-latex
A LaTeX renderer for Django REST Framework
Stars: ✭ 30 (-37.5%)
Mutual labels:  django-rest-framework

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].