mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-07 07:57:01 +00:00
Tweak code and docs
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# 3.11.0 (2024-XX-XX)
|
||||
|
||||
* Add the `singular` and `plural` filters in `StringExtension`
|
||||
* Deprecate the second argument of `Twig\Node\Expression\CallExpression::compileArguments()`
|
||||
|
||||
# 3.10.3 (2024-05-16)
|
||||
|
||||
@@ -41,11 +41,13 @@ Filters
|
||||
merge
|
||||
nl2br
|
||||
number_format
|
||||
plural
|
||||
raw
|
||||
reduce
|
||||
replace
|
||||
reverse
|
||||
round
|
||||
singular
|
||||
slice
|
||||
slug
|
||||
sort
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
``plural``
|
||||
==========
|
||||
|
||||
.. versionadded:: 3.11
|
||||
|
||||
The ``plural`` filter was added in Twig 3.11.
|
||||
|
||||
The ``plural`` filter transforms a given noun in its singular form into its
|
||||
plural version:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{# English (en) rules are used by default #}
|
||||
{{ 'partition'|pluralize() }}
|
||||
partitions
|
||||
|
||||
{{ 'partition'|pluralize('fr') }}
|
||||
partitions
|
||||
|
||||
.. note::
|
||||
|
||||
The ``plural`` filter is part of the ``StringExtension`` which is not
|
||||
installed by default. Install it first:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ composer require twig/string-extra
|
||||
|
||||
Then, on Symfony projects, install the ``twig/extra-bundle``:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ composer require twig/extra-bundle
|
||||
|
||||
Otherwise, add the extension explicitly on the Twig environment::
|
||||
|
||||
use Twig\Extra\String\StringExtension;
|
||||
|
||||
$twig = new \Twig\Environment(...);
|
||||
$twig->addExtension(new StringExtension());
|
||||
|
||||
Arguments
|
||||
---------
|
||||
|
||||
* ``locale``: The locale of the original string (limited to languages supported by the from Symfony `inflector`_, part of the String component)
|
||||
* ``all``: Whether to return all possible plurals as an array, default is ``false``
|
||||
|
||||
.. note::
|
||||
|
||||
Internally, Twig uses the `pluralize`_ method from the Symfony String component.
|
||||
|
||||
.. _`inflector`: <https://symfony.com/doc/current/components/string.html#inflector>
|
||||
.. _`pluralize`: <https://symfony.com/doc/current/components/string.html#inflector>
|
||||
@@ -1,45 +0,0 @@
|
||||
``pluralize``
|
||||
========
|
||||
|
||||
The ``pluralize`` filter transforms a given noun in its singular form into its plural version.
|
||||
|
||||
Here is an example:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ 'partitions'|pluralize('en') }}
|
||||
partition
|
||||
|
||||
.. note::
|
||||
`lang` parameter is mandatory for this filter as only English and French are supported by the Inflector in Symfony.
|
||||
|
||||
The ``pluralize`` filter uses the method by the same name in Symfony's
|
||||
`Inflector <https://symfony.com/doc/current/components/string.html#inflector>`_.
|
||||
|
||||
.. note::
|
||||
|
||||
The ``pluralize`` filter is part of the ``StringExtension`` which is not
|
||||
installed by default. Install it first:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ composer require twig/string-extra
|
||||
|
||||
Then, on Symfony projects, install the ``twig/extra-bundle``:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ composer require twig/extra-bundle
|
||||
|
||||
Otherwise, add the extension explicitly on the Twig environment::
|
||||
|
||||
use Twig\Extra\String\StringExtension;
|
||||
|
||||
$twig = new \Twig\Environment(...);
|
||||
$twig->addExtension(new StringExtension());
|
||||
|
||||
Arguments
|
||||
---------
|
||||
|
||||
* ``lang``: The lang of the original string. Only English (`en`) and French (`fr`) are supported.
|
||||
* ``singleResult``: This argument is optional. If set to false, the filter will return an array of pluralized words. Default is true.
|
||||
@@ -0,0 +1,52 @@
|
||||
``singular``
|
||||
============
|
||||
|
||||
.. versionadded:: 3.11
|
||||
|
||||
The ``singular`` filter was added in Twig 3.11.
|
||||
|
||||
The ``singular`` filter transforms a given noun in its plural form into its
|
||||
singular version:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{# English (en) rules are used by default #}
|
||||
{{ 'partitions'|singular() }}
|
||||
partition
|
||||
|
||||
{{ 'partitions'|singular('fr') }}
|
||||
partition
|
||||
|
||||
.. note::
|
||||
|
||||
The ``singular`` filter is part of the ``StringExtension`` which is not
|
||||
installed by default. Install it first:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ composer require twig/string-extra
|
||||
|
||||
Then, on Symfony projects, install the ``twig/extra-bundle``:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ composer require twig/extra-bundle
|
||||
|
||||
Otherwise, add the extension explicitly on the Twig environment::
|
||||
|
||||
use Twig\Extra\String\StringExtension;
|
||||
|
||||
$twig = new \Twig\Environment(...);
|
||||
$twig->addExtension(new StringExtension());
|
||||
|
||||
Arguments
|
||||
---------
|
||||
|
||||
* ``locale``: The locale of the original string (limited to languages supported by the from Symfony `inflector`_, part of the String component)
|
||||
* ``all``: Whether to return all possible plurals as an array, default is ``false``
|
||||
|
||||
.. note::
|
||||
|
||||
Internally, Twig uses the `singularize`_ method from the Symfony String component.
|
||||
|
||||
.. _`singularize`: <https://symfony.com/doc/current/components/string.html#inflector>
|
||||
@@ -1,45 +0,0 @@
|
||||
``singularize``
|
||||
========
|
||||
|
||||
The ``singularize`` filter transforms a given noun in its plural form into its singular version.
|
||||
|
||||
Here is an example:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ 'partitions'|singularize('en') }}
|
||||
partition
|
||||
|
||||
.. note::
|
||||
`lang` parameter is mandatory for this filter as only English and French are supported by the Inflector in Symfony.
|
||||
|
||||
The ``singularize`` filter uses the method by the same name in Symfony's
|
||||
`Inflector <https://symfony.com/doc/current/components/string.html#inflector>`_.
|
||||
|
||||
.. note::
|
||||
|
||||
The ``singularize`` filter is part of the ``StringExtension`` which is not
|
||||
installed by default. Install it first:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ composer require twig/string-extra
|
||||
|
||||
Then, on Symfony projects, install the ``twig/extra-bundle``:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ composer require twig/extra-bundle
|
||||
|
||||
Otherwise, add the extension explicitly on the Twig environment::
|
||||
|
||||
use Twig\Extra\String\StringExtension;
|
||||
|
||||
$twig = new \Twig\Environment(...);
|
||||
$twig->addExtension(new StringExtension());
|
||||
|
||||
Arguments
|
||||
---------
|
||||
|
||||
* ``lang``: The lang of the original string. Only English (`en`) and French (`fr`) are supported.
|
||||
* ``singleResult``: This argument is optional. If set to false, the filter will return an array of singularized nouns. Default is true.
|
||||
@@ -2,21 +2,20 @@ String Extension
|
||||
================
|
||||
|
||||
This package is a Twig extension that provides integration with the Symfony
|
||||
String component.
|
||||
String component. It provides the following filters:
|
||||
|
||||
It provides a [`u`][1] filter that wraps a text in a `UnicodeString`
|
||||
object to give access to [methods of the class][2].
|
||||
* [`u`][1]: Wraps a text in a `UnicodeString` object to give access to
|
||||
[methods of the class][2].
|
||||
|
||||
It also provides a [`slug`][3] filter which is simply a wrapper for the
|
||||
[`AsciiSlugger`][4]'s `slug` method.
|
||||
* [`slug`][3]: Wraps the [`AsciiSlugger`][4]'s `slug` method.
|
||||
|
||||
In addition, two filters are provided to [`singularize`][5] and [`pluralize`][6] a noun.
|
||||
Behind the scenes, it uses the [`Inflector`][6] class from the Symfony String component.
|
||||
* [`singular`][5] and [`plural`][6]: Wraps the [`Inflector`][7] `singularize`
|
||||
and `pluralize` methods.
|
||||
|
||||
[1]: https://twig.symfony.com/u
|
||||
[2]: https://symfony.com/doc/current/components/string.html
|
||||
[3]: https://twig.symfony.com/slug
|
||||
[4]: https://symfony.com/doc/current/components/string.html#slugger
|
||||
[5]: https://twig.symfony.com/singularize
|
||||
[6]: https://twig.symfony.com/pluralize
|
||||
[5]: https://twig.symfony.com/singular
|
||||
[6]: https://twig.symfony.com/plural
|
||||
[7]: https://symfony.com/doc/current/components/string.html#inflector
|
||||
|
||||
@@ -37,8 +37,8 @@ final class StringExtension extends AbstractExtension
|
||||
return [
|
||||
new TwigFilter('u', [$this, 'createUnicodeString']),
|
||||
new TwigFilter('slug', [$this, 'createSlug']),
|
||||
new TwigFilter('pluralize', [$this, 'pluralize']),
|
||||
new TwigFilter('singularize', [$this, 'singularize']),
|
||||
new TwigFilter('plural', [$this, 'plural']),
|
||||
new TwigFilter('singular', [$this, 'singular']),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -55,38 +55,36 @@ final class StringExtension extends AbstractExtension
|
||||
/**
|
||||
* @return array|string
|
||||
*/
|
||||
public function pluralize(string $value, string $lang, bool $singleResult = true)
|
||||
public function plural(string $value, string $locale = 'en', bool $all = false)
|
||||
{
|
||||
switch (true) {
|
||||
case $singleResult:
|
||||
return $this->getInflector($lang)->pluralize($value)[0];
|
||||
default:
|
||||
return $this->getInflector($lang)->pluralize($value);
|
||||
if ($all) {
|
||||
return $this->getInflector($locale)->pluralize($value);
|
||||
}
|
||||
|
||||
return $this->getInflector($locale)->pluralize($value)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|string
|
||||
*/
|
||||
public function singularize(string $value, string $lang, bool $singleResult = true)
|
||||
public function singular(string $value, string $locale = 'en', bool $all = false)
|
||||
{
|
||||
switch (true) {
|
||||
case $singleResult:
|
||||
return $this->getInflector($lang)->singularize($value)[0];
|
||||
default:
|
||||
return $this->getInflector($lang)->singularize($value);
|
||||
if ($all) {
|
||||
return $this->getInflector($locale)->singularize($value);
|
||||
}
|
||||
|
||||
return $this->getInflector($locale)->singularize($value)[0];
|
||||
}
|
||||
|
||||
private function getInflector(string $lang): InflectorInterface
|
||||
private function getInflector(string $locale): InflectorInterface
|
||||
{
|
||||
switch ($lang) {
|
||||
switch ($locale) {
|
||||
case 'fr':
|
||||
return $this->frenchInflector ?? $this->frenchInflector = new FrenchInflector();
|
||||
case 'en':
|
||||
return $this->englishInflector ?? $this->englishInflector = new EnglishInflector();
|
||||
default:
|
||||
throw new \InvalidArgumentException(sprintf('Language "%s" is not supported.', $lang));
|
||||
throw new \InvalidArgumentException(sprintf('Locale "%s" is not supported.', $locale));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
--TEST--
|
||||
"pluralize" filter
|
||||
"plural" filter
|
||||
--TEMPLATE--
|
||||
{{ 'partition'|pluralize('it') }}
|
||||
{{ 'partition'|plural('it') }}
|
||||
|
||||
--DATA--
|
||||
return []
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
--TEST--
|
||||
"plural" filter
|
||||
--TEMPLATE--
|
||||
{{ 'partition'|plural('fr') }}
|
||||
{{ 'partition'|plural('fr', all=true)|join(',') }}
|
||||
{{ 'person'|plural('fr') }}
|
||||
{{ 'person'|plural('en', all=true)|join(',') }}
|
||||
|
||||
--DATA--
|
||||
return []
|
||||
--EXPECT--
|
||||
partitions
|
||||
partitions
|
||||
persons
|
||||
persons,people
|
||||
@@ -1,17 +0,0 @@
|
||||
--TEST--
|
||||
"pluralize" filter
|
||||
--TEMPLATE--
|
||||
{{ 'partition'|pluralize('fr') }}
|
||||
{{ 'partition'|pluralize('fr', false)|first }}
|
||||
{{ 'person'|pluralize('fr') }}
|
||||
{{ 'person'|pluralize('en', false)|first }}
|
||||
{{ 'person'|pluralize('en', false)|last }}
|
||||
|
||||
--DATA--
|
||||
return []
|
||||
--EXPECT--
|
||||
partitions
|
||||
partitions
|
||||
persons
|
||||
persons
|
||||
people
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
--TEST--
|
||||
"singularize" filter
|
||||
"singular" filter
|
||||
--TEMPLATE--
|
||||
{{ 'partitions'|singularize('it') }}
|
||||
{{ 'partitions'|singular('it') }}
|
||||
|
||||
--DATA--
|
||||
return []
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
--TEST--
|
||||
"singular" filter
|
||||
--TEMPLATE--
|
||||
{{ 'partitions'|singular('fr') }}
|
||||
{{ 'partitions'|singular('fr', all=true)|join(',') }}
|
||||
{{ 'persons'|singular('fr') }}
|
||||
{{ 'persons'|singular('en', all=true)|join(',') }}
|
||||
{{ 'people'|singular('en') }}
|
||||
{{ 'people'|singular('en', all=true)|join(',') }}
|
||||
|
||||
--DATA--
|
||||
return []
|
||||
--EXPECT--
|
||||
partition
|
||||
partition
|
||||
person
|
||||
person
|
||||
person
|
||||
person
|
||||
@@ -1,19 +0,0 @@
|
||||
--TEST--
|
||||
"singularize" filter
|
||||
--TEMPLATE--
|
||||
{{ 'partitions'|singularize('fr') }}
|
||||
{{ 'partitions'|singularize('fr', false)|first }}
|
||||
{{ 'persons'|singularize('fr') }}
|
||||
{{ 'persons'|singularize('en', false)|first }}
|
||||
{{ 'people'|singularize('en') }}
|
||||
{{ 'people'|singularize('en', false)|first }}
|
||||
|
||||
--DATA--
|
||||
return []
|
||||
--EXPECT--
|
||||
partition
|
||||
partition
|
||||
person
|
||||
person
|
||||
person
|
||||
person
|
||||
Reference in New Issue
Block a user