Astrotomic / laravel-translatable

A Laravel package for multilingual models
https://docs.astrotomic.info/laravel-translatable/
MIT License
1.23k stars 157 forks source link

Unable to Publish the Package in Laravel Lumen #196

Closed hniehorster closed 3 years ago

hniehorster commented 3 years ago

Hi,

I'm trying to get this package working in Lumen. I've added this to composer ran the necessary migrations and now hit a roadblock.

Please make sure you have runphp artisan vendor:publish --provider="Astrotomic\Translatable\TranslatableServiceProvider"and that the locales configuration is defined.

Lumen doesn't come with the vendor:publish command and doesn't make it easy to setup the package. I've digged through your entire documentation and can't seem to find a help-article to get this working in Lumen.

I took the following route, borrowing the vendor:publish command from Laravel through the following repo:

https://github.com/laravelista/lumen-vendor-publish

After installing this package, and even running composer update, the command php artisan vendor:publish --tag=translatable gives the following message.

Unable to locate publishable resources.

Did I miss the instructions on how to get this working in Lumen? Or what should I do differently?

Gummibeer commented 3 years ago

Hey,

the first, official, answer: this package doesn't officially support Lumen so I can't help you here. It's ages since I've last used Lumen and even if someone gets up with a full description I don'T want to merge it as I would have to maintain it during upcoming releases.

Second, subjective, answer: don't use Lumen anymore. There's no reason to don't use Laravel as Laravels/Symfony5 router is now even faster than the Lumen/FastRoute one. Lumens update cycle also gets slower and slower as Taylor himself doesn't recommend to use Lumen anymore - don't worry, until now there's no official "Lumen is dead" notice. But because of this a lot of package developers really don't care anymore about Lumen as it's a pain to support both - primary if your package needs a database, cache or config or some nice helpers.

Nothing the less I would recommend you to simply copy https://github.com/Astrotomic/laravel-translatable/blob/master/src/config/translatable.php yourself and call configure('translatable') and register the package service provider in your lumen bootstrap. This should solve all the issues. The mentioned exception is thrown during locales loading and pops up when the translatable.locales are empty or not loaded at all.

https://github.com/Astrotomic/laravel-translatable/blob/2a2783c97778bb7f035fe67a1375b84d3b744900/src/Translatable/Locales.php#L86-L107

If you want you can describe your working Lumen procedure in this issue which should be found by others searching for Lumen support.

AD: If you want to switch to Laravel you can use Laravel Shift to migrate your Lumen app to Laravel. https://laravelshift.com/convert-lumen-to-laravel

hniehorster commented 3 years ago

@Gummibeer thanks for your prompt response. I got this to work and Lumen is returning the results. Not sure if it is working 100%. As you can see in the return array, the description in the data section remains null... and is only available in the translations section.

image
$business = Business::first();

$business->translate($locale); //Get's the right locale through a route group. 

return $this->successResponse($business, Response::HTTP_OK);

Is this related to the use of UUID's? In the migration files I linked the columns properly.

image

If you want you can describe your working Lumen procedure in this issue which should be found by others searching for Lumen support.

Will do as soon as I have this working!

Gummibeer commented 3 years ago

No, UUIDs are fine as long as all models/tables are configured properly. Is the response only $model->toArray()? Or do you called explicitly 'name' => $model->name? And so far I see you don't have a description column in your translation model? Every attribute that should be translatable has to have its own column in the translations table/model.

hniehorster commented 3 years ago

@Gummibeer thanks! Fixed that issue, I was under the impression it was more key-value storage. Now this returns the correct translation. But I'm still stuck with all the other translations in the response.

2 Questions:

  1. How do 1 remove all the other translations?
  2. How can I get this to work on a collection?

Remove the other translations:

    public function first($locale){

        $business = Business::first();

        $business->setDefaultLocale($locale);

        return $this->successResponse($business, Response::HTTP_OK);

    }

Results in:

image

Collection Example:

    public function index($locale)
    {
        $businesses = Business::paginate(env('PAGE_SIZE'));

        $businesses->setDefaultLocale($locale);

        return $this->successResponse($businesses);
    }

Results in : Method Illuminate\Database\Eloquent\Collection::setDefaultLocale does not exist.

Gummibeer commented 3 years ago

At all you shouldn't use setDefaultLocale() instead use the app locale or packages config locale. I will drop the method in v12 as it does something totally different than it's named. What it does is more enforceLocale() which I would like to have on the locales helper class instead of the model.

And removing the translations key - you could use a method, don't remember how it was named, to hide a key on runtime during array serialization - was something like makeHidden(). But I recommend api resource classes MUCH more as they explicitly describe the resource.