forked from gonz/django-model-i18n
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
74 lines (70 loc) · 4.34 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<h1 id="django-model-i18n">django-model-i18n</h1>
<p>django-model-i18n is a django application that tries to make multilingual data in models less painful.</p>
<p>The main features/goals are:</p>
<ul>
<li>Easy installation and integration. No data or schema migration pain.</li>
<li>Each multilingual model stores it's translations in a separate table, which from django is just a new model dynamically created, we call this model the translation model.</li>
<li>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 in the i18n part (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.</li>
<li>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).</li>
</ul>
<h1 id="installation">Installation</h1>
<ul>
<li>cloning repository</li>
</ul>
<h1 id="configuration">Configuration</h1>
<p>Go to urls.py into root project directory and put this</p>
<pre><code>from model_i18n import loaders
loaders.autodiscover_admin()</code></pre>
<p>also add 'django.middleware.locale.LocaleMiddleware' into MIDDLEWARE_CLASSES::</p>
<pre><code>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',
)</code></pre>
<p>and finally put in INSTALLED_APPS::</p>
<pre><code>INSTALLED_APPS = (
'model_i18n',
...
'django.contrib.admin',
...
)</code></pre>
<p><strong>When putting model_i18n must be first in the list of INSTALLED_APPS.</strong></p>
<h1 id="usage">Usage</h1>
<ol type="1">
<li>In the directory of the application create a translations.py</li>
<li><p>Inside the file you need to register translations like this example::</p>
<p>from model_i18n import translator from app.models import Item</p>
<p>class ItemTranslation(translator.ModelTranslation): fields = ('title',)</p>
<p>translator.register(Item, ItemTranslation)</p></li>
<li><p>Don't forget to run a schema migration if necessary.</p></li>
</ol>
<h1 id="notes">Notes</h1>
<p>If you will translate models that are into django.contrib.* such as flatpages you can create in your root project directory a file named translations.py and handle other third parties apps.models like this::</p>
<pre><code>from model_i18n import translator
from django.contrib.flatpages.models import FlatPage
class FlatPageTranslation(translator.ModelTranslation):
fields = ('title', 'content')
translator.register(FlatPage, FlatPageTranslation)</code></pre>
<p>and if you need use south you must use SOUTH_MIGRATION_MODULES setting like this::</p>
<pre><code>SOUTH_MIGRATION_MODULES = {
'flatpages': 'migrations.flatpages'
}</code></pre>
<p>It has good integration with django.contib.admin. It automatically configures ModelAdmin and inlines that apply.</p>
<h1 id="api-examples">API EXAMPLES</h1>
<h2 id="filtering">Filtering</h2>
<p>Code::</p>
<pre><code>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'))</code></pre>
<h2 id="updating">Updating</h2>
<p>Code::</p>
<p>Item.objects.set_language("es").filter(translations__title__contains='sometext').update(title=u'new text')</p>
<h2 id="deleting">Deleting</h2>
<p>Code::</p>
<pre><code>Item.objects.set_language("fr").filter(translations__title__contains='titres à éliminer').delete()</code></pre>