flobbos/laravel-translatable-db
Composer 安装命令:
composer require flobbos/laravel-translatable-db
包简介
A Laravel package for multilingual models
README 文档
README
Database-backed translated Eloquent attributes, resolved by a language id instead of a locale string.
This package started as a fork of dimsav/laravel-translatable. It only reads translated attributes. It does not manage CRUD forms or translation persistence.
Compatibility
| Laravel | Package |
|---|---|
| 13.x | 2.x |
| 12.x | 2.x |
| 11.x | 2.x |
| 10.x | 2.x |
| 5.3-9.x | 1.x |
Laravel 10 support means the package requires PHP ^8.1. Laravel itself may require a higher PHP version for newer framework releases.
Installation
composer require flobbos/laravel-translatable-db:^2.0
Laravel auto-discovery registers the service provider. Publish the config only when you need to override defaults:
php artisan vendor:publish --provider="Flobbos\TranslatableDB\TranslatableDBServiceProvider"
Tables
Example for a translated Country model:
Schema::create('languages', function (Blueprint $table) { $table->id(); $table->string('locale')->unique(); $table->string('name'); }); Schema::create('countries', function (Blueprint $table) { $table->id(); $table->string('code')->unique(); $table->timestamps(); }); Schema::create('country_translations', function (Blueprint $table) { $table->id(); $table->foreignId('country_id')->constrained()->cascadeOnDelete(); $table->foreignId('language_id')->constrained()->cascadeOnDelete(); $table->string('name'); $table->unique(['country_id', 'language_id']); });
If your default language lives on the main model table, add the translated column there and enable native fallback on the model:
public $fallbackAttributes = ['name'];
Then set:
'native_mode' => true,
Models
namespace App\Models; use Flobbos\TranslatableDB\TranslatableDB; use Illuminate\Database\Eloquent\Model; class Country extends Model { use TranslatableDB; public $translatedAttributes = ['name']; public $fallbackAttributes = ['name']; protected $fillable = ['code']; }
namespace App\Models; use Illuminate\Database\Eloquent\Model; class CountryTranslation extends Model { public $timestamps = false; protected $fillable = ['language_id', 'name']; }
By convention, Country uses CountryTranslation. Override it when needed:
public $translationModel = CountryText::class;
Usage
$country = Country::where('code', 'gr')->first(); app()->setLocale('en'); echo $country->name; // Greece app()->setLocale('de'); echo $country->name; // Griechenland echo $country->getAttribute('name:2'); // Explicit language id
Configuration
Important defaults:
'use_db' => true, 'language_model' => App\Models\Language::class, 'locale_key' => 'language_id', 'locale_column' => 'locale', 'use_fallback' => true, 'fallback_locale_id' => 1, 'middleware_default' => false, 'to_array_always_loads_translations' => false,
With database mode enabled, the middleware resolves app()->getLocale() through your language model and stores the matching id on the request.
With database mode disabled, configure languages directly:
'use_db' => false, 'language_array' => [ 'de' => ['name' => 'Deutsch', 'language_id' => 1], 'en' => ['name' => 'English', 'language_id' => 2], ],
Middleware
The middleware is no longer pushed globally by default in 2.x.
Register it where translated route content needs automatic language id resolution:
use Flobbos\TranslatableDB\Middleware\LanguageIdentification; Route::middleware(LanguageIdentification::class)->group(function () { // translated routes });
If you really want the old global behavior:
'middleware_default' => true,
Polymorphic Translations
Implement PolyTrans and set the morph name:
namespace App\Models; use Flobbos\TranslatableDB\Contracts\PolyTrans; use Flobbos\TranslatableDB\TranslatableDB; use Illuminate\Database\Eloquent\Model; class Country extends Model implements PolyTrans { use TranslatableDB; public $translationModel = Translation::class; protected $translationForeignKey = 'translatable'; }
Testing
composer test
flobbos/laravel-translatable-db 适用场景与选型建议
flobbos/laravel-translatable-db 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.37k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2017 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「translation」 「language」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 flobbos/laravel-translatable-db 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 flobbos/laravel-translatable-db 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 flobbos/laravel-translatable-db 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Dibi is Database Abstraction Library for PHP
Easy to use i18n translation PHP class for multi-language websites
Package for convenient work with Laravel's localization features
Store your language lines in the database, yaml or other sources
A custom URL rule class for Yii 2 which allows to create translated URL rules
统计信息
- 总下载量: 3.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 16
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-31
