What are some good strategies for creating localization files for trans () in Laravel 5.x?

This is a question about the methodology and proposed practice. I know that it is strictly not tied to the structure and not even PHP, and the answer may be "up to you". But my concern is with best practices and methodology, since there is usually a better approach to a particular context.

I would like to know which of the best methods for assigning key names to the trans() function of Laravel 5.1?

Given that Laravel's built-in translations are stored in an array, my concern is which administrator allows me to achieve the following goals:

Consistency Therefore, I minimize the possibility of using different words for the same meaning or creating many different keys that ultimately have the same translation (for example, common words).

reusability . Therefore, I minimize the size of the translation files and translate as little as possible and retain flexibility.

readability : translators can identify the purpose of a key even if there is no translation value.

organization . Thus, the developer can easily remember the full key path and each time minimize the scan of translation files.


To give an example, let's say I want to name a successful alert to update a user model for a management profile. Possible approaches:

 trans('manager.user.update.alert.sucess') trans('alerts.success.manager.user.update') trans('manager.user.alert.update.success') trans('alert.the_user_was_updated_successfully') 

Update

By November 2016, it looks like Laravel 5.4 introduces a JSON-based translation engine that can simplify translation files. Nevertheless, caring for a smart file structure and well-organized texts is a plus.

+1
php laravel laravel-5 translation laravel-localization
source share
1 answer

My suggestion would be to use the parameterized translation parameter in Laravel.

I would suggest creating such a structure:

For content that is shared and reusable:

 trans('messages.alerts.update.success', ['item' => 'User']); // results in: 'User has successfully been updated' trans('messages.alerts.update.success.default'); // results in: 'Updated was successfull.' 

For content strictly related to a specific area / problem ... (manager in this case):

 trans('manager.alerts.update.user.success'); // results in: 'User has successfully been updated' 

or

 trans('manager.alerts.update.success', ['item' => 'User']); // results in: 'User has successfully been updated' trans('manager.alerts.update.success.default'); // results in: 'Updated was successfull.' 

The idea is that for what is characteristic of the manager as having an update-successl message, which is likely to be different from other update-success messages, you should start with something of a specific type: manager.alerts... .. In general cases (where the same message can be used in several use cases). You should start with something general like messages.alerts.update...

Calling, for example, trans('alert.the_user_was_updated_successfully') should be avoided since you might have a BIG problem when you want to change the message. The key will still reflect the old value, while the value will be new.

Regarding your goals:

consistency and reuse . Some content will be duplicated. This cannot be avoided. However, this problem can be minimized by structuring the content and having a file with common words and phrases, for example. commons.words commons.phrases or 2 files (words and phrases) with several categories. Example: commons.time.day , commons.hello_world ...

readability . This will be a problem if you do not give the translator a file that already has all its meanings (in the default language or at the beginning from which it can translate). I really don't understand why you will not have the initial translation / content.

organization . You should try to think in the same way as with the developer. If you are trying to find something specific, you will understand and try to find something under that particular subject ( manager.alerts.... in this case) but if you are looking for something more general, you are most likely to search something in common ( messages.alerts.... in this case)

I have a similar problem and you sent a question about this. Unfortunately, there was also little interest in this issue.

+1
source share

All Articles