def get(self): uuid = self.getTranslationUuid() if isLanguageIndependent(self.field) and uuid: manager = TranslationManager(uuid) result = manager.get_translations() if len(result) >= 1: orig_lang = list(result.keys())[0] obj = result[orig_lang] name = self.field.__name__ # XXX # this does not work with behaviors, if other than direct # attribute storage was used. try: value = getattr(aq_base(obj), name) except AttributeError: pass else: return value if self.field.default is None: return NO_VALUE return self.field.default
def get(self): uuid = self.getTranslationUuid() if isLanguageIndependent(self.field) and uuid: manager = TranslationManager(uuid) result = manager.get_translations() if len(result) >= 1: orig_lang = result.keys()[0] obj = result[orig_lang] name = self.field.__name__ # XXX # this does not work with behaviors, if other than direct # attribute storage was used. try: value = getattr(aq_base(obj), name) except AttributeError: pass else: return value if self.field.default is None: return NO_VALUE return self.field.default
class not_translated_yet(BrowserView): """ View to inform user that the view requested is not translated yet, and shows the already translated related content. """ __call__ = ViewPageTemplateFile('templates/not_translated_yet.pt') def __init__(self, context, request): super(not_translated_yet, self).__init__(context, request) self.tg = None def publishTraverse(self, request, name): if self.tg is None: # ../@@not_translated_yet/translationgroup self.tg = TranslationManager(name) else: raise NotFound(self, name, request) return self def already_translated(self): if self.tg is not None: # Need to check if the user has permission return self.tg.get_restricted_translations().items() else: return [] def has_any_translation(self): if self.tg is not None: return len(self.tg.get_restricted_translations().items()) > 0 else: return 0 def language_name(self, lang=None): """ Get the current language native name """ if lang is None: lang_code = self.request.get('set_language') else: lang_code = lang util = getUtility(IContentLanguageAvailability) data = util.getLanguages(True) lang_info = data.get(lang_code) if lang_info is None: return None return lang_info.get('native', None) or lang_info.get('name')
def __init__(self, context, request): super(BabelUtils, self).__init__(context, request) portal_state = getMultiAdapter((context, request), name="plone_portal_state") self.portal_url = portal_state.portal_url() # If there is any translation_info lets use it try: self.group = TranslationManager(request.translation_info['tg']) except AttributeError: self.group = ITranslationManager(self.context)
def get(self): uuid = self.getTranslationUuid() if isLanguageIndependent(self.field) and uuid: manager = TranslationManager(uuid) result = manager.get_translations() if len(result) >= 1: orig_lang = result.keys()[0] obj = result[orig_lang] name = self.field.__name__ try: value = getattr(aq_base(obj), name) except AttributeError: pass else: return value if self.field.default is None: return NO_VALUE return self.field.default
def publishTraverse(self, request, name): if self.tg is None: # ../@@not_translated_yet/translationgroup self.tg = TranslationManager(name) else: raise NotFound(self, name, request) return self
def getClosestDestination(self): """Get the "closest translated object" URL. """ # We should travel the parent chain using the catalog here, # but I think using the acquisition chain is faster # (or well, __parent__ pointers) because the catalog # would require a lot of queries, while technically, # having done traversal up to this point you should # have the objects in memory already # As we don't have any content object we are going to look # for the best option site = getSite() root = getToolByName(site, 'portal_url') ltool = getToolByName(site, 'portal_languages') # We are using TranslationManager to get the translations of a # string tg manager = TranslationManager(self.tg) context = None languages = manager.get_translations() if len(languages) == 0: # If there is no results there are no translations # we move to portal root return self.wrapDestination(root(), postpath=False) # We are going to see if there is the preferred language translation # Otherwise we get the first as context to look for translation prefered = ltool.getPreferredLanguage(self.request) if prefered in languages: context = languages[prefered] else: context = languages[languages.keys()[0]] checkPermission = getSecurityManager().checkPermission chain = self.getParentChain(context) for item in chain: if ISiteRoot.providedBy(item) \ and not ILanguageRootFolder.providedBy(item): # We do not care to get a permission error # if the whole of the portal cannot be viewed. # Having a permission issue on the root is fine; # not so much for everything else so that is checked there return self.wrapDestination(item.absolute_url()) elif IFactoryTempFolder.providedBy(item) or \ IFactoryTool.providedBy(item): # TempFolder or portal_factory, can't have a translation continue try: canonical = ITranslationManager(item) except TypeError: if not ITranslatable.providedBy(item): # In case there it's not translatable go to parent # This solves the problem when a parent is not # ITranslatable continue else: raise translation = canonical.get_translation(self.lang) if translation and ( INavigationRoot.providedBy(translation) or bool(checkPermission('View', translation)) ): # Not a direct translation, therefore no postpath # (the view might not exist on a different context) return self.wrapDestination(translation.absolute_url(), postpath=False) # Site root's the fallback return self.wrapDestination(root(), postpath=False)
def getClosestDestination(self): """Get the "closest translated object" URL. """ # We should travel the parent chain using the catalog here, # but I think using the acquisition chain is faster # (or well, __parent__ pointers) because the catalog # would require a lot of queries, while technically, # having done traversal up to this point you should # have the objects in memory already # As we don't have any content object we are going to look # for the best option site = getSite() root = getToolByName(site, 'portal_url') ltool = getToolByName(site, 'portal_languages') # We are using TranslationManager to get the translations of a # string tg try: manager = TranslationManager(self.tg) languages = manager.get_translations() except AttributeError: languages = [] if len(languages) == 0: # If there is no results there are no translations # we move to portal root return self.wrapDestination(root(), postpath=False) # We are going to see if there is the preferred language translation # Otherwise we get the first as context to look for translation prefered = ltool.getPreferredLanguage(self.request) if prefered in languages: context = languages[prefered] else: context = languages[list(languages.keys())[0]] checkPermission = getSecurityManager().checkPermission chain = self.getParentChain(context) for item in chain: if ISiteRoot.providedBy(item) \ and not ILanguageRootFolder.providedBy(item): # We do not care to get a permission error # if the whole of the portal cannot be viewed. # Having a permission issue on the root is fine; # not so much for everything else so that is checked there return self.wrapDestination(item.absolute_url()) elif IFactoryTempFolder.providedBy(item) or \ IFactoryTool.providedBy(item): # TempFolder or portal_factory, can't have a translation continue try: canonical = ITranslationManager(item) except TypeError: if not ITranslatable.providedBy(item): # In case there it's not translatable go to parent # This solves the problem when a parent is not # ITranslatable continue else: raise translation = canonical.get_translation(self.lang) if translation and (INavigationRoot.providedBy(translation) or bool(checkPermission('View', translation))): # Not a direct translation, therefore no postpath # (the view might not exist on a different context) return self.wrapDestination(translation.absolute_url(), postpath=False) # Site root's the fallback return self.wrapDestination(root(), postpath=False)