I'm reviewing django-datatrans to use it in parallel with django-localeurl.
What I want to do is translate sl开发者_如何学Pythonugs so that my translated object is accessed as follows:
- www.mysite.com/fr/bonjour (in french)
- www.mysite.com/en/hello (in english)
For this I need the 'get' to depend on the 'current language'.
To clarify with an exemple :
If I do: object = MyObject.objects.get(slug=slug_from_url)
Then the 'get' should find the object either when:
- current_language is FR and slug_from_url == bonjour
- current_language is EN and slug_from_url == hello
I don't manage to get this behaviour and instead, the "get" only works with "slug_from_url" in the default language... whatever the 'current language' is during execution.
Maybe i'm approaching it the wrong way so any solution is welcome ! Thanks in advance
The easiest solution in this case is querying the KeyValue model
from datatrans.models import KeyValue
from datatrans.utils import get_current_language, get_default_language
digest = KeyValue.objects.get(value=slug_from_url, language=get_current_language()).digest
value = KeyValue.objects.get(digest=digest, language=get_default_language()).value
# value now contains 'hello'
myobject = MyObject.objects.get(slug=value)
This code essentially translates your string back to its original language so you can perform your lookup. I know that these are extra queries but if you have some caching in place this won't hurt your performance at all.
You can put this in some utility function for better reusability.
By the way, Datatrans is only designed for translating displayable content of a model, not lookup fields, as it would be a very drastic change to the Django QuerySet API.
Hope this helps...
精彩评论