Using lexicons in MODx

Using lexicons and speeding things up a little

Vom

When you start developing MODx sites, you usually get off to a great start. Doing it over and over again lets you know exactly where the bottlenecks are. Menno Pietersen already wrote some great tutorials and suggested some great improvements to MODx workflow and speeding development up in general, but for this tiny lttile piece of material, we'll focus on internationalization.

Normally you would do the following

  1. Add new lexicon entry
  2. pick a name, pick a namespace, pick a value
  3. save
  4. neeeeeeeeeeeext

And you'll notice that this process is taking forever. But this is not really necessary.

What you could also do is this:

  1. Create a namespace
  2. Create a directory structure so the namespace gets recognized ( {core_path}/components/YOURNAMESPACE/lexicon/(de|en|fr)
  3. Create a file default.inc.php in these directories
  4. be happy

As you can imagine, this is way faster than adding eeeeeeeeevery siiiiiiiiiiingle value one by one. You can just copy/paste stuff and change the values in the text-editor and there's no need to wait for ExtJS to commit its changes to the database and fill out the fields all over again.

Be sure to clear cache or use !% for testing

You use double-brackets and a % to access lexicon entries, as you know. You could clear the cache every time you change something or you could just use a ! while you are still developing your site, and of course remove it later for performance reasons.

We realized that this is by far the fastest way to add translations to stuff that is re-occuring in the templates and doesn't belong into resources at all. Common words like "E-Mail", "News", etc. - you'll want them to be internationalized without having to create really unnecessary TVs all over the place.

What's happening under the hood?

This is pretty easy. What you're doing is creating a namespace, which is kind of like registering a component in MODx. This component is not necessarily a piece of software. It can also be a simple piece of data, like the flat _lang array.

MODx will look at the array, and of course look at the path that you assigned it to and look for the reserved directory name "lexicon" and of course for culture keys (like "en", "de", "fr", "nl", etc.)

It will add this to a lexicon under a certain namespace. If you want to avoid the namespace parameter, you might as well load the lexicon through a plugin, but you'd have to read up on that yourself, because you should know what you're doing and not just copy/pasting what you read on "some blog" :)

So what do I do exactly?

That's simple.

  • Create a namespace (Settings or the cog thingy in the top right corner/Namespaces)
  • Define a core path for it
  • Leave the assets path emtpy unless you need it for something else
  • Put default.inc.php files into {COMPONENT_ROOT}/lexicon/en/ etc. for lexicon entries
  • clear cache and check again

FIELDNAME

Entries in language files:

$_lang['demo.news'] = 'English news text';
$_lang['demo.portfolio'] = 'References';

This is what a default.inc.php file should look like. And this is pretty much it. Let us know if you need any help.