Skip to content

Commit bc6bd9a

Browse files
authored
Merge pull request #147 from syslabcom/scrum-889-translated-default-page
Use default page in correct language for publications if possible
2 parents baee0e1 + db3d7c2 commit bc6bd9a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/recensio/plone/browser/configure.zcml

+8
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@
146146
layer="recensio.plone.interfaces.IRecensioPloneLayer"
147147
/>
148148

149+
<browser:page
150+
name="default_page"
151+
for="recensio.plone.content.publication.IPublication"
152+
class=".publications.PublicationDefaultPage"
153+
permission="zope2.View"
154+
layer="recensio.plone.interfaces.IRecensioPloneLayer"
155+
/>
156+
149157
<!-- XXX: maybe we restrict this to a particular interface? -->
150158
<browser:page
151159
name="publications-view"

src/recensio/plone/browser/publications.py

+29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from DateTime import DateTime
22
from plone import api
33
from plone.memoize import ram
4+
from Products.CMFPlone.browser.defaultpage import DefaultPage
45
from Products.Five.browser import BrowserView
56
from recensio.plone.browser.canonical import CanonicalURLHelper
67

@@ -9,6 +10,34 @@ def _render_cachekey(method, self, brain, lang):
910
return (brain.getPath(), lang, DateTime().dayOfYear())
1011

1112

13+
class PublicationDefaultPage(DefaultPage):
14+
"""Translated default page"""
15+
16+
def getDefaultPage(self):
17+
"""Get the default page, if possible in the correct language.
18+
We expect the default page to have no language suffix (e.g. 'index').
19+
Translations should have a language suffix (e.g. 'index-en').
20+
If this fails we check the language attribute of all contained documents.
21+
If there is only one document with the right language we return that.
22+
"""
23+
default_page = super().getDefaultPage()
24+
language = api.portal.get_current_language()
25+
defob = self.context.get(default_page)
26+
if not defob or defob.Language() == language:
27+
return default_page
28+
documents = self.context.listFolderContents(dict(portal_type="Document"))
29+
id_match = [
30+
item for item in documents if item.getId() == f"{default_page}-{language}"
31+
]
32+
if len(id_match) == 1:
33+
return id_match[0].getId()
34+
35+
language_match = [item for item in documents if item.Language() == language]
36+
if len(language_match) == 1:
37+
return language_match[0].getId()
38+
return default_page
39+
40+
1241
class PublicationsView(BrowserView, CanonicalURLHelper):
1342
"""Overview page of publications."""
1443

0 commit comments

Comments
 (0)