Using the MODX Lexicon Outside of MODX

One of the banes of my existence is writing MODX extras that will run inside and outside of MODX. Worse yet, sometimes they also run in a browser and from the command line.

In an earlier Article, I wrote about how to instantiate MODX outside of MODX. That makes things easier since you can use any MODX methods once you’ve instantiated the $modx object, but what about the parts of the program that might run before that? If they display messages to the user (possibly error messages), you might like to use lexicon strings to internationalize those messages.

I’m currently working on an extra that runs for some time outside of MODX and has a lot of output during that period. I needed a way to simulate the MODX lexicon so I could internationalize the output, and because I wanted to use MyComponent’s LexiconHelper and the program might be running inside of MODX, I needed to make the standard calls to $modx->lexicon().

This turns out to be surprisingly easy to do. Just create your lexicon files as usual and then do something like this in your class file’s initialization code:

/* See if we're in MODX or not */
$inModx = isset($modx) && $modx instanceof modX;

if (! $inModx) {
    $language = 'en';
    /* Set the $modx variable to point to this class */
    $this->modx =& $this;
    $lexPath = dirname(dirname(dirname(__FILE__))) .
    '/lexicon/' . $language . '/default.inc.php';

    if (file_exists($lexPath)) {
        include_once($lexPath);
        /* @var $_lang string */
        $this->lexStrings = $_lang;
    } else {
        die('could not find lexicon file: ' . $lexPath);
    }
} else {
    $this->modx->lexicon->load('MyProgram:default');
}

If the code above runs outside of MODX, the $this->modx class variable will be set to the class itself. That means you can call any of the class’s methods like this:

$this->modx->methodName();

Now, we need to dummy up the MODX lexicon method. Since the code above sets puts the lexicon strings in the $this->lexStrings variable, that couldn’t be much simpler:

public function lexicon($key) {
    if (isset($key)) {
        return ($this->lexStrings[$key]);
    } else {
        return $key;
    }
}

Now we can get any lexicon string with $this->modx->lexicon(), whether we’re running inside MODX or not.

Bonus Code

Here’s a slightly different version for people using MyComponent, where the lexicon calls are in the form $this->modx->lexicon('key~~lexicon string') until after you run LexiconHelper to normalize them:

public function lexicon($key) {
    $couple = explode('~~', $key);
    $key = $couple[0];
    if (isset($this->lexStrings[$key])) {
        return ($this->lexStrings[$key]);
    } else {
        return isset($couple[1])
            ? $couple[1]
            : $couple[0];
    }
}

The code above will return the lexicon string from the file if it’s there. If not it will return the part after the ‘~~‘, or if there’s no ‘~~‘ it will return the $key.


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray

I am the author of MODX: The Official Guide and over 30 MODX add-on components. I host Bob's Guides, a source of valuable information for MODX users, and I've been very active in the MODX Forums with over 14,000 posts.

Leave a Reply

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


8 − = 6

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>