Skip to content

Instantly share code, notes, and snippets.

@em-piguet
Created April 18, 2013 20:09
Show Gist options
  • Save em-piguet/5415835 to your computer and use it in GitHub Desktop.
Save em-piguet/5415835 to your computer and use it in GitHub Desktop.
lanstrip
<?php
/**
 * lanstrip snippet to output different languages, e.g. in Gallery templates.
 * The snippet displays the text matching the current "cultureKey".
 * Can be used anywhere - in chunks, templates etc.
 * The default text must be written first, without a preceeding "#" - this text is used if no matching cultureKey is found.
 * To make the Gallery snippet display a text in different languages, the editor enters the album title like this:
 * "Blå båt #en Blue boat #de Blau Boot"
 * In the gallery item chunk the snippet is called like this:
 * [[lanstrip? &text=`[[+name]]`]]
 *
 * @version 1.0
 * @author Roger Käll (kallteknik.se)
 *
 * @example [[lanstrip? &text=`Svenska #en Engelska #de Tyska`]] or when using a property [[lanstrip? &text=`[[+name]]`]]
 *
 */
 
// ----- Get current culturekey (sv,en,de...)
$cKey = $modx->getOption('cultureKey') ; //<--></-->
 
// ----- Find current key in text
$start = strpos($text, "#".$cKey);
 
// ----- If none print to first "#" and return
if(!$start){
    echo $start;
    $end = strpos($text, "#");
    if($end==false){echo $text; return;};
    echo substr($text,0,$end);
    return;
    };
     
$start = $start + 4;
 
// ----- Find next key (if any)
$end = strpos($text,"#",$start);
 
// ----- If no end set end to stringlength
if(!$end){$end = strlen($text);};
 
// ----- Print result
echo substr($text,$start,$end-$start);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment