diff --git a/product/ERP5/Document/WebSite.py b/product/ERP5/Document/WebSite.py index 4ce8e29721e5c2c257613d7d9e607b8482f0f4e3..854d84722f2891b0bafb5f2b6a604bfa36f4ef9e 100644 --- a/product/ERP5/Document/WebSite.py +++ b/product/ERP5/Document/WebSite.py @@ -174,7 +174,7 @@ class WebSite(WebSection): if request.get('AcceptLanguage') is not None: request['AcceptLanguage'].set(name, 100) request.set(WEBSITE_LANGUAGE_KEY, name) - return self.asContext(id=name) + return self.getOriginalDocument().asContext(id=name) return WebSection.getExtensibleContent(self, request, name) def _getExtensibleContent(self, request, name): diff --git a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_doLanguage.py b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_doLanguage.py index bed891a6076869907c9a460c2d3e25abcf53599f..f596c69a92614ba2f7ee89fb353c7a013dfbf785 100644 --- a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_doLanguage.py +++ b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_doLanguage.py @@ -1,3 +1,5 @@ +import re + try: website = context.getWebSiteValue() except AttributeError: @@ -5,25 +7,24 @@ except AttributeError: if website is not None and website.isStaticLanguageSelection(): # Web Mode + root_website = website.getOriginalDocument() + default_language = root_website.getDefaultAvailableLanguage() + root_website_url = root_website.absolute_url() + website_url_pattern = r'^%s(?:%s)*(/|$)' % ( + re.escape(root_website_url), + '|'.join('/' + re.escape(x) for x in root_website.getAvailableLanguageList())) referer_url = context.REQUEST.HTTP_REFERER - default_language = context.getDefaultAvailableLanguage() - current_language = context.Localizer.get_selected_language() - web_site_url = context.getWebSiteValue().absolute_url() - if web_site_url.endswith('/%s' % current_language): - # Quick hack to handle acquisition of temp object - # which is different in the case of a Web Site - web_site_url = web_site_url[:-len(current_language) - 1] - - if current_language == select_language: - redirect_url = referer_url - elif current_language == default_language: - redirect_url = referer_url.replace(web_site_url, '%s/%s' % - (web_site_url, select_language)) - elif select_language == default_language: - redirect_url = referer_url.replace('%s/%s' % (web_site_url, current_language), web_site_url) + if referer_url: + if select_language == default_language: + redirect_url = re.sub(website_url_pattern, r'%s\1' % root_website_url, referer_url) + else: + redirect_url = re.sub(website_url_pattern, r'%s/%s\1' % (root_website_url, select_language), + referer_url) else: - redirect_url = referer_url.replace('%s/%s' % (web_site_url, current_language), - '%s/%s' % (web_site_url, select_language)) + if select_language == default_language: + redirect_url = root_website_url + else: + redirect_url = '%s/%s' % (root_website_url, select_language) return context.REQUEST.RESPONSE.redirect(redirect_url) else: # ERP5 Mode diff --git a/product/ERP5/tests/testERP5Web.py b/product/ERP5/tests/testERP5Web.py index e99a74e35b4fc29e536764fa847958ed37543765..ed53b22b0294e08e99aa767477bff9500791da38 100644 --- a/product/ERP5/tests/testERP5Web.py +++ b/product/ERP5/tests/testERP5Web.py @@ -856,7 +856,7 @@ Hé Hé Hé!""", page.asText().strip()) Test that getWebSiteValue() and getWebSectionValue() always include selected Language. """ - website_id = self.setupWebSite().getId() + website_id = self.setupWebSite(default_available_language='en').getId() website = self.portal.restrictedTraverse( 'web_site_module/%s' % website_id) website_relative_url = website.absolute_url(relative=1) @@ -926,6 +926,46 @@ Hé Hé Hé!""", page.asText().strip()) self.assertEqual(websection_relative_url_fr, webpage_module_fr.getWebSectionValue().absolute_url(relative=1)) + # several languages in URL + website_bg_fr = self.portal.restrictedTraverse( + 'web_site_module/%s/bg/fr' % website_id) + self.assertEqual(website_bg_fr.getOriginalDocument(), website) + websection_bg_fr = self.portal.restrictedTraverse( + 'web_site_module/%s/bg/fr/%s' % (website_id, websection_id)) + webpage_bg_fr = self.portal.restrictedTraverse( + 'web_site_module/%s/bg/fr/%s/%s' % (website_id, websection_id, page_ref)) + + # change language without referer + request = self.portal.REQUEST + request['HTTP_REFERER'] = '' + website_absolute_url = website.absolute_url() + self.assertEqual(website_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url) + self.assertEqual(websection_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url) + self.assertEqual(webpage_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url) + self.assertEqual(website_fr.Base_doLanguage('en'), website_absolute_url) + self.assertEqual(websection_fr.Base_doLanguage('en'), website_absolute_url) + self.assertEqual(webpage_fr.Base_doLanguage('en'), website_absolute_url) + self.assertEqual(website_bg_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url) + self.assertEqual(websection_bg_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url) + self.assertEqual(webpage_bg_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url) + self.assertEqual(website_bg_fr.Base_doLanguage('en'), website_absolute_url) + self.assertEqual(websection_bg_fr.Base_doLanguage('en'), website_absolute_url) + self.assertEqual(webpage_bg_fr.Base_doLanguage('en'), website_absolute_url) + + # change language with referer + request['HTTP_REFERER'] = website_fr.absolute_url() + self.assertEqual(website_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url) + request['HTTP_REFERER'] = websection_fr.absolute_url() + self.assertEqual(websection_fr.Base_doLanguage('de'), websection_fr.absolute_url().replace('/fr/', '/de/')) + request['HTTP_REFERER'] = webpage_fr.absolute_url() + self.assertEqual(webpage_fr.Base_doLanguage('de'), webpage_fr.absolute_url().replace('/fr/', '/de/')) + request['HTTP_REFERER'] = website_bg_fr.absolute_url() + self.assertEqual(website_bg_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url) + request['HTTP_REFERER'] = websection_bg_fr.absolute_url() + self.assertEqual(websection_bg_fr.Base_doLanguage('de'), websection_bg_fr.absolute_url().replace('/bg/fr/', '/de/')) + request['HTTP_REFERER'] = webpage_bg_fr.absolute_url() + self.assertEqual(webpage_bg_fr.Base_doLanguage('de'), webpage_bg_fr.absolute_url().replace('/bg/fr/', '/de/')) + def test_13_DocumentCache(self): """ Test that when a document is modified, it can be accessed through a