getContext Snippet for MODx Multilingual Site

We've been doing more multiligual sites, and we needed a quick way to output the context (language) we're currently in.


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.

 


Comments (2)

  1. Mark Hamstra:
    Jan 25, 2013 at 10:37 PM

    Or, you can use the existing (or ) tag! Benefit of those are that 1) you don't need a snippet 2) they are already cached. :)

    Another third way, would be to put the bits that need to be translated into the lexicons (System > Lexicon Management), and just use the syntax to add in the multilingual text. That will load the right one based on the cultureKey setting.

    As always, there's many ways to do something in MODX :)

  2. Mark Hamstra:
    Jan 30, 2013 at 03:28 PM

    It looks like it ate the tags in the reply..

    > 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.


Add a Comment





Allowed tags: <b><i><br>Add a new comment: