Using the MODX Lexicon Outside of MODX

An easy way to use MODX Lexicon strings both inside and outside of MODX.

By Bob Ray  |  January 18, 2022  |  3 min read
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 earlier articles, I wrote about how to instantiate MODX outside of MODX2 and MODX3. 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. Note that it’s not an instance of MODX. 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.


Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.