Using the MODX Lexicon Outside of MODX

A method to use MODX lexicon strings that will work inside or 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 MOD, 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 because 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 classes 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.



Comments (0)


Please login to comment.

  (Login)