Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Xlang
- *
- * Copyright 2015 by Jean-Claude Reiss <jreiss@ajr.ch>
- *
- * Xlang is free software; you can redistribute it and/or modify it under the
- * terms of the GNU General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option) any later
- * version.
- *
- * Xlang is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- *
- */
- /**
- * xlang
- *
- * DESCRIPTION
- *
- * This Snippet allows to select the correct language version of a string
- * passed as parameter depending on the language selected by the end-user
- * You can optionally replace placeholders in the string
- *
- * PROPERTIES:
- *
- * &de String in german
- * &fr String in French
- * &.. Other languages can be added
- * &lg language to select (optional)
- * Defaults to the current cultureKey
- * &xyzabcPlaceholder Other parameters will be considered as replacement values
- * for placeholders defined in the lexicon entries or in the custom strings
- * These parameters will be matched to corresponding placeholders in
- * the multi-language string
- * Of course, your placeholders should not be named the same as one
- * of the other parameters of the snippet.
- *
- * USAGE:
- *
- * [[!xlang? &de=`German [[+xyzabcPlaceholder]]` &fr=`French [[+xyzabcPlaceholder]]`
- * &xyzabcPlaceholder=`with replaced string`]]
- *
- * Note: put "backticks" (`) around the parameters' values
- * Use this as a template to insert your texts :
- * With placeholder : [[!xlang? &de=`` &fr=`` &xyzabcPlaceholder=``]]
- * Without placeholder : [[!xlang? &de=`` &fr=``]]
- *
- */
- // Set default values for snippet
- $defaultLanguage = 'de';
- // Initialize temporary string variable
- $tmpString = '';
- // Get cultureKey for current request
- $lg = $modx->getOption('lg', $scriptProperties, $modx->cultureKey);
- // Set temporary variable to string in current culture key
- $tmpString = $modx->getOption($lg, $scriptProperties, '');
- // If string empty => string was not set for current culture key
- // => set default language string
- if ($tmpString == '') {
- $tmpString = $modx->getOption($defaultLanguage, $scriptProperties, '');
- }
- // Replace placeholders in string
- // We do not care about the language keys, as they will lanyway not
- // replace anything
- // Remember: placeholders are enclosed in "[[+" and "]]"
- foreach ($scriptProperties as $key => $value) {
- $tmpString = str_replace("[[+{$key}]]", $value, $tmpString);
- }
- // Return processed value
- return $tmpString;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement