(id のノーマライズ)

Description

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

はじめに

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 <http://svn.plone.org/svn/plone/plone.i18n/trunk/plone/i18n/normalizer/__init__.py>`_.

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() you need to provide the id yourself. Below is an example how to generate id from a title.

目次

前のトピックへ

(アノテーション)

次のトピックへ

(モンキーパッチ)

このページ