angvp/django-klingon

View on GitHub
docs/usage.rst

Summary

Maintainability
Test Coverage
========
Usage
========

Using Specific Widgets in the TranslationInline form of the admin:
________________________________________________

You can specify the widget to be use on an inline form by passing a dictionary to TranslationInlineForm.
So, you might want to extend the TranslationInline with a new form that will a "widgets" dictionary, 
where you can specify the widget that each filds has to use, for example::

    class RichTranslationInlineForm(TranslationInlineForm):
        widgets = {
            'CharField': forms.TextInput(attrs={'class': 'klingon-char-field'}),
            'TextField': forms.Textarea(attrs={'class': 'klingon-text-field'}),
        }

    class RichTranslationInline(TranslationInline):
        form = RichTranslationInlineForm

and then you just simply use the RichTranslationInline class on your AdminModels, for example::

    class BookAdmin(admin.ModelAdmin):
        inlines = [RichTranslationInline]

* see full example in example_project folder of source code of klingon

Using the API
------------------------------------

To create the translation you can do the follwing:

Suppose that you have and object called book::

    > book = Book.objects.create(
        title="The Raven",
        description="The Raven is a narrative poem",
        publication_date=datetime.date(1845, 1, 1)
    )

you can create translation for that instances like this::

    > book.set_translation('es', 'title', 'El Cuervo')
    > book.set_translation('es', 'description', 'El Cuervo es un poema narrativo')

a translation could be access individually::

    > self.book.get_translation('es', 'title')
    'El Cuervo'
    > book.get_translation('es', 'description')
    'El Cuervo es un poema narrativo'

or you can get all translations together::

    > self.book.translations('es')
    {
        'title': self.es_title,
        'description': self.es_description,
    }