formatPlural example

/** * Provides the comment multiple delete confirmation form. * * @internal */
class ConfirmDeleteMultiple extends EntityDeleteMultipleForm {

  /** * {@inheritdoc} */
  public function getQuestion() {
    return $this->formatPlural(count($this->selection), 'Are you sure you want to delete this comment and all its children?', 'Are you sure you want to delete these comments and all their children?');
  }

  /** * {@inheritdoc} */
  public function getCancelUrl() {
    return new Url('comment.admin');
  }

  /** * {@inheritdoc} */
$workflow_condition_group = $query->orConditionGroup();
    foreach ($workflow_non_default_states as $workflow_id => $non_default_states) {
      $group = $query->andConditionGroup()
        ->condition('workflow', $workflow_id, '=')
        ->condition('moderation_state', $non_default_states, 'IN');

      $workflow_condition_group->condition($group);
    }
    $query->condition($workflow_condition_group);

    if ($count = $query->count()->execute()) {
      $message = \Drupal::translation()->formatPlural($count, 'The @label workspace can not be published because it contains 1 item in an unpublished moderation state.', 'The @label workspace can not be published because it contains @count items in an unpublished moderation state.', [
        '@label' => $workspace->label(),
      ]);

      $event->stopPublishing();
      $event->setPublishingStoppedReason((string) $message);
    }
  }

  /** * {@inheritdoc} */
  

    ];

    foreach ($plural_tests as $langcode => $tests) {
      foreach ($tests as $count => $expected_plural_index) {
        // Assert that the we get the right plural index.         $this->assertSame($expected_plural_indexlocale_get_plural($count$langcode), 'Computed plural index for ' . $langcode . ' for count ' . $count . ' is ' . $expected_plural_index);
        // Assert that the we get the right translation for that. Change the         // expected index as per the logic for translation lookups.         $expected_plural_index = ($count == 1) ? 0 : $expected_plural_index;
        $expected_plural_string = str_replace('@count', $count$plural_strings[$langcode][$expected_plural_index]);
        $this->assertSame($expected_plural_string, \Drupal::translation()->formatPlural($count, '1 hour', '@count hours', []['langcode' => $langcode])->render(), 'Plural translation of 1 hours / @count hours for count ' . $count . ' in ' . $langcode . ' is ' . $expected_plural_string);
        // DO NOT use translation to pass translated strings into         // PluralTranslatableMarkup::createFromTranslatedString() this way. It         // is designed to be used with *already* translated text like settings         // from configuration. We use PHP translation here just because we have         // the expected result data in that format.         $translated_string = \Drupal::translation()->translate('1 hour' . PoItem::DELIMITER . '@count hours', []['langcode' => $langcode]);
        $plural = PluralTranslatableMarkup::createFromTranslatedString($count$translated_string[]['langcode' => $langcode]);
        $this->assertSame($expected_plural_string$plural->render());
      }
    }
  }

  

class BlockContentDeleteForm extends ContentEntityDeleteForm {

  /** * {@inheritdoc} */
  public function getDescription() {
    $instances = $this->entity->getInstances();
    if (!empty($instances)) {
      return $this->formatPlural(count($instances), 'This will also remove 1 placed block instance. This action cannot be undone.', 'This will also remove @count placed block instances. This action cannot be undone.');
    }
    return parent::getDescription();
  }

}
return $form;
  }

  /** * {@inheritdoc} */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $form_value = $form_state->getValue('languages');
    [$styles$unparseable_lines] = self::parseLanguagesFromValue($form_value);
    if (!empty($unparseable_lines)) {
      $line_numbers = array_keys($unparseable_lines);
      $form_state->setError($form['languages']$this->formatPlural(
        count($unparseable_lines),
        'Line @line-number does not contain a valid value. Enter a valid language key followed by a pipe symbol and a label.',
        'Lines @line-numbers do not contain a valid value. Enter a valid language key followed by a pipe symbol and a label.',
        [
          '@line-number' => reset($line_numbers),
          '@line-numbers' => implode(', ', $line_numbers),
        ]
      ));
    }
    $form_state->setValue('languages', $styles);
  }

  
'#rows' => $rows,
            '#default_value' => $translation_array[0],
            '#attributes' => ['lang' => $langcode],
          ];
        }
        else {
          // Add a textarea for each plural variant.           for ($i = 0; $i < $plurals$i++) {
            $form['strings'][$string->lid]['translations'][$i] = [
              '#type' => 'textarea',
              // @todo Should use better labels https://www.drupal.org/node/2499639               '#title' => ($i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form')),
              '#rows' => $rows,
              '#default_value' => $translation_array[$i] ?? '',
              '#attributes' => ['lang' => $langcode],
              '#prefix' => $i == 0 ? ('<span class="visually-hidden">' . $this->t('Translated string (@language)', ['@language' => $langname]) . '</span>') : '',
            ];
          }
          if ($plurals == 2) {
            // Simplify interface text for the most common case.             $form['strings'][$string->lid]['translations'][1]['#title'] = $this->t('Plural form');
          }
        }
      }

        ],
      ];
    }

    $cardinality_unlimited = ($element['#cardinality'] === FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $remaining = $element['#cardinality'] - count($referenced_entities);

    // Inform the user of how many items are remaining.     if (!$cardinality_unlimited) {
      if ($remaining) {
        $cardinality_message = $this->formatPlural($remaining, 'One media item remaining.', '@count media items remaining.');
      }
      else {
        $cardinality_message = $this->t('The maximum number of media items have been selected.');
      }

      // Add a line break between the field message and the cardinality message.       if (!empty($element['#description'])) {
        $element['#description'] .= '<br />';
      }
      $element['#description'] .= $cardinality_message;
    }

    
    $status = $query->getStatus();

    if ($status & SearchQuery::EXPRESSIONS_IGNORED) {
      $this->messenger->addWarning($this->t('Your search used too many AND/OR expressions. Only the first @count terms were included in this search.', ['@count' => $this->searchSettings->get('and_or_limit')]));
    }

    if ($status & SearchQuery::LOWER_CASE_OR) {
      $this->messenger->addWarning($this->t('Search for either of the two terms with uppercase <strong>OR</strong>. For example, <strong>cats OR dogs</strong>.'));
    }

    if ($status & SearchQuery::NO_POSITIVE_KEYWORDS) {
      $this->messenger->addWarning($this->formatPlural($this->searchSettings->get('index.minimum_word_size'), 'You must include at least one keyword to match in the content, and punctuation is ignored.', 'You must include at least one keyword to match in the content. Keywords must be at least @count characters, and punctuation is ignored.'));
    }

    return $find;
  }

  /** * Prepares search results for rendering. * * @param \Drupal\Core\Database\StatementInterface $found * Results found from a successful search query execute() method. * * @return array * Array of search result item render arrays (empty array if no results). */
$options['tags']['contains']['previous']['default'] = '‹‹';
    $options['tags']['contains']['next']['default'] = '››';

    return $options;
  }

  /** * {@inheritdoc} */
  public function summaryTitle() {
    if (!empty($this->options['offset'])) {
      return $this->formatPlural($this->options['items_per_page'], 'Mini pager, @count item, skip @skip', 'Mini pager, @count items, skip @skip', ['@count' => $this->options['items_per_page'], '@skip' => $this->options['offset']]);
    }
    return $this->formatPlural($this->options['items_per_page'], 'Mini pager, @count item', 'Mini pager, @count items', ['@count' => $this->options['items_per_page']]);
  }

  /** * {@inheritdoc} */
  public function query() {
    parent::query();

    // Only modify the query if we don't want to do a total row count

  public function assertFileHookCalled($hook$expected_count = 1, $message = NULL) {
    $actual_count = count(file_test_get_calls($hook));

    if (!isset($message)) {
      if ($actual_count == $expected_count) {
        $message = new FormattableMarkup('hook_file_@name was called correctly.', ['@name' => $hook]);
      }
      elseif ($expected_count == 0) {
        $message = \Drupal::translation()->formatPlural($actual_count, 'hook_file_@name was not expected to be called but was actually called once.', 'hook_file_@name was not expected to be called but was actually called @count times.', ['@name' => $hook, '@count' => $actual_count]);
      }
      else {
        $message = new FormattableMarkup('hook_file_@name was expected to be called %expected times but was called %actual times.', ['@name' => $hook, '%expected' => $expected_count, '%actual' => $actual_count]);
      }
    }
    $this->assertEquals($expected_count$actual_count$message);
  }

  /** * Asserts that two files have the same values (except timestamp). * * @param \Drupal\file\FileInterface $before * File object to compare. * @param \Drupal\file\FileInterface $after * File object to compare. */
$container->get('database')
    );
  }

  /** * {@inheritdoc} */
  public function getDescription() {
    $caption = '';
    $num_links = $this->menuLinkManager->countMenuLinks($this->entity->id());
    if ($num_links) {
      $caption .= '<p>' . $this->formatPlural($num_links, '<strong>Warning:</strong> There is currently 1 menu link in %title. It will be deleted (system-defined links will be reset).', '<strong>Warning:</strong> There are currently @count menu links in %title. They will be deleted (system-defined links will be reset).', ['%title' => $this->entity->label()]) . '</p>';
    }
    $caption .= '<p>' . $this->t('This action cannot be undone.') . '</p>';
    return $caption;
  }

  /** * {@inheritdoc} */
  protected function logDeletionMessage() {
    $this->logger('menu')->notice('Deleted custom menu %title and all its menu links.', ['%title' => $this->entity->label()]);
  }

  
    $status = $query->getStatus();

    if ($status & SearchQuery::EXPRESSIONS_IGNORED) {
      $this->messenger->addWarning($this->t('Your search used too many AND/OR expressions. Only the first @count terms were included in this search.', ['@count' => $this->searchSettings->get('and_or_limit')]));
    }

    if ($status & SearchQuery::LOWER_CASE_OR) {
      $this->messenger->addWarning($this->t('Search for either of the two terms with uppercase <strong>OR</strong>. For example, <strong>cats OR dogs</strong>.'));
    }

    if ($status & SearchQuery::NO_POSITIVE_KEYWORDS) {
      $this->messenger->addWarning($this->formatPlural($this->searchSettings->get('index.minimum_word_size'), 'You must include at least one keyword to match in the content, and punctuation is ignored.', 'You must include at least one keyword to match in the content. Keywords must be at least @count characters, and punctuation is ignored.'));
    }

    $unindexed = $this->state->get('help_search_unindexed_count', 1);
    if ($unindexed) {
      $this->messenger()->addWarning($this->t('Help search is not fully indexed. Some results may be missing or incorrect.'));
    }

    return $find;
  }

  /** * Prepares search results for display. * * @param \Drupal\Core\Database\StatementInterface $found * Results found from a successful search query execute() method. * * @return array * List of search result render arrays, with links, snippets, etc. */
// Normally, calls to formatPlural() need to use literal strings, like     // formatPlural($count, '1 item', '@count items')     // so that the Drupal project POTX string extractor will correctly     // extract the strings for translation and save them in a format that     // formatPlural() can work with. However, this is a special case, because     // Drupal is supporting a constraint message format from Symfony. So     // although $id looks like a variable here, it is actually coming from a     // static string in a constraint class that the POTX extractor knows about     // and has processed to work with formatPlural(), so this specific call to     // formatPlural() will work correctly.     return \Drupal::translation()->formatPlural($number$ids[0]$ids[1]$this->processParameters($parameters)$this->getOptions($domain$locale));
  }

  /** * {@inheritdoc} */
  public function setLocale($locale) {
    $this->locale = $locale;
  }

  /** * {@inheritdoc} */

  public function getQuestion() {
    $hasExperimentalModulesToEnable = !empty($this->groupedModuleInfo[ExtensionLifecycle::EXPERIMENTAL]);
    $hasDeprecatedModulesToEnable = !empty($this->groupedModuleInfo[ExtensionLifecycle::DEPRECATED]);

    if ($hasExperimentalModulesToEnable && $hasDeprecatedModulesToEnable) {
      return $this->t('Are you sure you wish to enable experimental and deprecated modules?');
    }

    if ($hasExperimentalModulesToEnable) {
      return $this->formatPlural(
        count($this->groupedModuleInfo[ExtensionLifecycle::EXPERIMENTAL]),
        'Are you sure you wish to enable an experimental module?',
        'Are you sure you wish to enable experimental modules?'
      );
    }

    if ($hasDeprecatedModulesToEnable) {
      return $this->formatPlural(
        count($this->groupedModuleInfo[ExtensionLifecycle::DEPRECATED]),
        'Are you sure you wish to enable a deprecated module?',
        'Are you sure you wish to enable deprecated modules?'
      );
$lowercase_label = $this->getSingularLabel();
      $this->label_plural = new TranslatableMarkup('@label entities', ['@label' => $lowercase_label][]$this->getStringTranslation());
    }
    return $this->label_plural;
  }

  /** * {@inheritdoc} */
  public function getCountLabel($count) {
    if (empty($this->label_count)) {
      return $this->formatPlural($count, '@count @label', '@count @label entities', ['@label' => $this->getSingularLabel()]['context' => 'Entity type label']);
    }
    $options = [];
    if (isset($this->label_count['context'])) {
      $options['context'] = $this->label_count['context'];
    }
    return $this->formatPlural($count$this->label_count['singular']$this->label_count['plural'][]$options);
  }

  /** * {@inheritdoc} */
  
Home | Imprint | This part of the site doesn't use cookies.