Hey, i am currently working on a django app for my studies, and came to the point of l18n. Localizing the site itself was very easy, but now i have to allow users, to translate the dynamic content of the application.
Users can save "products" in the database and give them names and descriptions, but since the whole site should be localized, i must provide a way of translating theses names and descriptions to the users.
Is there a natural way in django to do this? Or do i have to realize it as part of the application (by representing the translations in the datamodel)
Thanks, Janosch
I would suggest checking out django-multilingual. It is a third party app that lets you define translation fields on your models.
Of course, you still have to type in the actual translations, but they are stored transparently in the database (as opposed to in static PO files), which is what I believe you are asking about.
There are two projects of note for translatable content in Django:
http://code.google.com/p/django-multilingual/
http://code.google.com/p/transdb/
I use django-multilingual for localize content and django-localeurl for choosing language based on url (for example mypage/en/).
You can see how multilingua and localeurl work on JewishKrakow.net page.
"i must provide a way of translating theses names and descriptions to the users."
"Is there a natural way in django to do this?"
Are you asking if Django can translate from language to language? Are you asking about something like http://translate.google.com/ ?
I don't think Django can translate user input into another language.
If you are going to do the translation for your users, this must be part of your data model.
Django's i18n filter allows you to have a table of translation strings. The documentation says this.
Embed translation strings in your Python code and templates.
Get translations for those strings, in whichever languages you want to support. This is something you do manually, by hiring translators or knowing a lot of languages yourself.
Activate the locale middleware in your Django settings.
I have 2 languages on my site: English and Arabic
Users can switch between languages clicking on a flag.
In models i use a proxy model:
class Product(models.Model):
name=models.CharField(max_length=100)
name_ar=models.CharField(max_length=100, default='')
def __unicode__(self):
return self.name
class Product_ar(Product):
def __unicode__(self):
return self.name_ar
class Meta:
proxy=True
In forms I use 2 forms instead of one:
class CollectionEditForm_en(forms.Form):
name = forms.CharField(label=_('Name'), max_length=100, widget=forms.TextInput(attrs={'size':'50'}))
product = forms.ModelChoiceField(label=_('product'), queryset=Product.objects.filter(enabled=True), empty_label=None)
class CollectionEditForm_ar(forms.Form):
name = forms.CharField(label=_('Name'), max_length=100, widget=forms.TextInput(attrs={'size':'50'}))
product = forms.ModelChoiceField(label=_('product'), queryset=Product_ar.objects.filter(enabled=True), empty_label=None)
In code check language this way:
if request.LANGUAGE_CODE=='ar':
CollectionEditForm=CollectionEditForm_ar
else:
CollectionEditForm=CollectionEditForm_en
So in templates i check:
{% if LANGUAGE_CODE == "ar" %}
{{product.name_ar}}
{% else %}
{{product.name}}
{% endif %}
Hope this solution will help somebody
It depends on who will provide the translations. If you want to provide a web interface to translation, then you need to develop that yourself, and also represent the translations in the database.
If the same translators who translated the site will also translate the data, you can provide them with the same model that they use for the site (presumably gettext), and you can then also use gettext for this content.
Also looking for content localization plugin, or how to write it.
Can add to the list django-i18n-model
I think you should operate in two steps:
Get translations
Show translated strings
For the first step, you should tell Django that the user-inserted strings are to be translated. I think there is no native way to do so. Maybe you can extract the strings from your db putting them in locale-specific files, run 'makemessages' on them, obtaint django.po files and translate.
Second, use ugettext to show those strings on your web application.
Hope this can help the ones with your same problem.
Or try this:
http://packages.python.org/django-easymode/i18n/index.html
It stays very close to how you would normally do a django model, you just add 1 decorator above your model. It has admin support for the translated fields, including inlines and generic inlines. Almost anything you can do with regular models and admin classes you can do with the internationalized versions.
Related
I am studying Python and Django, my final goal is to create an administration panel that manages 2 things:
"dynamic article / item" (which can create types of forms to enter).
"dynamic taxonomies / tags" (which can create categories or groupers of the data "of the form type").
I have seen the documentation of DjangoCMS but I cannot find the names of these concepts, could you guide me?
thank you
You can do pretty much whatever you want to do. If you want to add to what a page gives you, extend the page;
http://docs.django-cms.org/en/latest/how_to/extending_page_title.html
I've used this to add images to pages, so you could use it to add tags or anything else you'd like.
Here's an example of attributing a colour to pages;
class PageColourExtension(PageExtension):
"""
Extend the page model with a background image field.
"""
page_colour = models.CharField(
_("Base colour"),
max_length=50,
null=True,
choices=COLOUR_CHOICES,
)
def __unicode__(self):
"""
Identify by the page it is tied to.
#return: page name
#rtype: str
"""
return '%s' % self.extended_object
extension_pool.register(PageColourExtension)
I'm not sure if djanog-cms is the right tool for your task. It's main purpose is to manage hierarchical pages, and the content on these pages. You need to keep in mind that this concept is already there, and cannot be changed, only extended.
For your specific case, I would recommend to create an own Article model, and then use the PlaceholderField, on your article model - it allows really dynamic content to be created, in the same fashion as when used on cms pages. See django-cms placeholder outside the cms docs for how to do that.
If you want to use django-cms and your custom application/article app alongside, you can use an app-hook to embed your urls under a cms page. See docs djanog-cms apphooks
So I have Auth and Profile implemented into my system however i'd like to extend the User model and I don't know what is considered the 'correct' way of doing this.
I just want to create a method that returns a link for the user like:
Username
I figured that this is probably best done using a method as the link will change and I don't want to be going through all my template files fixing this.
This may be common to somebody using Django but I have not used it much so I am not well versed in the conventions so any advice would be great
If you're not altering what is stored in the database, the easiest way is a proxy model. Here's an example straight out of the documentation (https://docs.djangoproject.com/en/dev/topics/db/models/#proxy-models)
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
class MyPerson(Person):
class Meta:
proxy = True
def do_something(self):
# ...
pass
If you want to do something more complex than adding methods to the User model, such as adding new data fields, I would recommend creating a Model with a one-to-one relation to User.
The normal way to create URLs in Django without needing to worry about changing them is to use the {% url %} tag. See the documentation on reversing URLs.
I know you are asking specifically for extending the user model but have you considered creating a template tag to generate the link instead?
If you only need the method in the templates than that's definitely the way to go.
I am new to django and I have I think a pretty fundamental question.
Lets say I have this theme:
I made a project already, so I know a bit about know how to build models for dynamic content, pass them to views and admin panel etc, but:
Question: on the image above I marked 3 containers that include text. There is only one instance of this text on the whole website, and it's not repeatable. If I developed for myself I would just hard-code that, but what if I develop for a client, who needs to be able to edit those fields using the admin panel?
Am I supposed to create a separate class containing multiple (lets say 20) fields for these kind of containers for the whole website, pass that class in a view (and filter with [:1]) to use it in a template?
Thats the only thing I came up with. Although it would work I think it's a terrible solution.
What I would do is write a model that contains a TextField for the blurb to insert and a CharField to identify it, and a custom template tag that reads the blurb from the database by the argument you pass to it.
class Blurb(models.Model):
ident = models.CharField(..., db_index=True)
blurb = models.TextField(...)
PK ident text
1 main Hey! Do you like this template? This...
{% load blurb %}
...
{% blurb main %}
you could have 1 model with a selection field containing a descriptor for the text in the model.
Something like:
class SomeText(models.Model):
position = models.CharField(max_length=120, choices=POSITION_DESCRIPTORS)
text = models.TextField()
A designer recently handed me designs for a site I'm building for a client. They're great designs but I'm really scratching my head as to how I'm going to implement it on the site.
The content can very easily be broken down into blocks or chunks of data where I could allocate a textarea for text input, a couple of charfields for link-buttons, etc and sequentially render them out to the page.
The problem (eg why I'm not just pulling in Django-CMS et al) is the blocks are quite unique from each other. There are perhaps 20 different models that I would build for each block type. Rather than hack around a pre-fab CMS, I'd like to build a Page model and then just have an M2M that links to an ordered list of subclasses of my abstract Block class. I hope I'm not losing you.
I don't understand how I can do this. These questions spring to mind:
Is there a simple CMS that does all of this already? Am I wasting my time trying to figure out the physics?
My Blocks subclasses will technically be different type. Do I need generics for a M2M-through class to link to them? Is so, how do I do that?
How do I render x different forms in an inline admin context? (I'd like to have the page form with a list of the Blocks underneath)
How can the user specify the type of Block in the inline control?
Edit: Or, alternatively, what about a templatetag-based CMS?
I'm thinking of something like plonking this in my template:
{% editable 'unique_id' 'content-type' %}
A further example:
{% editable 'home-intro' 'text' %}
Then I could just stick these around the templates I want to be editable, in the way I want them to be editable and when logged in the client would see "Edit text", "Edit link", "Edit image" links which simply popped up the right field.
This would make things more locked down but the layout needs to remain solid (and the client knows nothing about HTML/CSS) so it's one or other of these methods IMO.
Couldn't you implement your 'Blocks' as Django CMS Plugins? Then each page is just constructed from a number of plugins.
Each plugin has an admin form which gets the specifics for itself, and then the page template renders the plugin as you want it.
If you look at the first page of the django-cms demo:
https://www.django-cms.org/en/tour-demo/
you'll see in (1) a highlighted plugin block - in this case a formatted text block that is edited with TinyMCE or similar. You can define your own plugins and add them to pages.
last month I published an article (for review) on how tho build a basic CMS for Jinja. This's templating language does not dffer very much from Django, which I have been using before.
You can find it here. It uses template inheritance to fill the content blocks.
https://codereview.stackexchange.com/questions/5965/review-request-jinja-cms-for-energiekantoor-nl-on-google-app-engine
Or type in Google : Jinja CMS
I am developing a new project from scratch with Django. I see that there are many apps for handling translation of dynamic content.
Django-multilingual
Django-pluggable-model-i18n
Django-modeltranslation
Transdb
Django-multilingual-model-
Django-transmeta
to name few.
Transdb, transmeta and multilingual sounded fair, but I want to read some personal experiences.
Which one of them should I choose?
I agree with S.Lott in that you will want to think about what you need from internationalization. However, this is a very good run-down of all of the mentioned packages:
http://blog.muhuk.com/2010/01/06/dynamic-translation-apps-for-django.htm
My personal pick thus far is "none of the above". I am currently prototyping with datatrans:
http://github.com/citylive/django-datatrans
http://www.botondus.com/dynamic-translation-with-django-datatrans/
But I may yet choose another solution.
There are 2 kinds of model translation:
Adding extra columns/fields to the model to translate
Using a second table for translations
The 1st approach is more efficient because you don't use extra joins. And the 2nd one is less intrusive because it doesn't modify the original model table. I personally like the 1st option better and that's one of the main reasons why I use django-modeltranslation.
You can also have a look to this comparison grid.
Django-nani is a brand-new application, which is fast, and well-written using probably the best design approach.
It's still in development but works well and has a very complete documentation.
This is a snippet to see how the multilingual models are defined:
from django.db import models
from nani.models import TranslatableModel, TranslatedFields
class DjangoApplication(TranslatableModel):
name = models.CharField(max_length=255, unique=True)
author = models.CharField(max_length=255)
translations = TranslatedFields(
description = models.TextField(),
description_author = models.CharField(max_length=255),
)
def __unicode__(self):
return self.name
I'm using the django i18n for a bilingual project: I'm really really satisfied of this one and I definitely would recommend it, but I've got to say that I've never tried one of the others...