PHP code example of osama-98 / laravel-enum-translatable
1. Go to this page and download the library: Download osama-98/laravel-enum-translatable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
osama-98 / laravel-enum-translatable example snippets
enum OrderStatusEnum: string
{
use EnumTranslatable;
case PENDING = 'pending';
case SHIPPED = 'shipped';
case DELIVERED = 'delivered';
}
// config/laravel-enums.php
return [
/*
|--------------------------------------------------------------------------
| Supported Locales
|--------------------------------------------------------------------------
| The locales that allTrans() will return translations for.
*/
'supported_locales' => ['en'],
/*
|--------------------------------------------------------------------------
| Modular Support
|--------------------------------------------------------------------------
| Enable this if you use a module system such as nWidart/laravel-modules.
| Translations will be loaded from each module's namespace automatically.
*/
'modular_enabled' => false,
/*
|--------------------------------------------------------------------------
| Namespace Resolver
|--------------------------------------------------------------------------
| The class responsible for resolving a module's translation namespace.
| Extend TranslationNamespaceResolver to customise the resolution logic.
*/
'namespace_resolver' => \Osama\LaravelEnums\TranslationNamespaceResolver::class,
];
namespace App\Enums;
use Osama\LaravelEnums\Concerns\EnumTranslatable;
enum CourseStatusEnum: string
{
use EnumTranslatable;
case DRAFT = 'draft';
case PENDING = 'pending';
case PUBLISHED = 'published';
}
// lang/en/enums.php
return [
'course_statuses' => [
'draft' => 'Draft',
'draft_description' => ':name is currently in draft mode.',
'pending' => 'Pending',
'pending_description' => ':name is currently pending review.',
'published' => 'Published',
'published_description' => 'Your post has been published.',
],
];
// lang/ar/enums.php
return [
'course_statuses' => [
'draft' => 'مسودة',
'draft_description' => ':name في وضع المسودة حاليًا.',
'pending' => 'قيد المراجعة',
'pending_description' => ':name قيد المراجعة حاليًا.',
'published' => 'منشور',
'published_description' => 'تم نشر المنشور.',
],
];
CourseStatusEnum::wrap('draft'); // CourseStatusEnum::DRAFT
CourseStatusEnum::wrap(CourseStatusEnum::DRAFT); // CourseStatusEnum::DRAFT
CourseStatusEnum::wrap(OtherEnum::DRAFT); // CourseStatusEnum::DRAFT (matched by value)
CourseStatusEnum::wrap(null); // null
CourseStatusEnum::wrap('0'); // the case backed by '0', if any
CourseStatusEnum::wrap('invalid', strict: false); // null