def _update_user_actions_with_languages(self, actions): language_selector = LanguageSelector(self.context, self.request, self.view, self.manager) language_selector.update() langs = language_selector.languages() langs.reverse() if len(langs) == 1: return for index, lang in enumerate(langs): switch_lang_url = '{0}/switchLanguage?set_language={1}'.format( self.context.absolute_url(), lang['code']) actions.insert(0, {'category': 'language', 'descrption': '', 'title': lang['native'], 'url': switch_lang_url, 'code': lang['code'], 'selected': lang['selected'], 'id': 'lang-{0}'.format(lang['code']), 'separator': index == 0 })
class Renderer(base.Renderer): def __init__(self, context, request, view, manager, data): base.Renderer.__init__(self, context, request, view, manager, data) self.selector = LanguageSelector(context, request, None, None) self.selector.update() self.languages = self.selector.languages() def key(info): return info.get("native", info["name"]) self.languages.sort(key=key) portal_state = getMultiAdapter((context, request), name='plone_portal_state') self.navigation_root_url = portal_state.navigation_root_url() def show(self): return self.selector.available() and len(self.languages) > 1 @property def available(self): return self.show() def showFlags(self): return self.selector.showFlags() def update(self): pass render = ViewPageTemplateFile('language.pt')
def update(self): self.portal_state = getMultiAdapter((self.context, self.request), name=u'plone_portal_state') self.site_url = self.portal_state.portal_url() ls = LanguageSelector(self.context, self.request, self.view, self.manager) ls.update() results = ls.languages() translatable = ITranslatable(self.context, None) if translatable is not None: translations = translatable.getTranslations() else: translations = [] for data in results: data['translated'] = data['code'] in translations if data['translated']: trans = translations[data['code']][0] state = getMultiAdapter((trans, self.request), name='plone_context_state') data['url'] = state.view_url() + '?set_language=' + data['code'] else: state = getMultiAdapter((self.context, self.request), name='plone_context_state') try: data['url'] = state.view_url() + '?set_language=' + data['code'] except AttributeError: data['url'] = self.context.absolute_url() + '?set_language=' + data['code']
def languages(self): context = aq_inner(self.context) ls = LanguageSelector(context, self.request, None, None) ls.update() results = ls.languages() supported_langs = [v['code'] for v in results] missing = set([str(c) for c in supported_langs]) lsv = LanguageSelectorViewlet(context, self.request, None, None) translations = lsv._translations(missing) # We want to see the babel_view append_path = ('', 'babel_view',) non_viewable = set() for data in results: code = str(data['code']) data['translated'] = code in translations.keys() appendtourl = '/'.join(append_path) if data['translated']: trans, direct, has_view_permission = translations[code] if not has_view_permission: # shortcut if the user cannot see the item non_viewable.add((data['code'])) continue data['url'] = trans.absolute_url() + appendtourl else: non_viewable.add((data['code'])) # filter out non-viewable items results = [r for r in results if r['code'] not in non_viewable] return results
class Renderer(base.Renderer): def __init__(self, context, request, view, manager, data): base.Renderer.__init__(self, context, request, view, manager, data) self.selector = LanguageSelector(context, request, None, None) self.selector.update() self.languages = self.selector.languages() def key(info): return info.get("native", info["name"]) self.languages.sort(key=key) portal_state = getMultiAdapter( (context, request), name='plone_portal_state') self.navigation_root_url = portal_state.navigation_root_url() def show(self): return self.selector.available() and len(self.languages) > 1 @property def available(self): return self.show() def showFlags(self): return self.selector.showFlags() def update(self): pass render = ViewPageTemplateFile('language.pt')
def _update_user_actions_with_languages(self, actions): language_selector = LanguageSelector(self.context, self.request, self.view, self.manager) language_selector.update() langs = language_selector.languages() langs.reverse() if len(langs) == 1: return for index, lang in enumerate(langs): switch_lang_url = '{0}/switchLanguage?set_language={1}'.format( self.context.absolute_url(), lang['code']) actions.insert( 0, { 'category': 'language', 'descrption': '', 'title': lang['native'], 'url': switch_lang_url, 'code': lang['code'], 'selected': lang['selected'], 'id': 'lang-{0}'.format(lang['code']), 'separator': index == 0 })
def test_original_selector_data_not_modified_by_viewlet(self): self.selector.update() multilingual_lang_info = self.selector.languages() original_selector = LanguageSelector(self.portal, self.request, None, None) original_selector.update() original_lang_info = original_selector.languages() self.assertNotEquals(original_lang_info, multilingual_lang_info)
def __init__(self, context, request, view, manager, data): base.Renderer.__init__(self, context, request, view, manager, data) self.selector = LanguageSelector(context, request, None, None) self.selector.update() self.languages = self.selector.languages() def key(info): return info.get("native", info["name"]) self.languages.sort(key=key) portal_state = getMultiAdapter( (context, request), name='plone_portal_state') self.navigation_root_url = portal_state.navigation_root_url()
def test_selector_data_not_modified_by_viewlet(self): selector_viewlet = \ LanguageSelectorViewlet(self.portal, self.request, None, None) selector_viewlet.update() selector_viewlet_languages = selector_viewlet.languages() selector_adapter = \ LanguageSelector(self.portal, self.request, None, None) selector_adapter.update() selector_adapter_languages = selector_adapter.languages() self.assertNotEquals(selector_adapter_languages, selector_viewlet_languages)
def languages(self): current_language = unicode(getMultiAdapter((self.context, self.request), name='plone_portal_state').language()) languages = LanguageSelector.languages(self) translatable = ITranslatable(self.context, None) if translatable is not None: translations = translatable.getTranslations() else: translations = [] results = [] for data in languages: data['translated'] = data['code'] in translations if data['code'] == current_language or not data['translated']: continue if data['translated']: trans = translations[data['code']][0] state = getMultiAdapter((trans, self.request), name='plone_context_state') data['url'] = state.view_url() + '?set_language=' + data['code'] else: state = getMultiAdapter((self.context, self.request), name='plone_context_state') try: data['url'] = state.view_url() + '?set_language=' + data['code'] except AttributeError: data['url'] = self.context.absolute_url() + '?set_language=' + data['code'] results.append(data) return results
def __init__(self, context, request, view, manager, data): base.Renderer.__init__(self, context, request, view, manager, data) self.selector=LanguageSelector(context, request, None, None) self.selector.update() self.languages=self.selector.languages() def key(info): return info.get("native", info["name"]) self.languages.sort(key=key) portal_state = getMultiAdapter((context, request), name='plone_portal_state') self.navigation_root_url = portal_state.navigation_root_url()
def languages(self): """ Deprecated """ context = aq_inner(self.context) ls = LanguageSelector(context, self.request, None, None) ls.update() results = ls.languages() supported_langs = [v['code'] for v in results] missing = set([str(c) for c in supported_langs]) lsv = LanguageSelectorViewlet(context, self.request, None, None) translations = lsv._translations(missing) # We want to see the babel_view append_path = ( '', 'babel_view', ) non_viewable = set() for data in results: code = str(data['code']) data['translated'] = code in translations.keys() appendtourl = '/'.join(append_path) if data['translated']: trans, direct, has_view_permission = translations[code] if not has_view_permission: # shortcut if the user cannot see the item non_viewable.add((data['code'])) continue data['url'] = trans.absolute_url() + appendtourl else: non_viewable.add((data['code'])) # filter out non-viewable items results = [r for r in results if r['code'] not in non_viewable] return results
def languages(self): results = LanguageSelector.languages(self) # On the main portal, we want to be able to filter out unwanted # languages needes for subsites oshaview = getMultiAdapter((self.context, self.request), name='oshaview') subsite_path = oshaview.subsiteRootPath() potential_subsite = self.context.restrictedTraverse(subsite_path) # get the potentially present question- and country-id # Due to apache rewriting, we cannot use PATH_INFO, but must contruct the path manually # first, get the URL and snip the SERVER_URL off path = self.request.get('URL')[len(self.request.get('SERVER_URL'))+1:] # then pre-pend VirtualURL components if present elems = [x for x in self.request.get('VirtualRootPhysicalPath', [''])] + path.split('/') # join it to a path virtual_path = '/'.join(elems) # get the context's path context_path = '/'.join(self.context.getPhysicalPath()) # just an extra test, as the following expression should always be true if virtual_path.startswith(context_path): question_path = virtual_path[len(context_path):] else: question_path = '' group_by = 'group_by' in self.request and '&group_by=' + \ self.request.get('group_by') or '' # for translatable content, directly link to the translated objects translatable = ITranslatable(self.context, None) if translatable is not None: translations = translatable.getTranslations() else: translations = [] for data in results: data['translated'] = data['code'] in translations if data['translated']: trans = translations[data['code']][0] state = getMultiAdapter((trans, self.request), name='plone_context_state') data['url'] = state.view_url() + question_path + \ '?set_language=' + data['code'] + group_by else: state = getMultiAdapter((self.context, self.request), name='plone_context_state') try: data['url'] = state.view_url() + '/not_available_lang?set_language=' + data['code'] except AttributeError: data['url'] = self.context.absolute_url() + '/not_available_lang?set_language=' + data['code'] return results
def languages(self): results = LanguageSelector.languages(self) translations = self.context.getTranslations() for data in results: data['translated'] = data['code'] in translations if data['translated']: trans = translations[data['code']][0] state = queryMultiAdapter((trans, self.request), name='plone_context_state') data['url'] = state.view_url() + '?set_language=' + data['code'] else: state = queryMultiAdapter((self.context, self.request), name='plone_context_state') data['url'] = state.view_url() + '?set_language=' + data['code'] return results
def get_language_info(self): """Returns the current configured languages """ # Use the language selector viewlet viewlet = LanguageSelector(self.context, self.request, None, None) viewlet.update() return { "available": viewlet.available(), "languages": viewlet.languages(), }
def languages(self): context = aq_inner(self.context) results = LanguageSelector.languages(self) supported_langs = [v['code'] for v in results] missing = set([str(c) for c in supported_langs]) translations = self._translations(missing) # We want to preserve the current template / view as used for the # current object and also use it for the other languages append_path = self._findpath(context.getPhysicalPath(), self.request.get('PATH_INFO', '')) formvariables = self._formvariables(self.request.form) _checkPermission = getSecurityManager().checkPermission non_viewable = set() for data in results: code = str(data['code']) data['translated'] = code in translations.keys() set_language = '?set_language=%s' % code try: appendtourl = '/'.join(append_path) if self.set_language: appendtourl += '?' + make_query(formvariables, dict(set_language=code)) elif formvariables: appendtourl += '?' + make_query(formvariables) except UnicodeError: appendtourl = '/'.join(append_path) if self.set_language: appendtourl += set_language if data['translated']: trans, direct, has_view_permission = translations[code] if not has_view_permission: # shortcut if the user cannot see the item non_viewable.add((data['code'])) continue state = getMultiAdapter((trans, self.request), name='plone_context_state') if direct: data['url'] = state.canonical_object_url() + appendtourl else: data['url'] = state.canonical_object_url() + set_language else: has_view_permission = bool(_checkPermission('View', context)) # Ideally, we should also check the View permission of default # items of folderish objects. # However, this would be expensive at it would mean that the # default item should be loaded as well. # # IOW, it is a conscious decision to not take in account the # use case where a user has View permission a folder but not on # its default item. if not has_view_permission: non_viewable.add((data['code'])) continue state = getMultiAdapter((context, self.request), name='plone_context_state') try: data['url'] = state.canonical_object_url() + appendtourl except AttributeError: data['url'] = context.absolute_url() + appendtourl # filter out non-viewable items results = [r for r in results if r['code'] not in non_viewable] return results
def languages(self): context = aq_inner(self.context) results = LanguageSelector.languages(self) translatable = ITranslatable(context, None) if translatable is not None: translations = translatable.getTranslations() else: translations = [] # We want to preserve the current template / view as used for the # current object and also use it for the other languages # We need to find the actual translatable content object. As an # optimization we assume it is one of the last two path segments match = filter(None, context.getPhysicalPath()[-2:]) current_path = filter(None, self.request.get('PATH_INFO', '').split('/')) append_path = [] stop = False while current_path and not stop: check = current_path.pop() if check not in match: append_path.insert(0, check) else: stop = True # we remove virtualhostroot if 'VirtualHostRoot' in append_path: append_path.remove('VirtualHostRoot') if append_path: append_path.insert(0, '') formvariables = self.request.form for k, v in formvariables.items(): if isinstance(v, unicode): formvariables[k] = v.encode('utf-8') for data in results: data['translated'] = data['code'] in translations try: appendtourl = '/'.join(append_path) + \ '?' + make_query(formvariables, dict(set_language=data['code'])) except UnicodeError: appendtourl = '/'.join(append_path) + '?set_language=' + data['code'] if data['translated']: trans = translations[data['code']][0] container = aq_parent(trans) if isDefaultPage(container, trans): trans = container state = getMultiAdapter((trans, self.request), name='plone_context_state') data['url'] = state.view_url() + appendtourl else: container = aq_parent(context) if isDefaultPage(container, context): context = container state = getMultiAdapter((context, self.request), name='plone_context_state') try: data['url'] = state.view_url() + appendtourl except AttributeError: data['url'] = context.absolute_url() + appendtourl return results
def languages(self): context = aq_inner(self.context) results = LanguageSelector.languages(self) supported_langs = [v['code'] for v in results] missing = set([str(c) for c in supported_langs]) translations = self._translations(missing) # We want to preserve the current template / view as used for the # current object and also use it for the other languages append_path = self._findpath(context.getPhysicalPath(), self.request.get('PATH_INFO', '')) formvariables = get_formvariables(self.request) _checkPermission = getSecurityManager().checkPermission non_viewable = set() for data in results: code = str(data['code']) data['translated'] = code in translations.keys() set_language = '?set_language=%s' % code try: appendtourl = '/'.join(append_path) if self.set_language: appendtourl += '?' + make_query(formvariables, dict(set_language=code)) elif formvariables: appendtourl += '?' + make_query(formvariables) except UnicodeError: appendtourl = '/'.join(append_path) if self.set_language: appendtourl += set_language if data['translated']: trans, direct, has_view_permission = translations[code] if not has_view_permission: # shortcut if the user cannot see the item non_viewable.add((data['code'])) continue state = getMultiAdapter((trans, self.request), name='plone_context_state') if direct: data['url'] = state.canonical_object_url() + appendtourl else: data['url'] = state.canonical_object_url() + set_language else: has_view_permission = bool(_checkPermission('View', context)) # Ideally, we should also check the View permission of default # items of folderish objects. # However, this would be expensive at it would mean that the # default item should be loaded as well. # # IOW, it is a conscious decision to not take in account the # use case where a user has View permission a folder but not on # its default item. if not has_view_permission: non_viewable.add((data['code'])) continue state = getMultiAdapter((context, self.request), name='plone_context_state') try: data['url'] = state.canonical_object_url() + appendtourl except AttributeError: data['url'] = context.absolute_url() + appendtourl # filter out non-viewable items results = [r for r in results if r['code'] not in non_viewable] return results