Skip to content
Snippets Groups Projects
Commit 006bb1dc authored by Shibin Das's avatar Shibin Das
Browse files

Issue #3495995: Plugin Manager for interacting with Plural Form Rules

parent 8d542900
Branches
Tags
No related merge requests found
<?php
namespace Drupal\string_plural_form\Plugin\StringPluralForm;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\string_plural_form\Attribute\StringPluralForm;
use Drupal\string_plural_form\StringPluralFormBase;
/**
* Provides a custom block plugin.
*/
#[StringPluralForm(
id: "two_forms_singular_for_one_only",
admin_label: new TranslatableMarkup("Two forms, singular used for one only"),
description: new TranslatableMarkup("This is the form used in most existing programs since it is what English is using."),
)]
class TwoFormsSingularForOneOnly extends StringPluralFormBase {
/**
* {@inheritdoc}
*/
public function getGettextNplurals(): int {
return 2;
}
/**
* {@inheritdoc}
*/
public function getGettextPlural(): string {
return "n != 1";
}
/**
* {@inheritdoc}
*/
public function getAssociatedLanguageCodes(): array {
return [
"en",
"de",
"nl",
"sv",
"da",
"no",
"nb",
"nn",
"fo",
"es",
"pt",
"it",
"el",
"bg",
"fi",
"et",
"hu",
"he",
"id",
"eo",
"tr",
];
}
/**
* {@inheritdoc}
*/
public function getIndex(int $n): int {
return ($n != 1);
}
/**
* {@inheritdoc}
*/
public function getIndexLabel(int $index): TranslatableMarkup {
return match($index) {
0 => new TranslatableMarkup("Singular"),
1 => new TranslatableMarkup("Zero, Plural"),
};
}
}
<?php
namespace Drupal\string_plural_form;
/**
* Base Class for StringPluralForm Plugin implementation.
*/
abstract class StringPluralFormBase implements StringPluralFormPluginInterface {
/**
* {@inheritdoc}
*/
public function generateLocalePluralIndex(?int $max = 199): array {
$formula = [];
$max_plural_form_index = $this->getGettextNplurals() - 1;
$formula['default'] = $max_plural_form_index;
for ($count = 0; $count <= $max; $count++) {
if ($this->getIndex($count) != $max_plural_form_index) {
$formula[$count] = $this->getIndex($count);
}
}
return $formula;
}
}
<?php
namespace Drupal\string_plural_form;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* Plugin manager for "string_plural_form".
*/
class StringPluralFormManager extends DefaultPluginManager {
public function __construct(
\Traversable $namespaces,
CacheBackendInterface $cache_backend,
ModuleHandlerInterface $module_handler,
) {
parent::__construct(
'Plugin/StringPluralForm',
$namespaces,
$module_handler,
'Drupal\string_plural_form\StringPluralFormPluginInterface',
'Drupal\string_plural_form\Attribute\StringPluralForm'
);
$this->alterInfo($this->getType());
$this->setCacheBackend($cache_backend, 'string_plural_form_plugins');
}
/**
* {@inheritdoc}
*/
protected function getType() {
return 'string_plural_form';
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment