Looking to hire Laravel developers? Try LaraJobs

laravel-model-translation-helpers maintained by mphpmaster

Description
Laravel model translation helpers.
Author
Last update
2023/06/11 08:30 (dev-main)
License
Links
Downloads
4

Comments
comments powered by Disqus

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:

  1. Add trait to your model class (REQUIRED):
use \MPhpMaster\LaravelModelTranslationHelpers\Traits\TModelTranslation;
  1. Override this method if the model has custom translation filename (OPTIONAL):
public static function getTranslationKey(): ?string
{
    return str_singular(snake_case((new static)->getTable()));
}
  1. Create Translation file (REQUIRED):
  • Translation file must be under models folder in order to use it with this helper.
  • Name the file as you defined it with getTranslationKey method on step 2, by default the filename must be like model name in snake case. Example: If you use default getTranslationKey method it will be for model User as user.php.
touch lang/en/models/user.php
  1. Default translation file will be like:
<?php

return [
    'singular' => 'User',
    'plural' => 'Users',
    'fields' => [
        'id' => 'ID',
        'email' => 'Email',
        'password' => 'Password',
    ],
    'hi_name' => 'Hi, {name}',
];

?>
  1. 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.