2012-08-09 4 views
0

장고 번역에 이상한 문제가 있습니다. 알아 내는데 도움이 필요합니다. 문제는 그 대신 번역 된 문자열을 얻을 때마다 내가 임의로 번역 된 문자열 또는 기본 문자열 중 하나를 얻을 것 같다.이상한 django 번역 동작

나는 여러 페이지에 "조치"버튼을 넣기위한 클래스를 만들었습니다. 나는 간단한 도구 모음을 만들어보기

toolbar.add(tool)

를 사용하고 단지 모든 렌더링 templatetag를 사용할 수있을 때 내가 몇 가지 템플릿에

<a href="something">blabla</a>

을 두어야 왜 그 버튼의 대부분은 재사용 도구 .... 어쨌든.

class NewUserGroupButton(MyTool): 
    tool_id = 'new-user-group' 
    button_label = ugettext(u'Create new user group') 
    tool_href = 'new/' 
    js =() 

    def render(self): 
     s = '<a id="%(tool_id)s" href="%(tool_href)s" class="button blue">%(button_label)s</a>' 
     return s % {'tool_id': self.tool_id, 'tool_href': self.tool_href, 'button_label':self.button_label} 

도구는 다음과 같이 표현된다 : : 문자열을 번역 ugettext를 사용하여

class ToolbarTools: 
    def __init__(self): 
     self.tools = [] 

    def add(self, tool): 
     self.tools.append(tool) 

    def render(self): 
     # Organize tools by weight 
     self.tools.sort(key=lambda x: x.weight) 

     # Render tools 
     output = '<ul id="toolbar" class="clearfix">' 

     for tool in self.tools: 
      output += '<li id="%s">' % ('tool-'+ tool.tool_id) 
      output += tool.render() if tool.renderable else '' 
      output += '</li>' 

     output += '</ul>' 

     # Reset tools container 
     self.tools = [] 

     return mark_safe(output) 

메신저

는 하나의 도구의 예입니다. (u) gettext = lambda : s 또는 ugettext_lazy를 사용하면 함수 선택에 해당하는 변환이나 프록시 객체가 없습니다.

그리고 말했듯이 페이지의 나머지 부분은 올바른 언어로되어 있지만 툴바 버튼은 무작위로 번역 된 것처럼 보입니다. 다른 "도구"를 사용하여 다른 페이지로 이동하거나 동일한 페이지를 여러 번 새로 고치면 결함있는 동작이 일관되게 유지됩니다.

누군가이 문제의 원인과 해결 방법을 알아낼 수 있습니까?

답변

2

문제가 해결되었습니다.

문제 자체 (임의 변환)는 ugettext를 사용했을 때 발생했을 가능성이 큽니다. 그러나 동시에 나는 ugettext_lazy를 대신 사용해야했습니다.

그리고 이렇게 문제가 정말 돌아 프록시 개체 ugettext_lazy에서 온 문자열을 번역하지 ... 그리고이 발생합니다 :

[견적] 게으른 번역 작업 ugettext_lazy의 결과() 호출 객체 파이썬에서 유니 코드 문자열 (유니 코드 유형의 객체)을 사용할 때마다 사용할 수 있습니다. bytestring (str 객체)가 예상되는 곳에서 사용하려고하면 ugettext_lazy() 객체가 바이트 스트링으로 변환하는 방법을 모르기 때문에 예상대로 작동하지 않습니다. 바이트 코드 안에서 유니 코드 문자열을 사용할 수 없으므로 일반적인 파이썬 동작과 일치합니다.

This is fine: putting a unicode proxy into a unicode string. 
u"Hello %s" % ugettext_lazy("people") 
This will not work, since you cannot insert a unicode object 
into a bytestring (nor can you insert our unicode proxy there) 
"Hello %s" % ugettext_lazy("people") 

여기에서 취해진/견적] : https://docs.djangoproject.com/en/dev/topics/i18n/translation/#working-with-lazy-translation-objects

앨런