text.php
216 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikWP - Libraries |
| 4 | * @subpackage adapter.language |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * Text adapter class. |
| 16 | * |
| 17 | * @since 10.0 |
| 18 | */ |
| 19 | class JText |
| 20 | { |
| 21 | /** |
| 22 | * JavaScript strings store. |
| 23 | * |
| 24 | * @var array |
| 25 | * @since 10.1.14 |
| 26 | */ |
| 27 | protected static $strings = array(); |
| 28 | |
| 29 | /** |
| 30 | * Translates a string into the current language. |
| 31 | * |
| 32 | * @param string $string The string to translate. |
| 33 | * @param boolean $jsSafe Make the result javascript safe. |
| 34 | * @param boolean $interpretBackSlashes True to interpret backslashes (\\=\, \n=carriage return, \t=tabulation). |
| 35 | * |
| 36 | * @return string The translated string. |
| 37 | */ |
| 38 | public static function _($string, $jsSafe = false, $interpretBackSlashes = true) |
| 39 | { |
| 40 | // get current language |
| 41 | $lang = JFactory::getLanguage(); |
| 42 | |
| 43 | // translate the string |
| 44 | return $lang->_($string, $jsSafe, $interpretBackSlashes); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Proxy for underscore method. |
| 49 | * Needed to bypass the issue reported here: |
| 50 | * @link https://meta.trac.wordpress.org/ticket/3601 |
| 51 | * |
| 52 | * @since 10.1.32 |
| 53 | */ |
| 54 | public static function translate($string, $jsSafe = false, $interpretBackSlashes = true) |
| 55 | { |
| 56 | return static::_($string, $jsSafe, $interpretBackSlashes); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Passes a string thru a sprintf. |
| 61 | * Note that this method can take a mixed number of arguments as for the sprintf function. |
| 62 | * |
| 63 | * The last argument can take an array of options: |
| 64 | * array('jsSafe'=>boolean, 'interpretBackSlashes'=>boolean) |
| 65 | * |
| 66 | * where: |
| 67 | * |
| 68 | * 'jsSafe' is a boolean to generate a javascript safe strings. |
| 69 | * 'interpretBackSlashes' is a boolean to interpret backslashes \\->\, \n->new line, \t->tabulation. |
| 70 | * |
| 71 | * @param string $string The string to translate. |
| 72 | * |
| 73 | * @return string The translated strings. |
| 74 | */ |
| 75 | public static function sprintf($string) |
| 76 | { |
| 77 | $args = func_get_args(); |
| 78 | // pop the key to translate from the array |
| 79 | array_shift($args); |
| 80 | |
| 81 | // check if the last element is an array (to check for custom options) |
| 82 | if (is_array(end($args))) |
| 83 | { |
| 84 | // pop the options from the args list |
| 85 | $options = array_pop($args); |
| 86 | |
| 87 | // check for js safe |
| 88 | $jsSafe = isset($options['jsSafe']) |
| 89 | ? $options['jsSafe'] |
| 90 | : false; |
| 91 | |
| 92 | // check for interpret backslashes |
| 93 | $interpretBackSlashes = isset($options['interpretBackSlashes']) |
| 94 | ? $options['interpretBackSlashes'] |
| 95 | : true; |
| 96 | } |
| 97 | // otherwise set default options |
| 98 | else |
| 99 | { |
| 100 | $jsSafe = false; |
| 101 | $interpretBackSlashes = true; |
| 102 | } |
| 103 | |
| 104 | // get language |
| 105 | $lang = JFactory::getLanguage(); |
| 106 | |
| 107 | // translate the string |
| 108 | $t = $lang->_($string, $jsSafe, $interpretBackSlashes); |
| 109 | |
| 110 | // replace custom named placeholders with sprintf style placeholders |
| 111 | $t = preg_replace('/\[\[%([0-9]+):[^\]]*\]\]/', '%\1$s', $t); |
| 112 | |
| 113 | // push the translated string at the beginning of the args list |
| 114 | array_unshift($args, $t); |
| 115 | |
| 116 | // invoke sprintf |
| 117 | return call_user_func_array('sprintf', $args); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Like Text::sprintf but tries to pluralise the string. |
| 122 | * |
| 123 | * Note that this method can take a mixed number of arguments as for the sprintf function. |
| 124 | * |
| 125 | * The last argument can take an array of options: |
| 126 | * |
| 127 | * array('jsSafe'=>boolean, 'interpretBackSlashes'=>boolean, 'script'=>boolean) |
| 128 | * |
| 129 | * where: |
| 130 | * |
| 131 | * jsSafe is a boolean to generate a javascript safe strings. |
| 132 | * interpretBackSlashes is a boolean to interpret backslashes \\->\, \n->new line, \t->tabulation. |
| 133 | * script is a boolean to indicate that the string will be push in the javascript language store. |
| 134 | * |
| 135 | * Examples: |
| 136 | * `<script>alert(Joomla.JText._('<?php echo Text::plural("COM_PLUGINS_N_ITEMS_UNPUBLISHED", 1, array("script"=>true)); ?>'));</script>` |
| 137 | * will generate an alert message containing '1 plugin successfully disabled' |
| 138 | * `<?php echo Text::plural('COM_PLUGINS_N_ITEMS_UNPUBLISHED', 1); ?>` will generate a '1 plugin successfully disabled' string |
| 139 | * |
| 140 | * @param string $string The format string. |
| 141 | * @param integer $n The number of items. |
| 142 | * |
| 143 | * @return string The translated strings or the key if 'script' is true in the array of options. |
| 144 | * |
| 145 | * @since 10.1.19 |
| 146 | */ |
| 147 | public static function plural($string, $n) |
| 148 | { |
| 149 | // obtain arguments list |
| 150 | $args = func_get_args(); |
| 151 | |
| 152 | // evaluate pluralization |
| 153 | switch ((int) $n) |
| 154 | { |
| 155 | case '0': |
| 156 | $string .= '_0'; |
| 157 | break; |
| 158 | |
| 159 | case '1': |
| 160 | $string .= '_1'; |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Use plural string only if supported, otherwise rely on default string. |
| 166 | * |
| 167 | * @since 10.1.35 |
| 168 | */ |
| 169 | if ($string !== $args[0] && JFactory::getLanguage()->hasKey($string)) |
| 170 | { |
| 171 | // replace updated string |
| 172 | $args[0] = $string; |
| 173 | } |
| 174 | |
| 175 | // invoke JText::sprintf() to complete translation |
| 176 | return call_user_func_array(array('JText', 'sprintf'), $args); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Translate a string into the current language and registers it in the JavaScript language store. |
| 181 | * |
| 182 | * @param string $string The Text key. |
| 183 | * @param boolean $jsSafe Ensure the output is JavaScript safe. |
| 184 | * @param boolean $interpretBackSlashes True to interpret backslashes (\\=\, \n=carriage return, \t=tabulation). |
| 185 | * |
| 186 | * @return array The JavaScript language store. |
| 187 | * |
| 188 | * @uses _() |
| 189 | * @uses getScriptStrings() |
| 190 | * |
| 191 | * @since 10.1.14 |
| 192 | */ |
| 193 | public static function script($string, $jsSafe = false, $interpretBackSlashes = true) |
| 194 | { |
| 195 | // normalize the key and translate the string. |
| 196 | static::$strings[strtoupper($string)] = static::_($string, $jsSafe, $interpretBackSlashes); |
| 197 | |
| 198 | // Update Joomla.JText script options |
| 199 | JFactory::getDocument()->addScriptOptions('joomla.jtext', static::$strings, $merge = false); |
| 200 | |
| 201 | return static::getScriptStrings(); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Get the strings that have been loaded to the JavaScript language store. |
| 206 | * |
| 207 | * @return array The JavaScript language store. |
| 208 | * |
| 209 | * @since 10.1.14 |
| 210 | */ |
| 211 | public static function getScriptStrings() |
| 212 | { |
| 213 | return static::$strings; |
| 214 | } |
| 215 | } |
| 216 |