-
-
Save philippeowagner/6825714 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| """A ghetto stupid dead simple approach for model translation.""" | |
| from django.utils.translation import get_language | |
| class LanguageFieldProxy(object): | |
| """ | |
| Thanks to ojii for providing this snippet. | |
| Usage: | |
| class MyModel(models.Model): | |
| field_de = models.CharField(max_length=255) | |
| field_ja = models.CharField(max_length=255) | |
| field = LanguageFieldProxy() | |
| """ | |
| def contribute_to_class(self, cls, name): | |
| self.name = name | |
| setattr(cls, self.name, self) | |
| def __get__(self, instance, instance_type=None): | |
| if not instance: | |
| return '' | |
| return getattr(instance, '%s_%s' % (self.name, get_language())) | |
| def __set__(self, instance, value): | |
| setattr(instance, '%s_%s' % (self.name, get_language()), value) | |
| def __delete__(self, instance): | |
| delattr(instance, '%s_%s' % (self.name, get_language())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment