-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Working with variable translation keys #20
Comments
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. |
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:
or with the interpolated syntax (notice both
Since GetText will convert 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 Is this solution what you were looking for? |
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. In theory, assuming there would be a PR with tests, what there be some reasons not to integrate it? |
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'),
];
It's also the case with the solution (2), or maybe I'm missing something.
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. |
Hello,
I'm wondering if there is a way to register translation keys that come from other sources rather than being hardcoded.
Example use case is having a set of data (e.g. API or local constants), which need to be translated, but they would need to be changed over time.
I would envision a need for a separate scanning tool, that would require a script to be fully functional and therefore would be able to read variables/constants runtime.
Use example:
If one wants to change the value of
ROLE_ADMIN
to let's say, administrator, it would need to be done in just one place.In theory, is this something that you would be eager to have as a part of your package?
The text was updated successfully, but these errors were encountered: