You are here

function _webform_localization_component_translation_parse in Webform Localization 7.4

Same name and namespace in other branches
  1. 7 includes/webform_localization.i18n.inc \_webform_localization_component_translation_parse()

Parse a component renderable array to find the translatable properties.

Create / update or remove translation source for translatable properties of a webform component.

Parameters

array $element: The renderable array to be parsed.

array $component: The component which was rendered.

Return value

array An array of translatabled webform properties.

1 call to _webform_localization_component_translation_parse()
webform_localization_component_update_translation_strings in includes/webform_localization.i18n.inc
Update / create translation source for all the translatable properties.

File

includes/webform_localization.i18n.inc, line 158
Webform Localization i18n_string integration.

Code

function _webform_localization_component_translation_parse($element, $component) {
  $translated_properies = array();
  if (!isset($element['#parents'])) {
    $element['#parents'] = array();
  }
  $element['#translatable'][] = 'placeholder';
  if (isset($element['#translatable']) && is_array($element['#translatable'])) {
    foreach ($element['#translatable'] as $key) {
      if (!empty($element['#' . $key]) || !empty($element['#attributes'][$key])) {
        if (isset($element['#parents']) && count($element['#parents'])) {
          $property = '[' . implode('][', $element['#parents']) . ']#' . $key;
        }
        else {
          $property = '#' . $key;
        }
        $element_key = '';
        if ($key == 'placeholder' && !empty($element['#attributes']['placeholder'])) {
          $element_key = $element['#attributes']['placeholder'];
        }
        elseif ($key != 'placeholder' && !empty($element['#' . $key])) {
          $element_key = $element['#' . $key];
        }
        if (is_array($element_key)) {

          // If the translatable property is an array, we translate the
          // children.
          foreach ($element_key as $elem_key => $elem_value) {

            // If the child if an array, we translate the elements.
            if (is_array($elem_value)) {
              foreach ($elem_value as $k => $v) {
                $name = webform_localization_i18n_string_name($component['nid'], $component['cid'], $property, '/-' . $elem_key . '/-', $k);
                $translated_properies[] = $name;
                i18n_string($name, $v, array(
                  'update' => TRUE,
                ));
              }
              $name = webform_localization_i18n_string_name($component['nid'], $component['cid'], $property, '/-' . $elem_key . '/-');
              $translated_properies[] = $name;
              i18n_string($name, $elem_key, array(
                'update' => TRUE,
              ));
            }
            else {

              // If the child is not an array.
              $name = webform_localization_i18n_string_name($component['nid'], $component['cid'], $property . '-' . $elem_key);
              $translated_properies[] = $name;
              i18n_string($name, $elem_value, array(
                'update' => TRUE,
              ));
            }
          }
        }
        else {

          // If the translatable property is not an array,
          // it can be treated as a string.
          $name = webform_localization_i18n_string_name($component['nid'], $component['cid'], $property);
          $translated_properies[] = $name;
          i18n_string($name, $element_key, array(
            'update' => TRUE,
          ));
        }
      }
    }
  }

  // Recursevly call the function on the children, after adding the children
  // name to its #parents array.
  foreach (element_children($element) as $child) {
    $element[$child]['#parents'] = $element['#parents'];
    $element[$child]['#parents'][] = $child;

    // Add the translated propertied to the list.
    $translated_properies = array_merge($translated_properies, _webform_localization_component_translation_parse($element[$child], $component));
  }
  return $translated_properies;
}