Re: Translation inside the database
Am 08.02.2012 21:35, schrieb akaariai: On Feb 8, 9:33 pm, Thorsten Sanders wrote: Hello, I have tables having translation like this: name_en,name_de,name_fr... With google I found 2 solutions which support that, but they dont allow to use those fields then with order,filter, values etc...so its kinda useless. With some trying arround, I came up with the following: from django.db import models from django.utils.translation import get_language class TransCharField(models.CharField): def __getattribute__(self,value): if value == 'column': return '%s_%s' % (super(TransCharField, self).__getattribute__(value),get_language()) else: return super(TransCharField, self).__getattribute__(value) Inside model then for example: name = TransCharField(max_length=255) I did some testing and it seems to work proper as I want it, I can just use the orm normal and using name and inside the query the field get translated to the right name. My question is if that solution can have any side effects or if there is a better way to do that, I will btw only do read operations on those tables, there will never be write/updates to it via django. syncdb etc woud break for sure, but it seems that will not be a problem for you. Serialization will give you only one language. I can't think of anything else that will break for sure. On the other hand, it wouldn't be surprising if the list of things that do not work would be a lot longer. If it works, use it. But it will not work 100%. Yep I know syncdb breaks and that is not a big problem for me, serialization if used should only give 1 language as well and prolly it will be used and exactly the "it wouldn't be surprising if the list of things that do not work would be a lot longer" is why I am asking, I had it before that something works nice 99% of the time but the other 1% it dont, in a upcoming project I plan to use that approach for a lot of queries and would be quite bad if some side effects will appear, because it would be prolly a lot of work to change it. A safer way would be to do something like: SomeModel.objects.filter(t('some_column')=someval).order_by(t('some_column')) def t(col): return '%s_%s', (col, get_language()) - Anssi -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Translation inside the database
On Feb 8, 9:33 pm, Thorsten Sanders wrote: > Hello, > > I have tables having translation like this: > > name_en,name_de,name_fr... > > With google I found 2 solutions which support that, but they dont allow > to use those fields then with order,filter, values etc...so its kinda > useless. > > With some trying arround, I came up with the following: > > from django.db import models > from django.utils.translation import get_language > > class TransCharField(models.CharField): > > def __getattribute__(self,value): > if value == 'column': > return '%s_%s' % (super(TransCharField, > self).__getattribute__(value),get_language()) > else: > return super(TransCharField, self).__getattribute__(value) > > Inside model then for example: > > name = TransCharField(max_length=255) > > I did some testing and it seems to work proper as I want it, I can just > use the orm normal and using name and inside the query the field get > translated to the right name. > > My question is if that solution can have any side effects or if there is > a better way to do that, I will btw only do read operations on those > tables, there will never be write/updates to it via django. syncdb etc woud break for sure, but it seems that will not be a problem for you. Serialization will give you only one language. I can't think of anything else that will break for sure. On the other hand, it wouldn't be surprising if the list of things that do not work would be a lot longer. If it works, use it. But it will not work 100%. A safer way would be to do something like: SomeModel.objects.filter(t('some_column')=someval).order_by(t('some_column')) def t(col): return '%s_%s', (col, get_language()) - Anssi -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Translation inside the database
Hello, I have tables having translation like this: name_en,name_de,name_fr... With google I found 2 solutions which support that, but they dont allow to use those fields then with order,filter, values etc...so its kinda useless. With some trying arround, I came up with the following: from django.db import models from django.utils.translation import get_language class TransCharField(models.CharField): def __getattribute__(self,value): if value == 'column': return '%s_%s' % (super(TransCharField, self).__getattribute__(value),get_language()) else: return super(TransCharField, self).__getattribute__(value) Inside model then for example: name = TransCharField(max_length=255) I did some testing and it seems to work proper as I want it, I can just use the orm normal and using name and inside the query the field get translated to the right name. My question is if that solution can have any side effects or if there is a better way to do that, I will btw only do read operations on those tables, there will never be write/updates to it via django. Greetings, Thorsten -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.