A way to implement models with translatable content.
The translatable field of the default language has no extension, like "title" and the translations have extensions postfixes "_<two-letter language code>", like "title_la" or "title_mn". Method get_title() in this case returns the translation of the currently chosen language or the title of the default language, if the translation is not set.
The class XFieldList overrides the default list class and is used for modifying ordering and list_display settings according the chosen language. For example, when the German language is chosen, the list of translatable content objects will be ordered by German titles (not English).
*At the time when the list of field names is assigned to ordering or list_display (at the import of the model), the currently chosen language is still not known. But the language is known when ordering and list_display lists are used in contributed administration or elsewhere.*
The XFieldList returns the modified values of the passed-in list, when its methods/properties are triggered. XFieldList transforms field names ending "_" (except the ones beginning with "__", like "__str__") to appropriate translations according the currently chosen language. For example ['title_', 'content_', 'is_published'] will be changed to ['title', 'content', 'is_published'] for the English language and to ['title_lt', 'content_lt', 'is_published'] for the Lithuanian language.
*The best practice is to put XFieldList into a separate file and import it from different apps whenever needed.*
It's worth mentioning that one implementing this should also know about [Django internationalization](http://www.djangoproject.com/documentation/i18n/).
- i18n
- l10n
- translations
- multilingual
This class simplies the Feed class of django. The differences are:
1. Don't need define title and description template file
2. default feed generator class is Atom1Feed
3. According feed_url, EasyFeed can auto get the domain field, and you can also specify the domain parameter.(feed_url should be a full domain path, so you can use [Get the full request path](http://www.djangosnippets.org/snippets/41/) to get the full path of the feed url.)
4. There is a helper function render_feed() to return a response value.
example
---------
Feed class:
class BookCommentsFeed(EasyFeed):
def __init__(self, feed_url, book_id):
super(BookCommentsFeed, self).__init__(feed_url)
self.book_id = book_id
self.book = Book.objects.get(id=int(book_id))
def link(self):
return '/book/%s' % self.book_id
def items(self):
return self.book.comment_set.all().order_by('-createtime')[:15]
def title(self):
return 'Comments of: ' + self.book.title
def description(self):
return self.book.description
def item_link(self, item):
return '/book/%s/%s' % (self.book_id, item.chapter.num)
def item_description(self, item):
return item.content
def item_title(self, item):
return '#%d Comment for: ' % item.id + item.chapter.title
def item_pubdate(self, item):
return item.createtime
def item_author_name(self, item):
return item.username
And the view code is:
from feeds import *
from utils.easyfeed import render_feed
from utils.common import get_full_path
def bookcomments(request, book_id):
return render_feed(BookCommentsFeed(get_full_path(request), book_id))