Snippet: get ids baed on language

Hi, i have a trilingual site (web, en, tr).
I want to write a snippet which get as input a set of resource IDS from the web langauge and returns a set of ids for that context.

Snippet pheudocode, snippet name: fetchIdsBasedOnContext

    $ids = '1,2,3,4';
    if ($lang == en) {
       $idstoreturn = '5,6,7,8';
    } else if ($lang == en) {
       $idstoreturn = '9,10,11,12';
    } else if ($lang==web) {
       $idstoreturn = $ids;
    return $idstoreturn;

In the end i want to add the result of the snippet in a pdomenu like this


             &outerClass=`nav navbar-nav mr-auto`
             &parentClass=`nav-item dropdown`
             &tplOuter=`@INLINE <ul class="nav navbar-nav">[[+wrapper]]</ul>`
             &tpl=`@INLINE <li[[+classes]]><a href="[[+link]]" [[+attributes]]>[[+menutitle]]</a>[[+wrapper]]</li>`
             &tplParentRow=`@INLINE <li class="dropdown"><a href="[[+link]]" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" class="dropdown-toggle">[[+menutitle]]<span class="caret"></span></a>[[+wrapper]]</li>`
             &tplInner=`@INLINE <ul class="dropdown-menu">[[+wrapper]]</ul>`
             &tplInnerRow=`@INLINE <li[[+classes]]><a href="[[+link]]" [[+attributes]]>[[+menutitle]]</a>[[+wrapper]]</li>`

Do you use Babel to connect the resources between the different langugages?

Yes i do, but i dont know if there is a way to lets say fetch for id = 1 in WEB the id =2 for EN

maybe it helps, to have a look into the babeltranslation - snippet

Are you sure you’re not just looking for a way to filter pdoMenu to the context, using the regular publish/hidemenu flags to identify what goes into the menu of a specific context?

I have written a small utility snippet for this, which I call xlang. It uses the current "cultureKey" from Modx, which you set-up by context if you use Babel for the translation.

You call it uncached in your page template or chunk, like this:

[[!xlang? &de=`123` &fr=`456` &en=`789`]]

In the snippet, you retrieve the content of the "de", "fr" or "en" parameter with the following: :

// Return string in current culture key or empty string if not provided to the snippet
return $modx-&gt;getOption($modx-&gt;cultureKey, $scriptProperties, '');

You can of course define any other language, as long as the Modx cultureKey is defined and you call xlang with the corresponding language code(s).

A few examples of my usage:

<a href="[[~[[xlang? &de=`131` &fr=`236`]]]]">[[xlang? &de=`Member login / logout` &fr=`Login / logout membres`]]</a>

<div class="search">
	<span>[[xlang? &de=`suchen` &fr=`rechercher`]]</span>
	[[SimpleSearchForm? &landing=^`[[xlang? &de=`168` &fr=`168`]]`]]

My actual implementation of the snippet is a little more complex, as I had the requirement to be able to define placeholders in the strings and replace them when calling xlang. I have created a small Gist with my implementation.

1 Like

you can put three back ticks in the lines above and below your code, so that the code is not altered at all

I asked for this info to be put into a sticky on the front page, but was denied.

1 Like