gertjanroke/translations
Composer 安装命令:
composer require gertjanroke/translations
包简介
Translations: Easy way to make your database multilanguage
关键字:
README 文档
README
Easy way to make your database multilanguage
Install
composer require gertjanroke/translations
After install
Publish files
If all your models are directly in your app folder and your using the laravel convention, you do not need to publish the config file. But if your somehow not following this convention, like putting your models in a own folder. Than you need to publish the config file and define where your translation models are stored.
If you need to publish the config file, than first do the following think before running the publish command.
Add the following line to config/app.php.
at providers:
'providers' => [ ... Gertjanroke\Notification\NotificationServiceProvider::class, ... ],
To publish the config file run the following command:
php artisan vendor:publish --provider="Gertjanroke\Notification\NotificationServiceProvider"
Usage
Now comes the fun part:
To use the translations trait you only need to add a use inside your model class.
You can do so by:
class Page extends Model { ... use Translations; ... }
and add the use above your class like so:
... use Gertjanroke\Translations\Traits\Translations; class Page extends Model { ...
Now your almost done.
The only thing you need to do is make a translation model for the model you just added the trait.
Something like:
class PageTranslations extends Model { // }
The important part is that out of the box this package uses a model name like the one above. If you want to override this just edit the config file and your good to go.
Saving your translations
The first thing you need to do is check with way works best for you. This package comes with a default request attribute on with he he build a foreach to save all values per locale. This format is as following:
<input type="text" name="trans[<locale>][<attribute>]">
If your do not like the key word trans you can easly change this in the config.
To something like:
<input type="text" name="translations[<locale>][<attribute>]">
or
<input type="text" name="i18n[<locale>][<attribute>]">
or what ever you like.
After you made your forms you now need to save the data. This package comes with two ways to do so. Just call the following function from your model and send the request object with it. like so:
public function store(Request $request) { ... $page = new Page(); $page->saveTranslations($request); ... }
or the short version:
public function store(Request $request) { ... $page = new Page(); $page->saveTrans($request); ... }
And the package will create a new row in your database table or it will update its existing row.
Magic funtions
This package als comes with two handy funtions that let you do things like:
$page->title
and it will check if that attribute existing in your translations table. If it exists, the value will be returned in the currunt locale of your application.
The other magic function is dynamic getters. Let take the example from above and use that as a method.
$page->getTitle();
This will get the title just like the funtion above, but now you can specify with locale version you want of the given attribute.
$page->getTitle('en');
You can change 'en' for the require locale.
Custom Foreign key
If for some reason you do not like the way laravel handles the foreign keys and you want to use your own conventions,
Thust do so by setting the transForeignKey variable in your main model.
For example:
... use Gertjanroke\Translations\Traits\Translations; class Page extends Model { ... use Translations; protected $transForeignKey = 'pages_id'; ... }
gertjanroke/translations 适用场景与选型建议
gertjanroke/translations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 09 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「translations」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gertjanroke/translations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gertjanroke/translations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gertjanroke/translations 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Deepl Automated translations for Laravel lang files
Adds support for dynamic translations from Google Spreadsheets.
A Laravel package for multilingual models
TranslationBundle bundle
Symfony 2 Bundle providing form data mappers to handle translations using doctrine entities
A simple and beautiful laravel log reader
统计信息
- 总下载量: 24
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2016-09-22