You are here

function webform_localization_form_i18n_string_locale_translate_edit_form_alter in Webform Localization 7.4

Implements hook_form_FORM_ID_alter().

Add related translations to translation form.

File

./webform_localization.module, line 1128
Webform localization module.

Code

function webform_localization_form_i18n_string_locale_translate_edit_form_alter(&$form, &$form_state, $form_id) {
  if ($form['textgroup']['#value'] == 'webform') {
    if (isset($form['context']['#markup']) && preg_match('/#title/', $form['context']['#markup'])) {
      list($id, $cid, $name) = explode(':', $form['context']['#markup']);
      $form['#submit'][] = 'webform_localization_i18n_string_locale_translate_edit_form_submit';
      $rel_key = 'webform:' . $id . ':' . $cid . '%';
      $related = db_query('SELECT lid, source, context, textgroup, location FROM {locales_source} WHERE location LIKE :key', array(
        ':key' => $rel_key,
      ));
      $langs_installed = language_list();
      $langs_enabled = language_list('enabled');
      $langs_enabled = $langs_enabled[1];
      if (isset($form['translations'])) {
        foreach ($form['translations'] as $key => $value) {
          if (array_key_exists($key, $langs_installed) && !array_key_exists($key, $langs_enabled)) {
            unset($form['translations'][$key]);
          }
        }
      }
      $form['items'] = array(
        '#type' => 'fieldset',
        '#title' => t('Related strings'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#weight' => 2,
      );
      foreach ($related as $source) {
        if ($source->context != $form['context']['#markup']) {
          $lid = $source->lid;

          // Add original text to the top and some values for form altering.
          $form['items'][$lid]['original'] = array(
            '#type' => 'item',
            '#title' => t('Original text'),
            '#markup' => check_plain(wordwrap($source->source, 0)),
          );
          if (!empty($source->context)) {
            $form['items'][$lid]['context'] = array(
              '#type' => 'item',
              '#title' => t('Context'),
              '#markup' => check_plain($source->context),
            );
          }
          $form['items'][$lid]['textgroup'] = array(
            '#type' => 'value',
            '#value' => $source->textgroup,
          );
          $form['items'][$lid]['location'] = array(
            '#type' => 'value',
            '#value' => $source->location,
          );
          $languages = $langs_enabled;

          // We don't need the default language value, that value is in $source.
          $omit = $source->textgroup == 'default' ? 'en' : i18n_string_source_language();
          unset($languages[$omit]);
          $form['items'][$lid]['translations-' . $lid] = array(
            '#tree' => TRUE,
          );

          // Approximate the number of rows to use in the default textarea.
          $rows = min(ceil(str_word_count($source->source) / 12), 10);
          foreach ($languages as $langcode => $language) {
            $form['items'][$lid]['translations-' . $lid][$langcode] = array(
              '#type' => 'textarea',
              '#title' => t($language->name),
              '#rows' => $rows,
              '#default_value' => '',
            );
          }

          // Fetch translations and fill in default values in the form.
          $result = db_query("SELECT DISTINCT translation, language FROM {locales_target} WHERE lid = :lid AND language <> :omit", array(
            ':lid' => $lid,
            ':omit' => $omit,
          ));
          foreach ($result as $translation) {
            $form['items'][$lid]['translations-' . $lid][$translation->language]['#default_value'] = $translation->translation;
          }
        }
      }
    }
  }
}