Downloads
Stars
Version
~~Dynamically Manage Labels for Nova Resources
This document details how to dynamically manage labels for Nova resources using the bitcodesa/nova-label
package.
Install the package using Composer:
composer require bitcodesa/nova-label
In App/Nova/Resource.php
, extend the NovaResource
class and add the ResourceLabel
trait:
<?php
namespace App\Nova;
use Bitcodesa\NovaLabel\ResourceLabel;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Resource as NovaResource;
abstract class Resource extends NovaResource
{
use ResourceLabel;
// ...
}
Use the self::attribute()
method to generate field labels. This method handles both field name and database attribute:
Text::make(...self::attribute('name')); // field name and attribute same
Optional Parameters:
Specify a different database attribute name:
Text::make(...self::attribute('name', 'fullName')); // field name: name, attribute: fullName
Return only the attribute title:
Text::make(self::attribute('name', title_only: true));
For relationship fields, pass the corresponding resource class as the first parameter to self::relation()
:
BelongsTo::make(...self::relation(\App\Nova\User::class, many: false)); // One-to-one relationship
Relationship Label Customization:
Similar to field labels, you can customize relationship labels with title
and relation
parameters:
HasMany::make(...self::relation(\App\Nova\Task::class, title: "Tasks", relation: "tasks"));
Change File Name:
you can change file name for any resource by override getLangPath()
function:
public static function getLangPath()
{
return "Users";
}
Change Resource Name:
you can change file name for any resource by override getLangName()
function:
public static function getLangName()
{
return "Admin";
}
Each resource has a dedicated localization file for field and other translations. The file structure should follow:
<?php
return [
// Resource name in singular and plural form
"resource" => "Resource",
"resources" => "Resources",
// Button labels
"buttons" => [
"create" => "Create Resource",
"update" => "Update Resource",
],
// Attributes
"attributes" => [
// Translate each attribute name
"created_at" => __("created_at"),
// ...
],
// Additional sections (optional)
];
To create a new localization file for a specific resource and language:
php artisan make:label ResourceName LanguageSample
For example, to create an Arabic translation file for the Book
resource:
php artisan make:label Book ar
This command generates a file at Lang/ar/Book.php
. Translate each line in the file according to your needs.
Note: Run php artisan migrate
before creating the localization file to ensure all column names are available for
translation.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.