delboy1978uk/bone-i18n
Composer 安装命令:
composer require delboy1978uk/bone-i18n
包简介
I18n package for Bone Framework
README 文档
README
I18n package for Bone Framework
installation
bone-i18n is a core dependency of delboy1978uk/bone, and so it is installed by default.
setup
The skeleton app has a directory for translations (usually data/translations but you can set this to anything), which
will contain locale folders such as en_US etc.
Drop in your .mo and .po files. Open config/bone-i18n.php and tweak to suit:
<?php return [ 'i18n' => [ 'enabled' => true, 'translations_dir' => 'data/translations', 'default_locale' => 'en_PI', 'supported_locales' => ['en_PI', 'en_GB', 'nl_BE', 'fr_BE'], 'date_format' => 'd/m/Y', ] ];
For any package you have that will contain translation files, edit your package class and make it implement
Bone\I18n\I18nRegistrationInterface. Create the method public function getTranslationsDirectory(): string that will
return the translations directory path.
usage
locale view helper
You can have routes prepended with the current locale by calling
<?= $this->locale() ?>
Or more conveniently
<?= $this->l() ?>
in your view files.
A link such as /user would then become /en_US/user. Bone Framework uses i18n middleware to fetch the locale and it
strips the locale from the URL and sets it as a Request Attribute, so you do not need to define routes with a locale
parameter.
controllers
In a controller action if you need the locale you can say:
$locale = $request->getAttribute('locale');
To get a translator into your controller, make it implement Bone\I18n\I18nAwareInterface and use the
Bone\I18n\Traits\HasTranslatorTrait. If you package's Package class returns the controller without going through the
Bone\Mvc\Controller\Init class, change it now to this:
return Init::controller(new YourController(), $c);
You can now call $this->getTranslator() which will return an instance of the translator.
translation view helper
To translate text in your view, call the following:
<?= $this->translate('whatever.key.to.translate') ;?>
Or again, more conveniently
<?= $this->t('whatever.key.to.translate') ;?>
i18n aware forms
Bone Framework uses delboy1978uk/form for its form functionality. However, instead of extending Del\Form, you can
create a form extending Bone\I18n\Form, which takes the translator as a second argument.
delboy1978uk/bone-i18n 适用场景与选型建议
delboy1978uk/bone-i18n 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.42k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 02 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 delboy1978uk/bone-i18n 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 delboy1978uk/bone-i18n 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-02-26