xiCO2k / laravel-vue-i18n

Allows to connect your `Laravel` Framework translation files with `Vue`.
MIT License
617 stars 51 forks source link

Issue with translation enums php langs generation #190

Closed svenvanhees closed 4 days ago

svenvanhees commented 1 week ago

I'm trying to translate enums, but keep getting stuck because the php langs generation outputs undefined. Could this be a bug with the php generation?

php_en.json

{
  "enums.undefined.undefined": "Certified",
  "enums.registrationStatus.undefined": "Finished"
}

lang/en/enums.php

<?php

return [

    \App\Enums\DiveSchool\GroupCertificationStatus::class => [
        \App\Enums\DiveSchool\GroupCertificationStatus::OPEN => 'Open',
        \App\Enums\DiveSchool\GroupCertificationStatus::PARTIALLY_CERTIFIED => 'Partially certified',
        \App\Enums\DiveSchool\GroupCertificationStatus::CERTIFIED => 'Certified',
    ],

    'registrationStatus' => [
        \App\Enums\DiveSchool\RegistrationStatus::NEW->value => 'New',
        \App\Enums\DiveSchool\RegistrationStatus::IN_PROGRESS->value => 'In progress',
        \App\Enums\DiveSchool\RegistrationStatus::FINISHED->value => 'Finished',
    ],
];

RegistrationStatus.php

<?php

namespace App\Enums\DiveSchool;

use Filament\Support\Contracts\HasLabel;
use Illuminate\Support\Facades\Lang;

enum RegistrationStatus: string implements HasLabel
{
    case NEW = 'new';
    case IN_PROGRESS = 'in_progress';
    case FINISHED = 'finished';

    public function getLabel(): ?string
    {

        $localizedStringKey ='enums.registrationStatus.'.$this->value;

        if (Lang::has($localizedStringKey)) {

            return __($localizedStringKey);
        }

        return $this->value;
    }

    public static function array(): array
    {
        $result = [];
        foreach (self::cases() as $case) {
            $result[$case->name] = $case->getLabel();
        }
        return $result;
    }
}
xiCO2k commented 1 week ago

Will investigate, thanks for that.

xiCO2k commented 1 week ago

Hey @svenvanhees can you check using this PR?

https://github.com/xiCO2k/laravel-vue-i18n/pull/191

Let me know if you need any help.

svenvanhees commented 4 days ago

@xiCO2k Perfect! Seems to be working as expected now.

xiCO2k commented 4 days ago

released a new version, thanks for reporting.