承接 subham/auto-translations 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

subham/auto-translations

最新稳定版本:v1.1.0

Composer 安装命令:

composer require subham/auto-translations

包简介

Automatically generate translation files for all your Laravel models

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Automatically generate translation files for all your Laravel models. This package scans your models and creates JSON translation files based on their fillable attributes.

Installation

You can install the package via composer:

composer require subham/auto-translations

You can publish the config file with:

php artisan vendor:publish --tag="auto-translations-config"

This is the contents of the published config file:

    // Prefix for translation keys
    'prefix' => 'fields',
    
    // Suffix for translation keys
    'suffix' => 'label',
    
    // Locales to generate translations for
    'locale' => ['en', 'es', 'fr'],
    
    // Paths to scan for models
    'model_paths' => [
        app_path('Models'),
        // base_path('Modules/MyModule/Models'), // For modular apps
    ],
    
    // Where to publish translation files
    'publish_path' => resource_path('lang'),
];

Define Translation keys

  1. Fillable fields from your model class.
  2. Define translationKeys() method on your model class
class Product extends Model
{
    protected $fillable = ['name', 'price'];

    public function translationKeys(): array
    {
        return ['sku', 'category_name'];
    }
}

Translation Key Format

{prefix}.{table_name}.{column_name}.{suffix}

Example

Prefix Model Column Suffix Final Result
User email users.email
fields User email fields.users.email
fields User email column fields.users.email.column

Usage

Basic Command

Generate translation files for all models:

php artisan auto-translations:generate

This will prompt you for confirmation if translation files already exist.

Force Overwrite

Overwrite existing translation files without confirmation:

php artisan auto-translations:generate --force

Append Mode

Add new translations while preserving existing ones:

php artisan auto-translations:generate --append

Use this when translators have customized translations and you want to add new keys only.

Example Output

For a User model with fillable attributes ['name', 'email', 'phone'] with prefix fields and suffix label:

Generated Translation File

{
  "fields.users.email.label": "Email",
  "fields.users.name.label": "Name",
  "fields.users.phone.label": "Phone"
}

Important

All generated translations will be based on model's fillable attributes. The package only creates the file structure and keys - you need to manually translate the values for each locale afterward.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-13