bitcodesa/nova-label

Create Labels for Nova Resources with different languages.

Downloads

26

Stars

0

Version

1.0.1

Create Labels for Nova Resources with different languages.

Latest Version on Packagist Total Downloads

~~Dynamically Manage Labels for Nova Resources

This document details how to dynamically manage labels for Nova resources using the bitcodesa/nova-label package.

Installation

Install the package using Composer:

composer require bitcodesa/nova-label

Usage

  1. Include ResourceLabel:

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;

    // ...
}
  1. Generate Labels:~~

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:

  • Attribute name:

Specify a different database attribute name:

Text::make(...self::attribute('name', 'fullName')); // field name: name, attribute: fullName
  • Title only:

Return only the attribute title:

Text::make(self::attribute('name', title_only: true));
  1. Handle Relationships:

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";
}

Localization

File Structure

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)
];

Create Localization Files

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.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

BitcodeSA

Author

BitcodeSA