Comments (4)
Another solution would be creating non-runnable scannable files that use those enums.
return [
'enums' => [
'user-roles' => [
User::ROLE_ADMIN => p('enums.user-roles', 'admin')
]
],
];
And then creating a function to access those runtime.
Any thoughts on the subject are appreciated :)
from laravel.
Maybe it should be better explained in the docs, but if you have a definite set of enums to translate, you can just put these enums somewhere in the code (maybe in your model?) and they will be parsed by GetText:
// User.php
private const I18N_ROLES = [
t('admin'),
t('manager'),
t('customer'),
];
Then, later, you can decide to call GetText directly with your role variable like this:
t($user->role)
or with the interpolated syntax (notice both t
):
t("Your role is %s", t($user->role));
Since GetText will convert $user->role
in runtime to one of "admin", "manager" or "customer", and since it already knows that these 3 words exist and have been localized, it will return the correct translation.
This method also works for an outside set of data (API etc.). Just create a small script that will reach the API and generate a dummy PHP file in your project directory that will contain all the words/sentences to translate. And then, execute this script
each time just before syncing.
Is this solution what you were looking for?
from laravel.
Thank you for your input! I think as a generic solution, it works very well with specifying the keys separately.
As for scannable files, how does the concept sound like in general?
What value that brings is that if code ever gets refactored (e.g. constants removed, values changes), it would automatically update/remove the translation keys. While using an IDE, it would be also possible to track whether a particular constant is translated.
Search usages of a constant -> runnable-file.php -> See also translation parameters and context.
In theory, assuming there would be a PR with tests, what there be some reasons not to integrate it?
Could, of course implement some own standalone solution, but I'd find very convenient to have in one place.
from laravel.
As for scannable files, how does the concept sound like in general?
I'm sorry but I'm not really sure I understand what your solution would bring.
Your suggested scannable file (1) would be very similar to what I'm proposing (2) and both would be scanned and processed by GetText in the same way.
// (1)
return [
'enums' => [
'user-roles' => [
User::ROLE_ADMIN => p('enums.user-roles', 'admin'),
User::ROLE_MANAGER => p('enums.user-roles', 'manager'),
User::ROLE_CUSTOMER => p('enums.user-roles', 'customer'),
]
],
];
// (2)
private const I18N_ROLES = [
p('enums.user-roles', 'admin'),
p('enums.user-roles', 'manager'),
p('enums.user-roles', 'customer'),
];
What value that brings is that if code ever gets refactored (e.g. constants removed, values changes), it would automatically update/remove the translation keys. While using an IDE, it would be also possible to track whether a particular constant is translated.
It's also the case with the solution (2), or maybe I'm missing something.
In theory, assuming there would be a PR with tests, what there be some reasons not to integrate it?
Could, of course implement some own standalone solution, but I'd find very convenient to have in one place
I don't see any reason not to integrate it, but I'm not 100% convinced it would bring something more than what already exists.
from laravel.
Related Issues (15)
- Laravel 6 support HOT 1
- Add error message if translation.php file is missing
- Support for Laravel 7.6.2 HOT 1
- Support for Laravel 8 HOT 12
- File that is mentioned in 'ignored_key_prefixes' is removed after sync HOT 3
- Folder "app" doest not exists. Gettext scan aborted. HOT 7
- Strings inside blade attributes not recognized HOT 1
- php8 issue HOT 5
- Not issue possible config settings HOT 1
- Support for multiple JSON files HOT 18
- php artisan translation:init suceeds, php artisan translation:sync tells me to init first HOT 3
- How do I add new segments to a pre-existing project? HOT 1
- Laravel 9 upgrade HOT 3
- Creation of dynamic property Tio\Laravel\GettextTranslationSaver::$config is deprecated HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel.