"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Enums e lang no Laravel

Enums e lang no Laravel

Published on 2024-11-07
Browse:990

Report
In a project I worked on, there was a select field that had defined values ​​that would not change. So, to list the items in this select, I decided to create an enumeration class and then describe these values. However, the project needed to support the English and Spanish languages, and the text of the select options needed to adapt to this without losing the reference to the respective enum item. In other words, if I selected the item "horse", I needed the system to know that this item is still "horse" even though it appears as "horse" or "caballo". It was to maintain the integrity of the data that I created the following structure integrating the enum and lang in the project.

What are Enums
A brief introduction. Starting with PHP version 8.1, a special class was introduced for enumerating values. In practice, this means that we can create a class whose property values ​​will be constant. An example is status, because if you leave it free, throughout the code you will probably have an "active" status, another "Active", another "Active" etc. But if you tie the status to an enum and using the enum instead of typing the string, I will be sure that anywhere in the code the value will always be the one in the enum.

enum Status
{
    case ATIVO = "ativo";
    case INATIVO = "inativo";
    case PENDENTE = "pendente";
}

Enums and lang
Since the enum is a class, I can create methods for my class normally. So, I created the display() method that will be responsible for handling the visualization of my enum value, adapting it to the language the user is using, however, without changing the original value in my enum item. In other words, for the user, the status appears "Pending", or "Pending", or "Pending"; but in the database it is only saved as "pending" - which is the original value and through it I can get my item from the enum, and from that item use the display method to show it translated to the user.
But before showing the example, an introduction to Laravel lang. lang is a folder in the project root to store translations that your project will support. Each language has a folder, and in that folder files for translation contexts. These files have an array, where the key is a representation of the word and the value is the translation into the respective language.

Enums e lang no Laravel

Configuring the display method
In the lang folder, I will create a status.php file for each language folder with the following array:

value => "Ativo",
    Status::INATIVO->value => "Inativo",
    Status::PENDENTE->value => "Pendente",
];

I correct the array values ​​to the respective translations of the respective language.
And my display method will fetch the translation of the enum item in these status.php files and return this value.

public function display(): string
{
    return trans('status.'.$this->value);
}

Example of use:

// EN
echo Status::ATIVO->display(); // Active

// PT_BR
echo Status::INATIVO->display(); // Inativo

// ES
echo Status::PENDENTE->display(); // Pediente

Conclusion
With this, I guarantee that my enum value remains constant in my code, but it adapts to the user's language using the display method without losing data integrity. It also remains flexible if I add a new language to the system. I hope you enjoyed this one and see you next time.

Release Statement This article is reproduced at: https://dev.to/eunael/enums-e-lang-no-laravel-43am?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3