Typographic Behavior and Typography Helper

For CakePHP Apps.

What is the issue here?

Typography has quite a few regional differences. In some countries/languages you use quotation marks like so:

“Hello” // English, curly, often in texts (1)
«Hello» // French, brackets, often in texts (2)
‹Hello> // Single brackets, often in texts (3)
„Hello‟ // German, low/up, often in texts (4)
"Hello" // English, basic, unified (5)
...

If many people from different countries and languages contribute to your site and post their text or some snippets this will result in a mess pretty quick. Especially if there is a lot of copy-and-paste involved (texts from different websites) there will be many different chars involved. So if you have a blog or a social network for stories, articles and reports you might want to unify them for the database backend.

How can we solve it?

Wouldn’t it be a good idea to address this issue as soon as possible? On save() for instance. This is where the behavior comes into play. So after the save() the text will be clean and unified.

In a second step you can regional it again using the TypographyHelper. Either for your main country/language or on a personal user-defined basis. The latter is more difficult to work with if caching is involved. In most cases you would just want to display it in the main one – (1) for English, (4) for Germany etc.

Behavior usage

As simple as behaviors are there is only one line to attach to the model:

class Article extends AppModel {

    public $actsAs = array('Tools.Typographic'=>array('fields'=>array('title', 'short_content', 'content', 'annotations')));

}

The behavior needs to know the fields you want to transform. If you do not define any fields it will transform all textarea and text fields!

It usually fires on beforeSave() but you can make it fire prior to validation with 'before' => 'validate' as configuration option.

Helper usage

The helper is pretty straight-forward, as well:

echo $this->Typography->formatCharacter($content);

Don’t forget to add your helper to the public $helpers array of your controller.

The default will be the english style “double quotes”, but you can either pass the locale setting as second argument or globally set it in your configs:

Configure::write('Typography.locale', 'low'); // will produce sth like „double quotes‟ - e.g- for Germany

Feel free to contribute if something is missing so far.

The helper also contains some methods like autoTypography() to transform all kind of things automatically, including the character formatting.

Extra goodies

In case you attach your behavior afterwards (some records already exist uncleaned), you can use updateTypographie from the behavior. So if we create an admin action like so:

public function admin_typographic($dryRun = null) {
    $res = $this->Article->updateTypography((bool)$dryRun);
    $this->Session->setFlash(__('done %s', $res));
    $this->redirect(array('action'=>'index'));
}

we will be able to clean all those in one batch, as well.

Remarks

In combination those two are a good example how text can easily be modified on a modular and clean basis.

If you use a lot of typographic modification in your views you might want to think about caching the final html result. This can reduce server load and increase speed a lot for larger articles/texts.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.