mesavolt / inflector
易用的法语复数生成器
v1.0.0
2019-09-30 21:17 UTC
Requires
- php: >=7.1
- ext-json: *
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^7
- twig/twig: ^2.4
This package is auto-updated.
Last update: 2024-09-13 03:24:09 UTC
README
用法
将包添加到您的项目中
composer require mesavolt/inflector
// Use it with any number or countable value: Inflector::plural('cheval', 2); // Returns 'chevaux' Inflector::plural('cheval', 1); // Returns 'chevaux' Inflector::plural('cheval', [$horse1, $horse2]); // Returns 'chevaux' // Specify the plural form if you want: Inflector::plural('cheval', 2, 'chevals'); // Returns 'chevals' Inflector::plural('un petit cheval', 2, 'des petits chevaux'); // Returns 'des petits chevaux'
集成
Symfony >=3 与 Twig >1.26
如果您使用的是 Symfony 3.3 中引入的默认 自动配置功能,则只需将 MesaVolt\Twig\InflectorExtension
注册为服务在您的 services.yml
文件中。Symfony 会正确地标记它以将其注册到您的应用程序使用的 twig 环境中。
如果您不使用自动配置功能或您的版本中不可用,则需要手动应用标记,当您将扩展注册为服务时。
# Symfony 3: app/config/services.yml # Symfony 4: config/services.yaml services: # Use this if you use the default auto-configuring feature of Symfony >=3.3 DI container MesaVolt\Twig\InflectorExtension: ~ # Use this if you **don't** use the auto-configuring feature of Symfony >=3.3 DI container app.inflector_extension: class: MesaVolt\Twig\InflectorExtension tags: { name: twig.extension }
然后,您可以在模板中使用扩展提供的 plural
过滤器
{# templates/my-template.html.twig #} <p>Il y a {{ users|length }} {{ 'utilisateur'|plural(users) }} dans la base de données.</p>
测试
composer dump-autoload # make sure vendor/autoload.php exists
./vendor/bin/phpunit