diff options
| author | Moritz Strohm <strohm@data-quest.de> | 2024-05-03 14:06:50 +0000 |
|---|---|---|
| committer | Moritz Strohm <strohm@data-quest.de> | 2024-05-03 14:06:50 +0000 |
| commit | a54b6f395dbef03e679357e9e93891d4b4749ba1 (patch) | |
| tree | 71e6a004fb4924add05ec0ff6ac911d8b9209d1f /lib/functions.php | |
| parent | d066e3494ba0b661d149dcd7bb267aed8f4e65ea (diff) | |
functions.php: added studip_interpolate, closes #3555
Closes #3555
Merge request studip/studip!2442
Diffstat (limited to 'lib/functions.php')
| -rw-r--r-- | lib/functions.php | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/functions.php b/lib/functions.php index 90d4499..b68658a 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -1859,3 +1859,34 @@ function xml_escape($string) $string = preg_replace('/[\x00-\x08\x0b\x0c\x0e-\x1f]/', '', $string); return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); } + +/** + * This function mimics the functionality of the $gettextInterpolate function in JavaScript. + * This makes it easier to format text in translatable strings. + * + * Note that the behavior of this function is simplified in comparison with $gettextInterpolate: + * - All placeholders that have a value are replaced with the string value of that value. + * Numbers must be pre-formatted before added to the parameters. + * - All placeholders that have no replacements in the parameters array are output. + * + * @param string $gettext_string The translation string to be interpolated. + * + * @param array $parameters The parameters that replace the placeholders in the translation string. + * Array keys are the names of the placeholders while array values are the values that are + * placed inside the string. + * + * @return string The interpolated translation string. + */ +function studip_interpolate(string $gettext_string, array $parameters) : string +{ + return preg_replace_callback( + '/%\{\s*(\w+)\s*\}/', + function ($match) use ($parameters): string { + if (!isset($parameters[$match[1]])) { + throw new Exception('The parameter for the placeholder ' . $match[1] . ' is missing.'); + } + return $parameters[$match[1]]; + }, + $gettext_string + ); +} |
