str_word_count example

/** * Defines the textarea element for the configuration translation interface. */
class Textarea extends FormElementBase {

  /** * {@inheritdoc} */
  public function getTranslationElement(LanguageInterface $translation_language$source_config$translation_config) {
    // Estimate a comfortable size of the input textarea.     if (is_string($translation_config)) {
      $rows_words = ceil(str_word_count($translation_config) / 5);
      $rows_newlines = substr_count($translation_config, "\n") + 1;
      $rows = max($rows_words$rows_newlines);
    }

    return [
      '#type' => 'textarea',
      '#rows' => $rows ?? 1,
    ] + parent::getTranslationElement($translation_language$source_config$translation_config);
  }

}
if (!empty($string->context)) {
          $form['strings'][$string->lid]['original'][] = [
            '#type' => 'inline_template',
            '#template' => '<br><small>{{ context_title }}: <span lang="en">{{ context }}</span></small>',
            '#context' => [
              'context_title' => $this->t('In Context'),
              'context' => $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 (!$plural) {
          $form['strings'][$string->lid]['translations'][0] = [
            '#type' => 'textarea',
            '#title' => $this->t('Translated string (@language)', ['@language' => $langname]),
            '#title_display' => 'invisible',
            '#rows' => $rows,
            '#default_value' => $translation_array[0],
            '#attributes' => ['lang' => $langcode],
          ];
        }
        else {
          
Home | Imprint | This part of the site doesn't use cookies.