Django 中的内容翻译,兼容 South

发布于 2024-11-27 19:09:07 字数 390 浏览 1 评论 0原文

我正在寻找一种在 Django 中具有可翻译数据库字段的解决方案,并且该解决方案需要与 South 兼容。

我已经找到了 django-transmetatransdb。只有后者似乎与南方兼容,但我对此不确定。另外,transdb 值在数据库中看起来不太好(这是客户真正关心的)。

有人使用过这样的组合吗?我并不特别关注 South,但这似乎是 Django 中模式迁移的方法。

I'm looking for a solution to have translatable database fields in Django, and the solution needs to be compatible with South.

I already found django-transmeta and transdb. Only the latter seems to be compatible with South, but I'm not sure about that. Also, transdb values don't look nice in the database (something the customer actually cares about).

Has anybody worked with such a combination? I'm not particaluary focussed on South, but it seems to be the way to go for schema migration in Django.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

姐不稀罕 2024-12-04 19:09:07

我认为这两个应用程序使用自定义字段,而 South 对此有所抱怨。您可以在此处了解如何为这些自定义字段启用南:http://south.aeracode.org /docs/customfields.html

两个相关链接:

I suppose that tho two apps use custom fields and South complains about that. Here you can read about how to enable south for those custom fields: http://south.aeracode.org/docs/customfields.html.

Two relevant links:

醉生梦死 2024-12-04 19:09:07

因为我们确实知道语言的数量永远不会改变,所以我们转而为每种语言提供字段以及自动读取当前语言的正确字段的简单机制。我们的实现是这样使用的:

class Question(models.Model):
    __metaclass__ = LocalizeModelBase

    text_de = models.TextField(verbose_name=_(u"question text (german)"))
    text_en = models.TextField(verbose_name=_(u"question text (english)"))
    text = Translate

Question().text 然后根据当前语言自动返回正确的字段,并回退到默认语言。

它背后的实现应该是不言自明的:

from django.db.models.base import ModelBase
from django.utils.translation import get_language
from django.conf import settings

__all__ = ('Translate', 'LocalizeModelBase')

# a dummy placeholder object
Translate = object()


class LocalizeModelBase(ModelBase):
    """This meta-class provides automatically translated content properties. Set
    a model field to `Translate` and it will automatically return the property
    with the name of the current language. E.g. if there is a normal member
    `text_de`, a member `text = Translate` and the current language is `de`, then
    an object will return the content of `text_de` when it is asked for the value
    of `text`.
    """
    def __new__(metacls, classname, bases, classDict):
        # find all classDict entries that point to `Translate`
        for key in classDict.keys():
            if classDict[key] is Translate:
                # replace them with a getter that uses the current language
                classDict[key] = make_property(key)
        return super(LocalizeModelBase, metacls).__new__(metacls, classname, bases, classDict)


def make_property(k):
    """Creates a new property that implements the automatic translation
    described above. Every use of `Translate` in a class definition will be
    replaces with a property returned by this function."""
    def pget(self):
        try:
            # try to return the attribute for the current language
            return getattr(self, "%s_%s" % (k, get_language()))
        except AttributeError:
            # use the default language if the current language is not available
            return getattr(self, "%s_%s" % (k, settings.LANGUAGE_CODE))
    return property(pget)

Because we did know that the number of languages will never change, we switched to having fields for each language and a simple mechanism for automatically reading the correct field for the current language. Our implementation is used like this:

class Question(models.Model):
    __metaclass__ = LocalizeModelBase

    text_de = models.TextField(verbose_name=_(u"question text (german)"))
    text_en = models.TextField(verbose_name=_(u"question text (english)"))
    text = Translate

Question().text then automatically returns the correct field, based on the current language with a fallback to the default language.

The implementation behind it should be pretty much self-explaining:

from django.db.models.base import ModelBase
from django.utils.translation import get_language
from django.conf import settings

__all__ = ('Translate', 'LocalizeModelBase')

# a dummy placeholder object
Translate = object()


class LocalizeModelBase(ModelBase):
    """This meta-class provides automatically translated content properties. Set
    a model field to `Translate` and it will automatically return the property
    with the name of the current language. E.g. if there is a normal member
    `text_de`, a member `text = Translate` and the current language is `de`, then
    an object will return the content of `text_de` when it is asked for the value
    of `text`.
    """
    def __new__(metacls, classname, bases, classDict):
        # find all classDict entries that point to `Translate`
        for key in classDict.keys():
            if classDict[key] is Translate:
                # replace them with a getter that uses the current language
                classDict[key] = make_property(key)
        return super(LocalizeModelBase, metacls).__new__(metacls, classname, bases, classDict)


def make_property(k):
    """Creates a new property that implements the automatic translation
    described above. Every use of `Translate` in a class definition will be
    replaces with a property returned by this function."""
    def pget(self):
        try:
            # try to return the attribute for the current language
            return getattr(self, "%s_%s" % (k, get_language()))
        except AttributeError:
            # use the default language if the current language is not available
            return getattr(self, "%s_%s" % (k, settings.LANGUAGE_CODE))
    return property(pget)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文