getContext Snippet for MODx Multilingual Site

Below is the snippet code, yup, that's it.  Create a new snippet and call it getContext, done.  MODx API Reference.

<?php
$contextKey = $modx->context->key;
return $contextKey;

So how might you use it?  

We used it in getResources templates for "read more" buttons that needed translations. In our case it was "View Bio" but you get the idea.

 <a class="button radius nice" href="">
</a>

What this does is, if getContext is = web, that means it's in the defualt context of English; if not we'll assume Spanish. 

There might be other ways to do this, but it worked for us.

Update

Turns out there are better ways.

 Mark Hamstra from the MODx Team commented:

Or, you can use the existing [ [*context_key (or [ [+context_key) tag!
Another third way, would be to put the bits that need to be translated into the lexicons (System > Lexicon Management), and just use the [ [%some.lexicon.key syntax to add in the multilingual text.

 

Recent Posts

MODX Cloud NGINX Rule for ssl and www redirect

Replace "domain.com" with your domain. This example uses www as the end point, if you prefer non-www just change the two domain variations around in the first if statement. if ($host = "domain.com") { return 301 $scheme://www.domain.com$request_uri; } if…

PDF From Adobe Illustrator not showing a mask transparency right on macOS Preview or Windows? Here is the fix

So you sent a small preview PDF to the client, it looks great. Then you export the Press Quality version and it looks different, why? Let's dig in.

Improving MODX Page Speed for Google

Your website speed is impacting your bottom line , here are a few ways to improve it through browser caching, asynchronous loading of resources and other tips and tricks.