def update(self): """ Update the adapted item. If unregistered, register a Translation-Grouup (TG) for it and exit. Check that there aren't two translations on the same language This is to be used for changing the contexts language. If there is already an item in the same language, Remove the other items TG information and make the current adapted context the active language for the current TG. """ language = ILanguage(self.context).get_language() brains = self.pcatalog.unrestrictedSearchResults( TranslationGroup=self.tg, Language=language, ) if len(brains) == 0: # There is no translation within current TG for this language. self.register_translation(language, self.context) return # In case the language is already within the translated languages we are # going to orphan the old translation. brain = brains[0] content_id = self.get_id(self.context) if brain.UID == content_id: return # It is a different object -> remove the old one # We get the old uuid old_object = brain.getObject() IMutableTG(old_object).set(NOTG) old_object.reindexObject(idxs=("TranslationGroup", )) notify(TranslationUpdatedEvent(self.context, old_object, language))
def _update_deferred(self): for path, canonicalpath, language in self.deferred: obj = self._traverse(path) if obj is None: continue canonical = self._traverse(canonicalpath) if canonical is None: continue if (ITranslatable.providedBy(obj) and ITranslatable.providedBy(canonical)): try: translation_group = IMutableTG(canonical).get() IMutableTG(obj).set(translation_group) manager = ITranslationManager(obj) manager.register_translation(language, obj) except KeyError: # put obj in a separate translation group when registration # fails IMutableTG(obj).set(self.uuid_generator())
def createdEvent(obj, event): """ Subscriber to set language on the child folder It can be a - IObjectRemovedEvent - don't do anything - IObjectMovedEvent - IObjectAddedEvent - IObjectCopiedEvent """ if IObjectRemovedEvent.providedBy(event): return request = getattr(event.object, 'REQUEST', getRequest()) if not IPloneAppMultilingualInstalled.providedBy(request): return # On ObjectCopiedEvent and ObjectMovedEvent aq_parent(event.object) is # always equal to event.newParent. parent = aq_parent(event.object) # special parent handling if not ITranslatable.providedBy(parent): set_recursive_language(obj, LANGUAGE_INDEPENDENT) return # Normal use case # We set the tg, linking language = ILanguage(parent).get_language() set_recursive_language(obj, language) request = getattr(event.object, 'REQUEST', getRequest()) try: ti = request.translation_info except AttributeError: return # AT check portal = getSite() portal_factory = getToolByName(portal, 'portal_factory', None) if (not IDexterityContent.providedBy(obj) and portal_factory is not None and not portal_factory.isTemporary(obj)): return IMutableTG(obj).set(ti['tg']) modified(obj) tm = ITranslationManager(obj) old_obj = tm.get_translation(ti['source_language']) ILanguageIndependentFieldsManager(old_obj).copy_fields(obj)
def register_translation(self, language, content): """ register a translation for an existing content """ if not language and language != '': raise KeyError('There is no target language') if type(content) == str: content = uuidToObject(content) # Check if exists and is not myself brains = self.pcatalog.unrestrictedSearchResults( TranslationGroup=self.tg, Language=language) if len(brains) > 0 and brains[0].UID != self.get_id(content): raise KeyError("Translation already exists") # register the new translation in the canonical IMutableTG(content).set(self.tg) content.reindexObject(idxs=('Language', 'TranslationGroup')) notify(TranslationRegisteredEvent(self.context, content, language))
def __iter__(self): for item in self.previous: pathkey = self.pathkey(*item.keys())[0] # not enough info if not pathkey: yield item continue path = item[pathkey] # Skip the Plone site object itself if not path: yield item continue obj = self.context.unrestrictedTraverse(path.encode().lstrip('/'), None) if not IDexterityContent.providedBy(obj): # Path doesn't exist # obj can not only be None, but also the value of an attribute, # which is returned by traversal. yield item continue # fetch lang and translation group lang = item.get(self.langkey) tg = item.get(self.tgkey) # no translation if not lang or not tg: yield item continue # set language and translation group ILanguage(obj).set_language(lang) IMutableTG(obj).set(tg) obj.reindexObject() yield item
def __iter__(self): for item in self.previous: pathkey = self.pathkey(*item.keys())[0] if not pathkey: yield item continue path = item[pathkey] if isinstance(path, unicode): path = path.encode('ascii') obj = self._traverse(path) if obj is None: yield item continue canonicalkey = self.canonicalkey(*item.keys())[0] translationkey = self.translationkey(*item.keys())[0] if not canonicalkey: yield item continue canonicalpath = item[translationkey] if isinstance(canonicalpath, unicode): canonicalpath = canonicalpath.encode('ascii') language = item['language'] if ITranslatable.providedBy(obj): ILanguage(obj).set_language(language) if item[canonicalkey]: IMutableTG(obj).set(self.uuid_generator()) manager = ITranslationManager(obj) manager.register_translation(language, obj) else: self.deferred.append((path, canonicalpath, language)) yield item self._update_deferred()
def update(self): """ see interfaces""" language = ILanguage(self.context).get_language() # self.context.reindexObject(idxs=("Language", "TranslationGroup", )) # In case language is already on the translated languages we are # going to orphan the old translation brains = self.pcatalog.unrestrictedSearchResults( TranslationGroup=self.tg, Language=language) if len(brains) == 0: # There is not a translation with this tg on this language self.register_translation(language, self.context) else: # We need to check if the language has changed brain = brains[0] content_id = self.get_id(self.context) if brain.UID != content_id: # Is a different object -> remove the old one # We get the old uuid old_object = brain.getObject() IMutableTG(old_object).set(NOTG) old_object.reindexObject(idxs=( "Language", "TranslationGroup", ))
def remove_translation(self, language): """ see interfaces """ translation = self.get_translation(language) IMutableTG(translation).set(NOTG) translation.reindexObject()
def remove_translation(self, language): """ see interfaces """ translation = self.get_translation(language) IMutableTG(translation).set(NOTG) translation.reindexObject(idxs=("TranslationGroup", )) notify(TranslationRemovedEvent(self.context, translation, language))