| 24 |
* Helper function for iterating and processing forms. |
* Helper function for iterating and processing forms. |
| 25 |
*/ |
*/ |
| 26 |
function _civicrm_localize_alter(&$element) { |
function _civicrm_localize_alter(&$element) { |
| 27 |
// Match the text inside label tags. |
// Match label, description table cell, and select tags. |
| 28 |
$regex = '|(<label.*?>)(.*?)(<\/.*?>)|'; |
$regex_1 = '/(<label.*?>)(.*?)(<\/label>)/s'; |
| 29 |
|
$regex_2 = '/(<td class="description">)(.*?)(<\/td>)/s'; |
| 30 |
|
$regex_3 = '/(<select.*?>)(.*?)(<\/select>)/s'; |
| 31 |
foreach (element_children($element) as $key) { |
foreach (element_children($element) as $key) { |
| 32 |
// CiviCRM elements have no '#type' set. Assume if there is no '#type' that this |
// CiviCRM elements have no '#type' set. Assume if there is no '#type' that this |
| 33 |
// is a field we need to alter. |
// is a field we need to alter. |
| 34 |
if (isset($element[$key]['#value']) && !isset($element[$key]['#type'])) { |
if (isset($element[$key]['#value']) && !isset($element[$key]['#type'])) { |
| 35 |
$element[$key]['#value'] = preg_replace_callback($regex, '_civicrm_localize_replace', $element[$key]['#value']); |
$element[$key]['#value'] = preg_replace_callback($regex_1, '_civicrm_localize_replace', $element[$key]['#value']); |
| 36 |
|
$element[$key]['#value'] = preg_replace_callback($regex_2, '_civicrm_localize_replace', $element[$key]['#value']); |
| 37 |
|
$element[$key]['#value'] = preg_replace_callback($regex_3, '_civicrm_localize_select_replace', $element[$key]['#value']); |
| 38 |
} |
} |
| 39 |
} |
} |
| 40 |
} |
} |
| 41 |
|
|
| 42 |
/** |
/** |
| 43 |
* Helper function for localization. |
* General purpose helper function for localization. |
| 44 |
*/ |
*/ |
| 45 |
function _civicrm_localize_replace($matches) { |
function _civicrm_localize_replace($matches) { |
| 46 |
return $matches[1] . t($matches[2]) . $matches[3]; |
// If a field is required, its label will have a span. |
| 47 |
|
// Detect and process this so the label is available for localization |
| 48 |
|
// without the span element. |
| 49 |
|
$regex = '/(.*?)(<span class="marker".*?>)(.*?)(<\/span>)/s'; |
| 50 |
|
if (preg_match($regex, $matches[2], $out)) { |
| 51 |
|
// Localize title attribute text. |
| 52 |
|
$out[2] = str_replace('This field is required.', t('This field is required.'), $out[2]); |
| 53 |
|
$matches[2] = t($out[1]) .' '. $out[2] . $out[3]. $out[4]; |
| 54 |
|
} |
| 55 |
|
else { |
| 56 |
|
$matches[2] = t($matches[2]); |
| 57 |
|
} |
| 58 |
|
return $matches[1] . $matches[2] . $matches[3]; |
| 59 |
|
} |
| 60 |
|
|
| 61 |
|
/** |
| 62 |
|
* Helper function for localization of selects. |
| 63 |
|
*/ |
| 64 |
|
function _civicrm_localize_select_replace($matches) { |
| 65 |
|
$options = array(); |
| 66 |
|
$out = array(); |
| 67 |
|
// Match options within the select element. |
| 68 |
|
$regex = '/(<option.*?>)(.*?)(<\/option>)/s'; |
| 69 |
|
preg_match_all($regex, $matches[2], $out, PREG_SET_ORDER); |
| 70 |
|
foreach($out as $match) { |
| 71 |
|
$options[t($match[2])] = $match[1] . t($match[2]) . $match[3]; |
| 72 |
|
} |
| 73 |
|
// Sort the options alphabetically by their new localized strings. |
| 74 |
|
// The first is, e.g., '- select -'. Remove it temporarily. |
| 75 |
|
$first = array_shift($options); |
| 76 |
|
ksort($options); |
| 77 |
|
// Add the first item back to the sorted list. |
| 78 |
|
array_unshift($options, $first); |
| 79 |
|
return $matches[1] . implode("\n", $options) . $matches[3]; |
| 80 |
} |
} |