question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unable to use class constants as translation keys

See original GitHub issue

Hi,

I’d like to use this loader to prepare language strings for vue, but I’m using BenSampo/laravel-enum package, and my translation arrays for enums looks like this:

<?php

use App\Enums\OfferStatus;

return [
    OfferStatus::class => [
        OfferStatus::New => 'new',
        OfferStatus::Closed => 'closed',
    ],
];

laravel-translation-loader is unfortunately unable to parse such php file. I get the following error:

 error  in ./node_modules/@kirschbaum-development/laravel-translations-loader/all.js

Module build failed: Error: Unexpected PHP key: "staticlookup", details: {"kind":"staticlookup","loc":{"source":null,"start":{"line":3,"column":28,"offset":32},"end":{"line":3,"column":35,"offset":39}},"what":{"kind":"identifier","loc":{"source":null,"start":{"line":3,"column":4,"offset":8},"end":{"line":3,"column":28,"offset":32}},"resolution":"uqn","name":"OfferStatus"},"offset":{"kind":"constref","loc":{"source":null,"start":{"line":3,"column":30,"offset":34},"end":{"line":3,"column":35,"offset":39}},"name":"class"}}
...

would it be possible to implement lookup for class constants?

A workaround was to rewrite translation array like this:

<?php

return [
    'App\Enums\OfferStatus' => [
        1 => 'new',
        2 => 'closed',
    ],
];

but that’s not very elegant…

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
slanocommented, Nov 12, 2018

I guess a major functionality would have to be implemented into php-array-parser, I’ll just keep using a workaround for now, it gets the job done.

0reactions
luisdalmolincommented, Apr 30, 2021

Yes, this is still a thing. To make this work we basically would have to run php file.php and process the result. It may not be so bad, but definitely introduces a few potential problems. I may look into this when I have some time, but feel free to send PRs for this. This would not need any updates on the php-array-parser side, it would be work done before that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - unable to use class constants as translation keys -
Hi,. I'd like to use this loader to prepare language strings for vue, but I'm using BenSampo/laravel-enum package, and my translation arrays for...
Read more >
Translations for constants files in react - Stack Overflow
If you have translation file with key - value translation format you can use it. So you have constants file like this:
Read more >
Using Enums for Translations keys : r/PHP - Reddit
Since Enums cannot be used as array keys, the same ideea could be done with a regular class with string constants
Read more >
How to translate words in the component(.ts) file?? · Issue #835
At present I am using the translation in ".html" file. But, now I have to use the translation in "Component(.ts)" file as well....
Read more >
Everything You Need to Know About Ruby Constants
Notice that you can't define constants inside a method. ... You can then access these constants inside the class methods or outside the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found