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
Requires
- php: ^8.1|^8.2|^8.3|^8.4
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- spatie/laravel-ray: ^1.35
This package is auto-updated.
Last update: 2025-12-24 14:22:40 UTC
README
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
- Fillable fields from your model class.
- 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 | users.email | |||
| fields | User | fields.users.email | ||
| fields | User | 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.