2013/11/13

Plone Header I18N

On creating a new Plone instance, you will be asked to set its title and description. If localized strings are used for the title field, they are literally displayed in <head><title>Title Here</title></head>. We can not easily switch the the title strings based on a chosen language.

Maybe plone.app.multilingual will take care of this issue, as a whole multilingual story. Anyway, I try to solve this issue by manipulating viewlets.

My theme is based on Sunburst, that uses main_template.pt as the starting point. plone.htmlhead provider (viewlet manager) is where we need to hack into:

<div tal:replace="structure provider:plone.htmlhead" />

See plone.app.layout/viewlets/configure.zcml and we will find the related viewlet manager and viewlets:

<browser:viewletManager
  name="plone.htmlhead"
  provides=".interfaces.IHtmlHead"
  permission="zope2.View" ...>

<browser:viewlet
  name="plone.htmlhead.title"
  manager=".interfaces.IHtmlHead"
  class=".common.TitleViewlet" ...>

Now we know TitleViewlet in plone.app.layout/viewlets/common.py is the target:

class TitleViewlet(ViewletBase):
  index = ViewPageTemplateFile('title.pt')

  @property
  @memoize
  def page_title(self):
    '''
    Get the page title.
    '''

Each page has its site_title set as page_titleportal_title. With a little help from my friends -- zope.i18n.translate:

from zope.i18n import translate

def update(self):
  ...
  self.site_title = u"%s &mdash; %s" % (self.page_title,
    translate('portal_title',
              domain='my.theme',
              context=self.request,
              default='I18N Site'))

Finally we need my.theme.po in the locales folder:

#: Default: I18N Site
msgid "portal_title"
msgstr "中文名稱"

No comments: