function locale_translate_edit_form

Form constructor for the string editing form.

See also

locale_menu()

locale_translate_edit_form_validate()

locale_translate_edit_form_submit()

Related topics

1 string reference to 'locale_translate_edit_form'
locale_translate_page in drupal/core/modules/locale/locale.pages.inc
Page callback: Shows the string search screen.

File

drupal/core/modules/locale/locale.pages.inc, line 253
Interface translation summary, editing and deletion user interfaces.

Code

function locale_translate_edit_form($form, &$form_state) {
  $filter_values = locale_translate_filter_values();
  $langcode = $filter_values['langcode'];
  drupal_static_reset('language_list');
  $languages = language_list();
  $langname = isset($langcode) ? $languages[$langcode]->name : "- None -";
  $path = drupal_get_path('module', 'locale');
  $form['#attached']['css'] = array(
    $path . '/css/locale.admin.css',
  );
  $form['#attached']['library'][] = array(
    'locale',
    'drupal.locale.admin',
  );
  $form['langcode'] = array(
    '#type' => 'value',
    '#value' => $filter_values['langcode'],
  );
  $form['strings'] = array(
    '#type' => 'item',
    '#tree' => TRUE,
    '#language' => $langname,
    '#theme' => 'locale_translate_edit_form_strings',
  );
  if (isset($langcode)) {
    $strings = locale_translate_filter_load_strings();
    $plural_formulas = Drupal::state()
      ->get('locale.translation.plurals') ?: array();
    foreach ($strings as $string) {

      // Cast into source string, will do for our purposes.
      $source = new SourceString($string);

      // Split source to work with plural values.
      $source_array = $source
        ->getPlurals();
      $translation_array = $string
        ->getPlurals();
      if (count($source_array) == 1) {

        // Add original string value and mark as non-plural.
        $form['strings'][$string->lid]['plural'] = array(
          '#type' => 'value',
          '#value' => 0,
        );
        $form['strings'][$string->lid]['original'] = array(
          '#type' => 'item',
          '#title' => t('Source string'),
          '#title_display' => 'invisible',
          '#markup' => check_plain($source_array[0]),
        );
      }
      else {

        // Add original string value and mark as plural.
        $form['strings'][$string->lid]['plural'] = array(
          '#type' => 'value',
          '#value' => 1,
        );
        $form['strings'][$string->lid]['original_singular'] = array(
          '#type' => 'item',
          '#title' => t('Singular form'),
          '#markup' => check_plain($source_array[0]),
        );
        $form['strings'][$string->lid]['original_plural'] = array(
          '#type' => 'item',
          '#title' => t('Plural form'),
          '#markup' => check_plain($source_array[1]),
        );
      }
      if (!empty($string->context)) {
        $form['strings'][$string->lid]['context'] = array(
          '#type' => 'value',
          '#value' => check_plain($string->context),
        );
      }

      // Approximate the number of rows to use in the default textarea.
      $rows = min(ceil(str_word_count($source_array[0]) / 12), 10);
      if (empty($form['strings'][$string->lid]['plural']['#value'])) {
        $form['strings'][$string->lid]['translations'][0] = array(
          '#type' => 'textarea',
          '#title' => t('Translated string'),
          '#title_display' => 'invisible',
          '#rows' => $rows,
          '#default_value' => $translation_array[0],
        );
      }
      else {

        // Dealing with plural strings.
        if (isset($plural_formulas[$langcode]['plurals']) && $plural_formulas[$langcode]['plurals'] > 2) {

          // Add a textarea for each plural variant.
          for ($i = 0; $i < $plural_formulas[$langcode]['plurals']; $i++) {
            $form['strings'][$string->lid]['translations'][$i] = array(
              '#type' => 'textarea',
              '#title' => $i == 0 ? t('Singular form') : format_plural($i, 'First plural form', '@count. plural form'),
              '#rows' => $rows,
              '#default_value' => isset($translation_array[$i]) ? $translation_array[$i] : '',
            );
          }
        }
        else {

          // Fallback for unknown number of plurals.
          $form['strings'][$string->lid]['translations'][0] = array(
            '#type' => 'textarea',
            '#title' => t('Singular form'),
            '#rows' => $rows,
            '#default_value' => $translation_array[0],
          );
          $form['strings'][$string->lid]['translations'][1] = array(
            '#type' => 'textarea',
            '#title' => t('Plural form'),
            '#rows' => $rows,
            '#default_value' => isset($translation_array[1]) ? $translation_array[1] : '',
          );
        }
      }
    }
    if (count(element_children($form['strings']))) {
      $form['actions'] = array(
        '#type' => 'actions',
      );
      $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save translations'),
      );
    }
  }
  return $form;
}