subham/auto-translations

Automatically generate translation files for all your Laravel models

Fund package maintenance!
subham

Installs: 15

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 1

pkg:composer/subham/auto-translations

v1.1.0 2025-10-15 15:41 UTC

This package is auto-updated.

Last update: 2025-12-24 14:22:40 UTC


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.