mphpmaster/laravel-model-translation-helpers

Laravel model translation helpers.

1.0.0 2023-06-11 06:30 UTC

This package is auto-updated.

Last update: 2024-04-20 11:01:43 UTC


README

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.