larasup/localization 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 Localizable trait intercepts __get / __set for attributes listed in the model's LOCALIZABLE constant.
  • Translations are stored in the localize_localizations table, 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

License

MIT

统计信息

  • 总下载量: 43
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 2
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固