GithubHelp home page GithubHelp logo

socialsign-in / django-model-i18n Goto Github PK

View Code? Open in Web Editor NEW

This project forked from juanpex/django-model-i18n

1.0 4.0 0.0 441 KB

Model translations with a pluggable twist

Home Page: http://juanpex.github.com/django-model-i18n/

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

HTML 11.29% Python 88.71%

django-model-i18n's Introduction

django-model-i18n

master:

https://travis-ci.org/juanpex/django-model-i18n.png?branch=master

dev:

https://travis-ci.org/juanpex/django-model-i18n.png?branch=dev

django-model-i18n is a django application that tries to make multilingual data in models less painful.

The main features/goals are:

  • Easy installation and integration. No data or schema migration pain.
  • Each multilingual model stores its translations in a separate table, which from django is just a new model dynamically created, we call this model the translation model.
  • You can add (or even drop) i18n support for a model at any time and you won't need to migrate any data or affect the original model (we call this the master model) table definition. This allows you to develop your apps without thinking about i18n (you even can load data for the main language and you won't need to migrate it) and when you are comfortable with it register the multilingual options and start working with the content translations.
  • 3rd party apps friendly. You can add i18n support to the existing models without modifying their definition at all (think in apps you can't modify directly for example djago.contrib.flatpages).

Installation

Clone the git repository:

git clone https://github.com/juanpex/django-model-i18n.git

or install with pip:

pip install django-model-i18n

Configuration

Add model_i18n admin loaders to your root project urls.py after admin.autodiscover():

from model_i18n import loaders

loaders.autodiscover_admin()

and add 'django.middleware.locale.LocaleMiddleware' into MIDDLEWARE_CLASSES:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    ## IF CACHE MIDDLEWARE IS SETTING PUT HERE
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)

and finally add model_i18n as the first item of INSTALLED_APPS:

INSTALLED_APPS = (
    'model_i18n',
    ...
    'django.contrib.admin',
    ...
)

Usage

  1. Create a translations.py in each app directory where you want to use model_i18n

  2. Register translations per-model:

    from model_i18n import translator
    from .models import Poll
    
    class PollTranslation(translator.ModelTranslation):
        fields = ('title',)
    
    translator.register(Poll, PollTranslation)
    
  3. Don't forget to update the database: a new schemamigration if you're using South, or syncdb if not.

Notes

If you want to translate models that are in django.contrib.*, e.g. flatpages, you can create a translations.py in your root project directory and register them like this:

from model_i18n import translator
from django.contrib.flatpages.models import FlatPage

class FlatPageTranslation(translator.ModelTranslation):
    fields = ('title', 'content')

translator.register(FlatPage, FlatPageTranslation)

and if you use south, you must also set the SOUTH_MIGRATION_MODULES setting to pick up this change:

SOUTH_MIGRATION_MODULES  = {
    'flatpages': 'migrations.flatpages'
}

django_i18n has good integration with django.contib.admin; it automatically configures ModelAdmin and inlines that apply.

API EXAMPLES

Filtering

Item.objects.set_language("es").filter(translations__title__contains='sometext')
items = Item.objects.filter(Q(translations___language='en') | Q(translations___language='es'))

items = items.exclude(category__name='stuff')
items = items.filter(Q(title__icontains='book') | Q(translations__title__icontains='toy'))

Updating

Item.objects.set_language("es").filter(translations__title__contains='sometext').update(title=u'new text')

Deleting

Item.objects.set_language("fr").filter(translations__title__contains='titres à éliminer').delete()
Bitdeli badge

django-model-i18n's People

Contributors

juanpex avatar gonz avatar jfunez avatar bitdeli-chef avatar supervacuo avatar marcelor avatar

Stargazers

Meng He avatar

Watchers

 avatar James Cloos avatar Mike Perrone avatar Meng He avatar

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.