开发者

How to set language on copied event with Plone

I'm using LinguaPlone for my personal website and I have set it up using languages folder.

When I try to copy and paste an image from the en language folder into the 'fr' folder, the language is not changed. So I want to fix this behavior.

I'm trying to fix this at the moment in my own code but I just don't know why it doesn't work.

So the question is: how do I achieve this ? am I on the good way to do this ? what is missing here ?

from zope import component
from zope.globalrequest import getRequest

def updatelang(ob, event):
    current = event.object
    tools = component.getMultiAdapter((ob, getRequest()), name=u'plone_portal_state')
    current_lang = current.getLanguage()
    开发者_StackOverflow中文版lang = tools.language()
    if current_lang != lang:
        current_object.setLanguage(lang)
        ob.reindexObject(idxs=['Language'])

The setLanguage call throws an attribute error on reference_catalog.

Note, I'm working on Plone4.1


Self answer:

LinguaPlone override setLanguage to move the content in the first translated container in the parent chain.

Modify a bit the code to use getField pattern:

from zope import component
from zope.globalrequest import getRequest

def updatelang(ob, event):
    current = event.object
    tools = component.getMultiAdapter((ob, getRequest()), name=u'plone_portal_state')
    current_lang = current.getLanguage()
    lang = tools.language()
    if current_lang != lang:
        current.getField('language').set(current, lang)
        current.reindexObject(idxs=['Language'])

Warning this code doesn do any check on already existing translation (if the current object has a translation in that language it will break things). but doing copy paste from one language to the other is a bad action, may be we should try to make them fail at all with a raise exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜