laravel-model-translation-helpers maintained by mphpmaster
Description
Laravel model translation helpers.
Author
Last update
2023/06/11 08:30
(dev-main)
License
Downloads
4
Tags
Laravel model translation helpers
Dependencies:
- php >=8.1 REQUIRED IN YOUR PROJECT
- laravel >=8 REQUIRED IN YOUR PROJECT
- laravel/helpers ^1.5 composer will install it automatically
Installation:
composer require mphpmaster/laravel-model-translation-helpers
Usage:
- Add trait to your model class (REQUIRED):
use \MPhpMaster\LaravelModelTranslationHelpers\Traits\TModelTranslation;
- Override this method if the model has custom translation filename (OPTIONAL):
public static function getTranslationKey(): ?string
{
return str_singular(snake_case((new static)->getTable()));
}
- Create Translation file (REQUIRED):
- Translation file must be under
modelsfolder in order to use it with this helper.- Name the file as you defined it with
getTranslationKeymethod on step 2, by default the filename must be like model name in snake case. Example: If you use defaultgetTranslationKeymethod it will be for modelUserasuser.php.
touch lang/en/models/user.php
- Default translation file will be like:
<?php
return [
'singular' => 'User',
'plural' => 'Users',
'fields' => [
'id' => 'ID',
'email' => 'Email',
'password' => 'Password',
],
'hi_name' => 'Hi, {name}',
];
?>
- Use the helper anywhere:
// Basic usage
\App\Models\User::trans('singular'); // User
\App\Models\User::trans('plural'); // Users
// fields:
\App\Models\User::trans('fields.email'); // Email
// or
\App\Models\User::trans('email'); // Email
// Advanced Usage:
\App\Models\User::trans('hi_name', [ 'name' => 'mPhpMaster' ], 'en', 'Please Login!'); // Hi, mPhpMaster
License
The Laravel framework is open-sourced software licensed under the MIT license.
This Helpers is open-sourced software licensed under the MIT license.