Merge pull request #15818 from colemanw/fields
[civicrm-core.git] / CRM / Core / Smarty / plugins / block.localize.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * Smarty block function for multilingualizing upgrade SQL queries.
22 * The string passed in $text is repeated locale-number times, with the
23 * param field (if provided) appended with a different locale every time.
24 *
25 * @param array $params
26 * Template call's parameters.
27 * @param string $text
28 * {ts} block contents from the template.
29 * @param CRM_Core_Smarty $smarty
30 * The Smarty object.
31 *
32 * @return string
33 * multilingualized query
34 */
35 function smarty_block_localize($params, $text, &$smarty) {
36 if (!$smarty->_tpl_vars['multilingual']) {
37 return $text;
38 }
39
40 $lines = [];
41 foreach ($smarty->_tpl_vars['locales'] as $locale) {
42 $line = $text;
43 if ($params['field']) {
44 $fields = explode(',', $params['field']);
45 foreach ($fields as $field) {
46 $field = trim($field);
47 $line = preg_replace('/\b' . preg_quote($field) . '\b/', "{$field}_{$locale}", $line);
48 }
49 }
50 $lines[] = $line;
51 }
52
53 return implode(', ', $lines);
54 }