2011-08-02 7 views
1

私はDjangoで翻訳可能なデータベースフィールドを持つソリューションを探しています。ソリューションはSouthと互換性が必要です。Djangoでのコンテンツ翻訳、南との互換性

私はすでにdjango-transmetatransdbが見つかりました。後者だけが南と互換性があるようですが、私はそれについては分かりません。また、transdbの値はデータベースではよく見えません(顧客が実際に気にするもの)。

このような組み合わせで作業した人はいますか?私は南部に焦点を当てたparticaluaryではありませんが、それはDjangoでスキーマの移行のために行く方法のようです。

答えて

0

我々は言語の数が変化しないことを知っていた、我々はフィールドを持つに切り替えているので現在の言語の正しいフィールドを自動的に読み込むための簡単なメカニズムを提供します。その後、自動的にデフォルトの言語へのフォールバックと現在の言語に基づいて、適切なフィールドを返し

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)