Last active
January 5, 2024 16:56
-
-
Save tasiot/b85aa195c622f4c15c095a2cae8ccfc2 to your computer and use it in GitHub Desktop.
Revisions
-
tasiot revised this gist
May 25, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,7 @@ public function pluralize(int $count, string $singular, string $plural, string $ if ($count > 1){ return str_replace('{}', $count, $plural); } else if ($count <= 0 && null !== $zero){ return $zero; // No string replacement required for zero } return str_replace('{}', $count, $singular); } -
tasiot created this gist
May 9, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ <?php namespace App\Twig; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; class AppExtension extends AbstractExtension { public function getFilters(): array { return [ new TwigFilter('pluralize', [$this, 'pluralize']) ]; } public function pluralize(int $count, string $singular, string $plural, string $zero = null): string { if ($count > 1){ return str_replace('{}', $count, $plural); } else if ($count <= 0 && null !== $zero){ return str_replace('{}', $count, $zero); } return str_replace('{}', $count, $singular); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ # Usage ## Two parameters, singular and plural text ### Value: 0, show "You have 0 apple." `` {% set apples = 0 %} {{ apples|pluralize('You have {} apple.', 'You have {} apples.') }} `` ### Value: 1, show "You have 1 apple." `` {% set apples = 0 %} {{ apples|pluralize('You have {} apple.', 'You have {} apples.') }} `` ### Value: 2, show "You have 2 apples." `` {% set apples = 0 %} {{ apples|pluralize('You have {} apple.', 'You have {} apples.') }} `` ## Three parameters, singular, plural and zero text ### Value: 0, show "You don't have an apple." `` {% set apples = 0 %} {{ apples|pluralize('You have only one apple.', 'You have {} apples.', 'You don\'t have an apple.') }} `` ### Value: 1, show "You have only one apple." `` {% set apples = 0 %} {{ apples|pluralize('You have only one apple.', 'You have {} apples.', 'You don\'t have an apple.') }} `` ### Value: 2, show "You have 2 apples." `` {% set apples = 0 %} {{ apples|pluralize('You have only one apple.', 'You have {} apples.', 'You don\'t have an apple.') }} ``