mirror of
https://github.com/Hopiu/django-tos.git
synced 2026-03-16 20:10:24 +00:00
django-tos i18n support using django-modeltranslation
This commit is contained in:
parent
41eb35066d
commit
24f2e4a61f
7 changed files with 104 additions and 1 deletions
79
README.rst
79
README.rst
|
|
@ -24,4 +24,81 @@ Installation
|
|||
urlpatterns += patterns('',
|
||||
url(r'^login/$', 'tos.views.login', {}, 'auth_login',),
|
||||
url(r'^terms-of-service/', include('tos.urls')),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
===============
|
||||
django-tos-i18n
|
||||
===============
|
||||
|
||||
django-tos internationalization using django-modeltranslation.
|
||||
|
||||
Installation
|
||||
============
|
||||
|
||||
Assuming you have correctly installed django-tos in your app you only need to add following apps to ``INSTALLED_APPS``::
|
||||
|
||||
INSTALLED_APPS += ('modeltranslation', 'tos_i18n')
|
||||
|
||||
and also you should also define your languages in django ``LANG`` variable, eg.::
|
||||
|
||||
LANGUAGES = (
|
||||
('pl', 'Polski'),
|
||||
('en', 'English'),
|
||||
)
|
||||
|
||||
Please note that adding those to ``INSTALLED_APPS`` **changes** django models. Concretely it adds for every registered ``field`` that should translated, additional fields with name ``field_<lang_code>``, e.g. for given model::
|
||||
|
||||
class MyModel(models.Model):
|
||||
name = models.CharField(max_length=10)
|
||||
|
||||
There will be generated fields: ``name`` , ``name_en``, ``name_pl``.
|
||||
|
||||
You should probably migrate your database, using South is recommended. Migrations should be kept in your local project.
|
||||
|
||||
How to migrate tos with South
|
||||
`````````````````````````````
|
||||
|
||||
Here is some step-by-step example how to turn your legacy django-tos instalation synced using syncdb into translated tos with South migrations.
|
||||
|
||||
1. Inform South that you want to store migrations in custom place::
|
||||
|
||||
# Add to INSTALLED_APS
|
||||
SOUTH_MIGRATION_MODULES = {
|
||||
'tos': 'YOUR_APP.migrations.tos',
|
||||
}
|
||||
|
||||
2. Add required directory (django package)::
|
||||
|
||||
mkdir -p YOUR_APP/migrations/tos
|
||||
touch YOUR_APP/migrations/tos/__init__.py
|
||||
|
||||
3. Create initial migration (referring to the database state as it is now)::
|
||||
|
||||
python manage.py schemamigration --initial tos
|
||||
|
||||
4. Fake migration (because the changes are already in the database)::
|
||||
|
||||
python manage.py migrate tos --fake
|
||||
|
||||
5. Install tos_i18n to INSTALLED_APPS::
|
||||
|
||||
INSTALLED_APS += ('tos_i18n', )
|
||||
|
||||
6. Migrate what changed::
|
||||
|
||||
$ python manage.py schemamigration --auto tos
|
||||
$ python migrate tos
|
||||
|
||||
|
||||
That's it. You are now running tos in i18n mode with languages you declared in LANGUAGES setting.
|
||||
|
||||
This app will also make all required adjustments in django admin.
|
||||
|
||||
For more info on how translation works in details please refer to `django-modeltranslation docs<https://django-modeltranslation.readthedocs.org/en/latest/>`_.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
0
tos_i18n/__init__.py
Normal file
0
tos_i18n/__init__.py
Normal file
13
tos_i18n/admin.py
Normal file
13
tos_i18n/admin.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
from django.contrib import admin
|
||||
from modeltranslation.admin import TranslationAdmin
|
||||
from tos.admin import TermsOfServiceAdmin
|
||||
|
||||
# Admin translation for django-plans
|
||||
from tos.models import TermsOfService
|
||||
|
||||
|
||||
class TranslatedTermsOfServiceAdmin(TermsOfServiceAdmin, TranslationAdmin):
|
||||
pass
|
||||
|
||||
admin.site.unregister(TermsOfService)
|
||||
admin.site.register(TermsOfService, TranslatedTermsOfServiceAdmin)
|
||||
3
tos_i18n/models.py
Normal file
3
tos_i18n/models.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
0
tos_i18n/tests.py
Normal file
0
tos_i18n/tests.py
Normal file
9
tos_i18n/translation.py
Normal file
9
tos_i18n/translation.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
from modeltranslation.translator import translator, TranslationOptions
|
||||
from tos.models import TermsOfService
|
||||
|
||||
# Translations for django-tos
|
||||
|
||||
class TermsOfServiceTranslationOptions(TranslationOptions):
|
||||
fields = ('content', )
|
||||
|
||||
translator.register(TermsOfService, TermsOfServiceTranslationOptions)
|
||||
1
tos_i18n/views.py
Normal file
1
tos_i18n/views.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# Create your views here.
|
||||
Loading…
Reference in a new issue