getNumberOfPlurals example

use Drupal\language\Config\LanguageConfigOverride;

/** * Defines form elements for plurals in configuration translation. */
class PluralVariants extends FormElementBase {

  /** * {@inheritdoc} */
  protected function getSourceElement(LanguageInterface $source_language$source_config) {
    $plurals = $this->getNumberOfPlurals($source_language->getId());
    $values = explode(PoItem::DELIMITER, $source_config);
    $element = [
      '#type' => 'fieldset',
      '#title' => new FormattableMarkup('@label <span class="visually-hidden">(@source_language)</span>', [
        // Labels originate from configuration schema and are translatable.         '@label' => $this->t($this->definition->getLabel()),
        '@source_language' => $source_language->getName(),
      ]),
      '#tree' => TRUE,
    ];
    for ($i = 0; $i < $plurals$i++) {
      
'#header' => [
        $this->t('Source string'),
        $this->t('Translation for @language', ['@language' => $langname]),
      ],
      '#empty' => $this->t('No strings available.'),
      '#attributes' => ['class' => ['locale-translate-edit-table']],
    ];

    if (isset($langcode)) {
      $strings = $this->translateFilterLoadStrings();

      $plurals = $this->getNumberOfPlurals($langcode);

      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.           $plural = FALSE;
          $form['strings'][$string->lid]['original'] = [
            
protected function formatPlural($count$singular$plural, array $args = [], array $options = []) {
    return new PluralTranslatableMarkup($count$singular$plural$args$options$this->getStringTranslation());
  }

  /** * Returns the number of plurals supported by a given language. * * @see \Drupal\locale\PluralFormulaInterface::getNumberOfPlurals() */
  protected function getNumberOfPlurals($langcode = NULL) {
    if (\Drupal::hasService('locale.plural.formula')) {
      return \Drupal::service('locale.plural.formula')->getNumberOfPlurals($langcode);
    }
    // We assume 2 plurals if Locale's services are not available.     return 2;
  }

  /** * Gets the string translation service. * * @return \Drupal\Core\StringTranslation\TranslationInterface * The string translation service. */
  
'#type' => 'checkbox',
      '#title' => $this->t('Format plural'),
      '#description' => $this->t('If checked, special handling will be used for plurality.'),
      '#default_value' => $this->options['format_plural'],
    ];
    $form['format_plural_string'] = [
      '#type' => 'value',
      '#default_value' => $this->options['format_plural_string'],
    ];

    $plural_array = explode(PoItem::DELIMITER, $this->options['format_plural_string']);
    $plurals = $this->getNumberOfPlurals($this->view->storage->get('langcode'));
    for ($i = 0; $i < $plurals$i++) {
      $form['format_plural_values'][$i] = [
        '#type' => 'textfield',
        // @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')),
        '#default_value' => $plural_array[$i] ?? '',
        '#description' => $this->t('Text to use for this variant, @count will be replaced with the value.'),
        '#states' => [
          'visible' => [
            ':input[name="options[format_plural]"]' => ['checked' => TRUE],
          ],
        ],
    $this->importPoFile($this->getPoFile()[
      'langcode' => 'fr',
    ]);
    $this->config('locale.settings');
    // The import should automatically create the corresponding language.     $this->assertSession()->pageTextContains("The language French has been created.");

    // The import should have created 8 strings.     $this->assertSession()->pageTextContains("One translation file imported. 8 translations were added, 0 translations were updated and 0 translations were removed.");

    // This import should have saved plural forms to have 2 variants.     $locale_plurals = \Drupal::service('locale.plural.formula')->getNumberOfPlurals('fr');
    $this->assertEquals(2, $locale_plurals, 'Plural number initialized.');

    // Ensure we were redirected correctly.     $this->assertSession()->addressEquals(Url::fromRoute('locale.translate_page'));

    // Try importing a .po file with invalid tags.     $this->importPoFile($this->getBadPoFile()[
      'langcode' => 'fr',
    ]);

    // The import should have created 1 string and rejected 2.
Home | Imprint | This part of the site doesn't use cookies.