You are here

function _webform_localization_translate_component in Webform Localization 7.4

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

Translates the component properties that are translatable.

These are found in under 'translated_strings' in the 'extra' array of the component, which is build when the component is inserted / updated, or when all webform strings are updated from admin/config/regional/translate/i18n_string.

Parameters

array $element: The FAPI renderable array of the component instance.

array $component: The component.

2 calls to _webform_localization_translate_component()
webform_localization_webform_component_display_alter in ./webform_localization.module
Implements hook_webform_component_display_alter().
webform_localization_webform_component_render_alter in ./webform_localization.module
Implements hook_webform_component_render_alter().

File

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

Code

function _webform_localization_translate_component(&$element, $component) {
  if (isset($component['extra']['translated_strings']) && is_array($component['extra']['translated_strings'])) {
    $node = !empty($component['nid']) ? node_load($component['nid']) : NULL;
    foreach ($component['extra']['translated_strings'] as $name) {
      $name_list = explode(':', $name);
      $current_element =& $element;
      $current_element_format;
      if (isset($current_element['#format'])) {
        $current_element_format = $current_element['#format'];
      }
      else {
        $current_element_format = I18N_STRING_FILTER_XSS_ADMIN;
      }
      if (strpos($name_list[3], '[') !== FALSE) {

        // The property is deeper in the renderable array, we must extract the
        // the place where it is.
        list($children, $property) = explode(']#', $name_list[3]);

        // Remove the '[' from the begining of the string.
        $children = drupal_substr($children, 1);
        $children_array = explode('][', $children);
        foreach ($children_array as $child) {
          if (isset($current_element[$child])) {
            $current_element =& $current_element[$child];
          }
          else {
            continue;
          }
        }
      }
      else {

        // Remove the '#' from the begining of the property, for consistency.
        $property = drupal_substr($name_list[3], 1);
      }
      if (strpos($property, '-') !== FALSE) {

        // If property is array, we extract the key from the property.
        list($property, $key) = explode('-', $property, 2);
        if (isset($current_element['#' . $property][$key])) {
          $text = i18n_string($name, $current_element['#' . $property][$key], array(
            'format' => I18N_STRING_FILTER_XSS,
          ));
          if (module_exists('token')) {
            $current_element['#' . $property][$key] = webform_replace_tokens($text, $node);
          }
          else {
            $current_element['#' . $property][$key] = $text;
          }
        }
      }
      else {

        // If we are dealing with option groups.
        if (isset($name_list[4]) && strpos($name_list[4], '/-') !== FALSE) {
          $option_group = str_replace('/-', '', $name_list[4]);

          // If it's a element.
          if (isset($name_list[5])) {
            $text = i18n_string($name, $current_element['#' . $property][$option_group][$name_list[5]], array(
              'format' => $current_element_format,
            ));
            if (module_exists('token')) {
              $current_element['#' . $property][$option_group][$name_list[5]] = webform_replace_tokens($text, $node);
            }
            else {
              $current_element['#' . $property][$option_group][$name_list[5]] = $text;
            }
          }
          else {

            // If it's a option group we translate the key.
            $text = i18n_string($name, $option_group, array(
              'format' => $current_element_format,
            ));
            if (module_exists('token')) {
              $translated_option_group = webform_replace_tokens($text, $node);
            }
            else {
              $translated_option_group = $text;
            }
            if ($translated_option_group != $option_group) {
              _webform_localization_array_key_replace($current_element['#' . $property], $option_group, $translated_option_group);
            }
          }
        }
        else {

          // Else we can treat the property as string.
          if (isset($current_element['#' . $property])) {
            if ($property == 'markup' && $current_element['#type'] == 'markup') {
              $text = i18n_string($name, $current_element['#' . $property], array(
                'format' => $current_element['#format'],
              ));
            }
            elseif ($property == 'description') {
              $text = i18n_string($name, $current_element['#' . $property], array(
                'sanitize' => FALSE,
              ));
            }
            else {
              $text = i18n_string($name, $current_element['#' . $property], array(
                'sanitize' => FALSE,
              ));
            }
            $current_element['#' . $property] = $text;
          }
        }
      }
    }
  }
}