GithubHelp home page GithubHelp logo

xupisco / django-translations Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bbmokhtari/django-translations

0.0 1.0 0.0 2.15 MB

Django model translation for perfectionists with deadlines.

Home Page: http://bbmokhtari.github.io/django-translations

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

django-translations's Introduction

Django Translations

build python pypi django flake8

Django model translation for perfectionists with deadlines.

Goal

There are two types of content, each of which has its own challenges for translation:

  • Static content: This is the content which is defined in the code. e.g. "Please enter a valid email address."

    Django already provides a solution for translating static content.

  • Dynamic content: This is the content which is stored in the database. (We can't know it beforehand!)

    Django Translations provides a solution for translating dynamic content.

Requirements

  • Python (>=3.5)
  • Django (>=2.0)

Installation

  1. Install Django Translations using pip:

    $ pip install django-translations
  2. Add translations to the INSTALLED_APPS in the settings of your project:

    INSTALLED_APPS += [
        'translations',
    ]
  3. Run migrate:

    $ python manage.py migrate
  4. Configure Django internationalization and localization settings:

    USE_I18N = True          # use internationalization
    USE_L10N = True          # use localization
    
    MIDDLEWARE += [          # locale middleware
        'django.middleware.locale.LocaleMiddleware',
    ]
    
    LANGUAGE_CODE = 'en-us'  # default (fallback) language
    LANGUAGES = (            # supported languages
        ('en', 'English'),
        ('en-gb', 'English (Great Britain)'),
        ('de', 'German'),
        ('tr', 'Turkish'),
    )

    Please note that these settings are for Django itself.

Basic Usage

Model

Inherit Translatable in any model you want translated:

from translations.models import Translatable

class Continent(Translatable):
    code = models.Charfield(...)
    name = models.Charfield(...)
    denonym = models.Charfield(...)

    class TranslatableMeta:
        fields = ['name', 'denonym']

No migrations needed afterwards.

Admin

Use the admin extensions:

from translations.admin import TranslatableAdmin, TranslationInline

class ContinentAdmin(TranslatableAdmin):
    inlines = [TranslationInline,]

This provides specialized translation inlines for the model.

image

QuerySet

Use the queryset extensions:

>>> from sample.models import Continent
>>> continents = Continent.objects.all(
... ).distinct(           # familiar distinct
... ).probe(['en', 'de']  # probe (filter, exclude, etc.) in English and German
... ).filter(             # familiar filtering
...     countries__cities__name__startswith='Köln'
... ).translate('de'      # translate the results in German
... ).translate_related(  # translate these relations as well
...     'countries', 'countries__cities',
... )
>>> print(continents)
<TranslatableQuerySet [
    <Continent: Europa>,
]>
>>> print(continents[0].countries.all())
<TranslatableQuerySet [
    <Country: Deutschland>,
]>
>>> print(continents[0].countries.all()[0].cities.all())
<TranslatableQuerySet [
    <City: Köln>,
]>

This provides a powerful yet familiar interface to work with the querysets.

Context

Use the translation context:

>>> from translations.context import Context
>>> from sample.models import Continent
>>> continents = Continent.objects.all()
>>> relations = ('countries', 'countries__cities',)
>>> with Context(continents, *relations) as context:
...     context.read('de')    # read the translations onto the context
...     print(':')            # use the objects like before
...     print(continents)
...     print(continents[0].countries.all())
...     print(continents[0].countries.all()[0].cities.all())
... 
...     continents[0].countries.all()[0].name = 'Change the name'
...     context.update('de')  # update the translations from the context
... 
...     context.delete('de')  # delete the translations of the context
... 
...     context.reset()       # reset the translations of the context
...     print(':')            # use the objects like before
...     print(continents)
...     print(continents[0].countries.all())
...     print(continents[0].countries.all()[0].cities.all())
:
<TranslatableQuerySet [
    <Continent: Asien>,
    <Continent: Europa>,
]>
<TranslatableQuerySet [
    <Country: Deutschland>,
]>
<TranslatableQuerySet [
    <City: Köln>,
]>
:
<TranslatableQuerySet [
    <Continent: Asia>,
    <Continent: Europe>,
]>
<TranslatableQuerySet [
    <Country: Germany>,
]>
<TranslatableQuerySet [
    <City: Cologne>,
]>

This can CRUD the translations of any objects (instance, queryset, list) and their relations.

Documentation

For more interesting capabilities browse through the documentation.

By The Way

If you liked this library please consider giving it a star! ⭐️

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.