bruno-fernandes / laravel-multi-language
为模型实现多语言支持的简单方法。
0.0.4
2020-03-17 13:07 UTC
Requires
- php: >=7.1
- illuminate/support: ^5.6|^5.7|^5.8|^6.0|^7.0
Requires (Dev)
- orchestra/testbench: 3.8.*
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-09-17 23:32:35 UTC
README
重要:正在积极开发中。请勿在生产环境中使用,API 可能会更改。
eloquent模型翻译的简单方法。其他包也提供了翻译功能,但这是一种不同的方法,有一些权衡。优先考虑简单性。
// TODO: 在此处添加示例数据库模式。
要点
- 易于在现有应用程序中启动
- 所有eloquent模型字段均可翻译
- 原始内容可以创建在任何语言中
- 翻译可以轻松地从原始内容复制
- 翻译可以稍后轻松关联
安装
您可以通过composer安装此包
composer require bruno-fernandes/laravel-multi-language
如果您想设置自定义列名,请发布配置文件并覆盖默认值
php artisan vendor:publish --provider="BrunoFernandes\LaravelMultiLanguage\LaravelMultiLanguageServiceProvider"
用法
// Import the Translatable trait into the eloquent model use BrunoFernandes\LaravelMultiLanguage\Translatable; class Page extends Model { use Translatable; } // Create a migration to add the required columns to the model's table // Example: class AddMultilanguageFieldsToPagesTable extends Migration { public function up() { Schema::table('pages', function (Blueprint $table) { // Create columns $table->string(config('laravel-multi-language.lang_key'), 6) ->default('en')->index()->after('id'); $table->integer(config('laravel-multi-language.foreign_key')) ->unsigned()->nullable()->index()->after('id'); // Create composite unique index to prevent multiple // records using the same lang key $table->unique([ config('laravel-multi-language.foreign_key'), config('laravel-multi-language.lang_key') ]); }); // TODO: if there are already records on the table, create a migration to update // all of them and set the lang and the original_id with the correct values } } // // Usage // $page = Page::create(['title' => 'English title']); $englishPage = $page->translateTo('es', ['title' => 'Spanish title']); $originalPages = Page::onlyOriginals()->get(); // the package will apply the lang scope by default, so only // the current locale records are returned (it can be disable in the config file) $currentLocalePages = Page::get(); // if apply lang global scope is disabled you can use the lang scope as follow: $enPagesWithTranslations = Page::lang('en')->withTranslations()->get(); // NOTE: always use withTranslations() rather than with('translations) as it is more efficient // using withTranslations() will exlude the current locale from the translations relationship // if you would like to remove a global scope for a given query, // you may use the withoutGlobalScope method as follow: use BrunoFernandes\LaravelMultiLanguage\Scopes\LangScope; $allPagesOfAllLocales = Page::withoutGlobalScope(LangScope::class)->get(); // TODO: add usage samples to be added
已知问题
-
当与 Searchable 包 一起使用时,需要在使用搜索方法之后手动删除全局范围并重新应用。
-
当使用hasOne关系时,如果未设置 foreign_key 和 local_key,则LangScope(全局范围)将应用于关系。如果关系模型不可翻译,则会抛出错误。
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'live_players.original_id' in 'where clause' (SQL: select * from `live_players` where `live_players`.`original_id` in (35) and `live_players`.`deleted_at` is null) (View: /home/vagrant/code/resources/frontend/views/index.blade.php)
// This does not work class Content extends Model { public function livePlayer() { return $this->hasOne(LivePlayer::class); } } // This works class Content extends Model { public function livePlayer() { return $this->hasOne(LivePlayer::class, 'id', 'live_player_id'); } }
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 security@brunofernandes.com 而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅 许可证文件。