GithubHelp home page GithubHelp logo

Comments (4)

sybbear avatar sybbear commented on June 15, 2024

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.

MichaelHoste avatar MichaelHoste commented on June 15, 2024

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.

sybbear avatar sybbear commented on June 15, 2024

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.

MichaelHoste avatar MichaelHoste commented on June 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.