hostnet/entity-translation-bundle
Composer 安装命令:
composer require hostnet/entity-translation-bundle
包简介
Translation bundle to help with rendering pretty names for enum-like classes.
README 文档
README
This bundle allows the automation of translating an enum value to a human readable string, for example, displaying a status text instead of the enum-value of a status code.
In it's essence, it allows to map a value to a translation within the domain that is the class name. What this means in practice is that you can use it as follows:
$status_text = $translator->trans(SetupStatus::DONE, [], SetupStatus::class);
Or in a twig template:
{{ constant('AppBundle\\Entity\\SetupStatus::DONE') | trans([], 'AppBundle\\Entity\\SetupStatus') }}
Requirements
The entity translation bundle requires at least php 7.3 and the symfony translation component. For specific requirements, please check composer.json
Installation
Installing is pretty straightforward, this package is available on packagist.
Example
$ composer require hostnet/entity-translation-bundle
Register The Bundle in your AppKernel
This bundle makes use of the translator which is registered by the framework bundle. So make sure you register this bundle after the FrameworkBundle.
class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // ... new AppBundle\AppBundle(), new Hostnet\Bundle\EntityTranslationBundle\HostnetEntityTranslationBundle(), // ... ]; return $bundles; } }
Usage
Simply add an enum.en.yml to the translations folder in the Resources folder of one of your bundles. This will contain the translations for a given enum. The translation keys are the fully qualified namespaces in lowercase and an _ between CamelCase words. So for instance the enum AppBundle\Entity\SetupStatus would become app_bundle.entity.setup_status.
Consider the following class:
<?php namespace AppBundle\Entity; final class SetupStatus { const PENDING = 1; const DONE = 2; const ERROR = 3; const REVERTING_CHANGES = 4; }
Your AppBundle/Resources/translations/enum.en.yml could look as followed:
app_bundle: entity: setup_status: pending : Installation Pending. done : Installation Complete. error : An error occured. reverting_changes : Reverting changes.
The translator will then pick up all enum classes defined in your translation file.
hostnet/entity-translation-bundle 适用场景与选型建议
hostnet/entity-translation-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 99.78k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 09 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hostnet/entity-translation-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hostnet/entity-translation-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 99.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-09-25