garrettmassey's avatar

Laravel Nova Translations: use resource title instead of "Resource" for actions

In Nova, when a user goes to delete or force delete a resource, the text for that action displays "Delete Resource" or "Force Delete Resource". I am trying to figure out if it's possible to use the resource's title instead of the word "Resource", so that it says, for example, "Delete User" or "Force Delete Book". I have tried using the translations file to change it, but it doesn't seem to work, and I can't find anything about it in the docs.

This is how I changed the translations:

  "Delete Resource": "Archive :resource",

and instead of displaying as "Archive User" or whatever resource title, it just says "Archive :resource"

I'm not sure if what I want to do is possible, but any tips would be great!

0 likes
1 reply
LaryAI's avatar
Level 58

In Laravel Nova, customizing the action text to include the resource's title instead of the generic "Resource" can be achieved by using the singularLabel method in your Nova resource class. This method allows you to define a custom label for the resource, which can then be used in various parts of the Nova interface, including action confirmations.

Here's how you can implement this:

  1. Define the singularLabel Method:

    In your Nova resource class (e.g., User.php or Book.php), you can define the singularLabel method to return the desired label for the resource.

    public static function singularLabel()
    {
        return 'User'; // or 'Book', depending on the resource
    }
    
  2. Update the Translation Strings:

    In your translation files, you can use the :resource placeholder to dynamically insert the resource's singular label. Make sure your translation string is set up to replace :resource with the label provided by the singularLabel method.

    For example, in your resources/lang/vendor/nova/en.json file, you can have:

    {
        "Delete :resource": "Delete :resource",
        "Force Delete :resource": "Force Delete :resource"
    }
    
  3. Ensure Proper Usage in Nova:

    Nova should automatically use the singularLabel method when generating action confirmation messages. If you have set up the translations correctly, Nova will replace :resource with the label returned by singularLabel.

By following these steps, you should be able to customize the action text to display "Delete User" or "Force Delete Book" instead of the generic "Delete Resource" or "Force Delete Resource". If you encounter any issues, ensure that your translation files are correctly set up and that the singularLabel method is returning the expected value.

Please or to participate in this conversation.