Skip to content

Commit

Permalink
Added option to clear translation references.
Browse files Browse the repository at this point in the history
  • Loading branch information
malthe committed Nov 21, 2012
1 parent 31d3b56 commit b348e70
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/collective/multilingual/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
permission="cmf.ManagePortal"
/>

<browser:page
name="clear-translations"
for="..interfaces.IMultilingual"
class=".setup.ClearTranslationsView"
layer="..interfaces.IBrowserLayer"
permission="cmf.ModifyPortalContent"
/>

<browser:page
name="set-translation-for"
for="..interfaces.IMultilingual"
Expand Down
14 changes: 13 additions & 1 deletion src/collective/multilingual/browser/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,19 @@ def getTranslationActionItems(context, request):

menu.sort(key=lambda item: unicode(item['title']))

if not graph.getTranslations():
if graph.getTranslations():
menu.append({
"title": _(u"Clear..."),
"description": _(u"Clear the list of translation references."),
"action": graph.context.absolute_url() + "/@@clear-translations",
"selected": False,
"icon": None,
"extra": {"id": "clearTranslations",
"separator": None,
"class": ""},
"submenu": None,
})
else:
menu.append({
"title": _(u"This is a translation of..."),
"description": _(u"Mark this item as the translation for "
Expand Down
64 changes: 63 additions & 1 deletion src/collective/multilingual/browser/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class IAdding(Interface):
schema.ASCIILine()


class IClearTranslations(Interface):
pass


class ISelectTranslation(Interface):
target = RelationChoice(
title=_(u"Item"),
Expand Down Expand Up @@ -168,6 +172,14 @@ def handleCreate(self, action):


class SetTranslationForView(Form):
label = _(u"Select translation")
description = _(
u"Locate an existing content item for which the current item "
u"is a translation. If the selected item already has been "
u"translated into the current language, the other reference "
u"will be removed."
)

fields = field.Fields(ISelectTranslation)
ignoreContext = True

Expand All @@ -182,8 +194,9 @@ def handleUse(self, action):
return

obj = data['target']
language = aq_base(self.context).language

if aq_base(obj).language == aq_base(self.context).language:
if language == aq_base(obj).language:
lt = getToolByName(self.context, 'portal_languages')

lang_name = self.request.locale.displayNames.languages.get(
Expand All @@ -197,6 +210,25 @@ def handleUse(self, action):

return

# Check if an existing translation in the current language
# already exists.
translations = ITranslationGraph(obj).getTranslations()
for lang_id, item in translations:
if lang_id != language:
continue

parent = ITranslationGraph(item).detach()
if parent is not None:
title = item.Title().decode('utf-8')
IStatusMessage(self.request).addStatusMessage(
_(u"The existing reference to \"${title}\" has "
u"been replaced.", mapping={'title': title}),
"info"
)

modified(parent)

# Register new translation.
ITranslationGraph(self.context).registerTranslation(obj)
modified(obj)

Expand All @@ -206,3 +238,33 @@ def handleUse(self, action):

next_url = self.context.absolute_url()
self.request.response.redirect(next_url)


class ClearTranslationsView(Form):
label = _(u"Clear translations")
description = _(
u"Please confirm that you want to clear the translation "
u"references."
)

fields = field.Fields(IClearTranslations)

@button.buttonAndHandler(_(u'Clear'))
def handleClear(self, action):
data, errors = self.extractData()
if errors:
self.status = _("Please correct errors.")
return

graph = ITranslationGraph(self.context)
graph.clear()
parent = graph.detach()
if parent is not None:
modified(parent)

IStatusMessage(self.request).addStatusMessage(
_(u"References cleared."), "info"
)

next_url = self.context.absolute_url()
self.request.response.redirect(next_url)
2 changes: 1 addition & 1 deletion src/collective/multilingual/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def objectRemovedEvent(context, event):

container = event.oldParent
wrapped = context.__of__(container)
obj = ITranslationGraph(wrapped).removeTranslation()
obj = ITranslationGraph(wrapped).detach()
if obj is not None:
modified(obj)

Expand Down
19 changes: 17 additions & 2 deletions src/collective/multilingual/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,26 @@ def unregisterTranslation(self, parent):
parent.translations = translations
getPersistentTranslationCounter(self.context).change(1)

def removeTranslation(self):
def clear(self):
uuids = aq_base(self.context.translations)
if uuids:
items = map(self.resolve, uuids)
self.context.translations = set()
getPersistentTranslationCounter(self.context).change(1)
return items

return ()

def detach(self):
result = self.catalog(translations=self.uuid)
if len(result) != 1:
if not result:
return

if len(result) > 1:
logger.warn(
"This object is contained in multiple translation graphs!"
)

obj = result[0].getObject()
obj.translations = obj.translations - set((self.uuid, ))
getPersistentTranslationCounter(self.context).change(1)
Expand Down
5 changes: 4 additions & 1 deletion src/collective/multilingual/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ def registerTranslation(parent):
def unregisterTranslation(parent):
"""Removes context as translation for the provided parent."""

def removeTranslation():
def clear():
"""Clear own translation references."""

def detach():
"""Remove context from graph."""


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,33 @@ msgstr "Oversættelse valgt."

msgid "n/a"
msgstr "ikke tilgængeligt"

msgid "Select translation"
msgstr "Vælg oversættelse"

msgid "Clear translations"
msgstr "Fjern referencer til oversættelser"

msgid "Clear"
msgstr "Udfør"

msgid "References cleared."
msgstr "Referencer fjernet."

msgid "Clear the list of translation references."
msgstr "Ryd listen af referencer til oversættelser."

msgid "Clear..."
msgstr "Fjern..."

msgid "Please confirm that you want to clear the translation references."
msgstr "Bekræft venligst, at listen over referencer til oversættelser skal ryddes for dette indholdsobjekt."

msgid "Locate an existing content item for which the current item is a translation. If the selected item already has been translated into the current language, the other reference will be removed."
msgstr "Anvend et eksisterende indholdsobjekt som oversættelse. Bemærk, at hvis det valgte indhold allerede er oversat til det aktuelle sprog, vil denne handling overskrive denne reference."

msgid "The existing reference to \"${title}\" has been replaced."
msgstr "Bemærk, at en reference til en tidligere oversættelse (\"${title}\") er fjernet i forbindelse med denne handling."



3 changes: 2 additions & 1 deletion src/collective/multilingual/tests/test_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def extract_langs(titles):
been set up.
"""

return set([title.split(' ', 1)[0] for title in titles])
langs = [title.split(' ', 1)[0] for title in titles]
return set(lang for lang in langs if len(lang) <= 3)


def extract_actions(items):
Expand Down

0 comments on commit b348e70

Please sign in to comment.