Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. <?php
  2. /**
  3. * Xlang
  4. *
  5. * Copyright 2015 by Jean-Claude Reiss <jreiss@ajr.ch>
  6. *
  7. * Xlang is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * Xlang is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. */
  17. /**
  18. * xlang
  19. *
  20. * DESCRIPTION
  21. *
  22. * This Snippet allows to select the correct language version of a string
  23. * passed as parameter depending on the language selected by the end-user
  24. * You can optionally replace placeholders in the string
  25. *
  26. * PROPERTIES:
  27. *
  28. * &de String in german
  29. * &fr String in French
  30. * &.. Other languages can be added
  31. * &lg language to select (optional)
  32. * Defaults to the current cultureKey
  33. * &xyzabcPlaceholder Other parameters will be considered as replacement values
  34. * for placeholders defined in the lexicon entries or in the custom strings
  35. * These parameters will be matched to corresponding placeholders in
  36. * the multi-language string
  37. * Of course, your placeholders should not be named the same as one
  38. * of the other parameters of the snippet.
  39. *
  40. * USAGE:
  41. *
  42. * [[!xlang? &de=`German [[+xyzabcPlaceholder]]` &fr=`French [[+xyzabcPlaceholder]]`
  43. * &xyzabcPlaceholder=`with replaced string`]]
  44. *
  45. * Note: put "backticks" (`) around the parameters' values
  46. * Use this as a template to insert your texts :
  47. * With placeholder : [[!xlang? &de=`` &fr=`` &xyzabcPlaceholder=``]]
  48. * Without placeholder : [[!xlang? &de=`` &fr=``]]
  49. *
  50. */
  51.  
  52. // Set default values for snippet
  53. $defaultLanguage = 'de';
  54.  
  55. // Initialize temporary string variable
  56. $tmpString = '';
  57.  
  58. // Get cultureKey for current request
  59. $lg = $modx->getOption('lg', $scriptProperties, $modx->cultureKey);
  60.  
  61. // Set temporary variable to string in current culture key
  62. $tmpString = $modx->getOption($lg, $scriptProperties, '');
  63.  
  64. // If string empty => string was not set for current culture key
  65. // => set default language string
  66. if ($tmpString == '') {
  67. $tmpString = $modx->getOption($defaultLanguage, $scriptProperties, '');
  68. }
  69.  
  70. // Replace placeholders in string
  71. // We do not care about the language keys, as they will lanyway not
  72. // replace anything
  73. // Remember: placeholders are enclosed in "[[+" and "]]"
  74. foreach ($scriptProperties as $key => $value) {
  75. $tmpString = str_replace("[[+{$key}]]", $value, $tmpString);
  76. }
  77.  
  78. // Return processed value
  79. return $tmpString;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement