Normalizing ids

Description

How to convert arbitary text input to URL/CSS/file/programming safe ids.

Introduction

Normalizers turns arbitary string (with unicode letters) to machine friendly ASCII ids. Plone provides different id normalizers.

E.g:

åland -> aland

Plone has conversion utilies for

  • For URIs and URLs (plone.i18n.normalizer.interfaces.IURLNormalizer)
  • For filenames
  • For HTML ids and CSS

Normalization depends on the locale. E.g. in English "Š" will be normalized as "ae" but in Finnish it will be normalized "å" -> "a".

See plone.i18n.normalizers package.

Examples

Simple example for CSS id:

from zope.component import getUtility
from plone.i18n.normalizer.interfaces import IIDNormalizer

normalizer = getUtility(IIDNormalizer)
id = "portlet-static-%s" % normalizer.normalize(header)

Hard-coded id localizer which directly uses class instance and does not allow override by utility configuration. You can use normalizers this way also when getUtility() is not available (e.g. start up code):

from plone.i18n.normalizer import idnormalizer

id = idnormalizer.normalize(u"ÅÄÖrjy")

Language specific example for URL:

from zope.component import getUtility
from plone.i18n.normalizer.interfaces import IURLNormalizer

    # Get URL normalizer for language english
util = queryUtility(IURLNormalizer, name="en")

To see available language specific localizers, see the source code of plone.i18n.normalizers package.

More examples:

Creating ids programmatically

If you are creating content programmatically using invokeFactory() or by calling the class constructor you need to provide the id yourself.

Below is an example how to generate id from a title. container is the folderish object that will contain our new object.:

import time
import transaction
from zope.container.interfaces import INameChooser

# For the NameChooser to work, it needs our object to already exist.
# So we create our object with a temporary but unique id. Seconds since
# epoch will do.
oid = container.invokeFactory(portal_type, id=time.time())

# It's necessary to save the object creation before we can rename it
transaction.savepoint(optimistic=True)
new_obj = container._getOb(oid)

# Now we create and set a new user-friendly id from the object title
title = "My Little Pony"
oid = INameChooser(container).chooseName(title, new_obj)
content.setId(oid)
content.reindexObject()

Table Of Contents

About Plone

This is documentation for Plone®. Plone is a popular, open source, content management system written in Python programming language.




Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Go to Normalizing ids on GitHub.
  2. Press Fork and edit this file button.
  3. Edit file contents using GitHub's text editor in your web browserm
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on Github.

For basic information about updating this manual and Sphinx format please see Writing and updating the manual guide.