larasup/localization
最新稳定版本:2.0.1
Composer 安装命令:
composer require larasup/localization
包简介
Model-level localization for Laravel via translatable attributes
README 文档
README
Model-level localization for Laravel. Add translatable attributes to any Eloquent model without modifying its table — translations are stored in a separate table.
Installation
composer require larasup/localization
The package uses Laravel auto-discovery. Run migrations:
php artisan migrate
Publish the config (optional):
php artisan vendor:publish --tag=localize-config
Configuration
// config/localize.php
return [
'table_prefix' => 'localize_', // customize table names prefix
'classes' => [
// Map model classes to fixed integer IDs (optional).
// If not mapped, IDs are auto-generated via larasup/reference.
// App\Models\Product::class => 10,
],
];
Usage
1. Add the trait to your model
use Larasup\Localization\Traits\Localizable;
class Product extends Model
{
use Localizable;
const LOCALIZABLE = ['name', 'description'];
}
2. Write translations
Translations are saved for the current app locale:
App::setLocale('en');
$product->name = 'Laptop';
App::setLocale('ru');
$product->name = 'Ноутбук';
3. Read translations
App::setLocale('ru');
echo $product->name; // "Ноутбук"
App::setLocale('en');
echo $product->name; // "Laptop"
4. Query relations
// Localizations for the current locale
$product->localizations;
// All localizations across all languages
$product->all_localizations;
Languages
The package ships with a localize:import command that seeds common languages:
php artisan localize:import
Default languages: English (en), Russian (ru), Kazakh (kk), Ukrainian (uk), German (de), French (fr), Spanish (es), Italian (it), Arabic (ar), Chinese (zh).
English and Russian are enabled by default (status=1), others are disabled (status=0).
How it works
- The
Localizabletrait intercepts__get/__setfor attributes listed in the model'sLOCALIZABLEconstant. - Translations are stored in the
localize_localizationstable, keyed by a class ID, model primary key, language code, and attribute key. - Class IDs are resolved via config or auto-generated using
larasup/reference(Book "models" + Item per class). - Class IDs are cached indefinitely via
Cache::rememberForever().
Dependencies
- larasup/reference — for automatic class ID management.
License
MIT
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 2
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-26