You are here

function i18n_string_translate_page_form_strings in Internationalization 7

Create field elements for strings

Parameters

i18n_string_object[] $strings:

string $langcode:

Return value

array

1 call to i18n_string_translate_page_form_strings()
i18n_string_translate_page_form in i18n_string/i18n_string.pages.inc
Form builder callback for in-place string translation.

File

i18n_string/i18n_string.pages.inc, line 179
Internationalization (i18n) package - translatable strings reusable admin UI.

Code

function i18n_string_translate_page_form_strings($strings, $langcode) {
  global $user;
  $form = array();
  foreach ($strings as $item) {

    // Check permissions to translate this string, depends on format, etc..
    if ($message = $item
      ->check_translate_access()) {

      // We'll display a disabled element with the reason it cannot be translated.
      $disabled = TRUE;
      $description = $message;
    }
    else {
      $disabled = FALSE;
      $description = '';

      // If we don't have a source and it can be translated, we create it.
      if (!$item
        ->get_source()) {

        // Enable messages just as a reminder these strings are not being updated properly.
        $status = $item
          ->update(array(
          'messages' => TRUE,
        ));
        if ($status === FALSE || $status === SAVED_DELETED) {

          // We don't have a source string so nothing to translate here
          $disabled = TRUE;
        }
      }
    }
    $default_value = $item
      ->format_translation($langcode, array(
      'langcode' => $langcode,
      'sanitize' => FALSE,
      'debug' => FALSE,
    ));
    $available_formats = array_keys(filter_formats($user));
    if (!in_array($item->format, $available_formats)) {
      $item->format = NULL;
    }
    $form[$item
      ->get_name()] = array(
      '#title' => $item
        ->get_title(),
      '#type' => $item->format ? 'text_format' : 'textarea',
      '#default_value' => $default_value,
      '#format' => $item->format,
      // This will trigger i18n_string_pre_render_text_format() to actually
      // alter the element.
      '#i18n_string_is_translation' => TRUE,
      '#disabled' => $disabled,
      '#description' => $description,
      // If disabled, provide smaller textarea (that can be expanded anyway).
      '#rows' => $disabled ? 1 : min(ceil(str_word_count($default_value) / 12), 10),
      // Change the parent for disabled strings so we don't get empty values later
      '#parents' => array(
        $disabled ? 'disabled_strings' : 'strings',
        $item
          ->get_name(),
      ),
    );
  }
  return $form;
}