mnaimjons/multi-lang-entities
Composer 安装命令:
composer require mnaimjons/multi-lang-entities
包简介
A Laravel package for handling multi-language entities.
README 文档
README
The mnaimjons/multi-lang-entities package allows you to easily manage multi-language (localized) entities in Laravel projects. This package provides a flexible way to translate attributes of any Eloquent model in Laravel.
Installation
-
Require the package via Composer:
composer require mnaimjons/multi-lang-entities
-
(Optional) Publish the configuration file:
You can publish the configuration file to customize the default locale and other settings.
php artisan vendor:publish --provider="Mnaimjons\MultiLangEntities\MultiLangEntitiesServiceProvider" --tag="config"
This will publish the configuration file
config/multi_lang_entities.phpto your Laravel application.
Configuration
Once the configuration file is published, you can customize the following options:
return [ 'default_locale' => env('APP_LOCALE', 'en'), // Default locale 'eager_loading' => false, // Whether to use eager loading for translations ];
Usage
To use the package in your Laravel models, you can add the HasTranslations trait to any Eloquent model and define which attributes should be translatable.
Step 1: Add Translatable Attributes
Add the HasTranslations trait to your model and define the attributes you want to translate by adding the $translatable property.
Example with Category Model:
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Mnaimjons\MultiLangEntities\Traits\HasTranslations; class Category extends Model { use HasTranslations; protected $fillable = ['slug']; // Define the translatable attributes protected $translatable = ['name', 'description']; }
Step 2: Adding Translations
You can add or update translations for any of the defined translatable attributes by calling the setTranslations method on the model.
Example:
$category = Category::find(1); $category->setTranslations([ 'name' => 'Category Name in English', 'description' => 'Description in English' ], 'en'); // Adding translation for 'en' (English) $category->setTranslations([ 'name' => 'Category Name in French', 'description' => 'Description in French' ], 'fr'); // Adding translation for 'fr' (French)
Step 3: Retrieving Translations
You can retrieve translated attributes using the standard Eloquent model attribute accessors. The package automatically uses the current application locale (app()->getLocale()) to return the appropriate translation.
Example:
$category = Category::find(1); // Get the translated name and description based on the current locale echo $category->name; // Output: Translated 'name' attribute echo $category->description; // Output: Translated 'description' attribute
Step 4: Changing Locale
If you want to retrieve translations in a specific locale other than the current application locale, you can pass the desired locale to the getTranslatedAttribute method.
Example:
$category = Category::find(1); // Retrieve translations for the 'fr' locale (French) $nameInFrench = $category->getTranslatedAttribute('name', 'fr'); $descriptionInFrench = $category->getTranslatedAttribute('description', 'fr');
Step 5: Eager or Lazy Loading Translations
By default, translations are lazy-loaded (loaded on demand). If you want to eager-load translations to improve performance, use the withTranslations method.
Example:
$category = Category::find(1)->withTranslations(); // Eager-load translations echo $category->name; // Will use eager-loaded translations
Artisan Commands
The package provides several Artisan commands to manage translations from the command line:
-
List Translations: Display all translations for a specific entity.
php artisan translations:list "App\Models\Category" 1This will list all translations for the
Categorymodel with ID1. -
Add or Update Translations: Add or update a translation for a specific attribute.
php artisan translations:add "App\Models\Category" 1 en name "New Category Name"
-
Remove Translation: Remove a specific translation.
php artisan translations:remove "App\Models\Category" 1 en name
mnaimjons/multi-lang-entities 适用场景与选型建议
mnaimjons/multi-lang-entities 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mnaimjons/multi-lang-entities 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mnaimjons/multi-lang-entities 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-03