Astrotomic / laravel-translatable

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

Why data returned with astrotomic/laravel-translatable have invalid language? #316

Closed PetroGromovo closed 1 year ago

PetroGromovo commented 1 year ago

On laravel 9 site I added astrotomic/laravel-translatable and getting set of data I see a bit different results I expected As I have 2 languages defined in config/translatable.php with default 'en' :

'locales' => [
    'en',
    'fr',
...
    'locale' => 'en',
...

I do request with 'fr' locale:

$banners           = Banner
    ::translatedIn(app()->getLocale())
    ->get(function ($banner);

I check logs :

            [id] => 5
            [text] => laravel library site
            [description] => laravel is a powerful php library
            ...
            [translations] => Array
                (
                    [0] => Array
                        (
                            [id] => 13
                            [text] => laravel library site
                            [description] => laravel is a powerful php library
                            ...
                        )

                    [1] => Array
                        (
                            [id] => 15
                            [text] => site de la bibliothèque laravel
                            [description] => laravel est une puissante bibliothèque php
                            ...
                        )

                )

)

Text in text, description fields(of banner, not translations] subarray...) are in ‘en’, not in 'fr', as I expected...

In file app/Models/Banner.php I have :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Astrotomic\Translatable\Translatable;

class Banner extends Model implements HasMedia, TranslatableContract
{
    use InteractsWithMedia;
    use Translatable;

    protected $table = 'banners';
    protected $primaryKey = 'id';
    public $timestamps = false;

    protected $fillable = [/*'text', 'description',*/ 'url', 'active', 'ordering', 'banner_bgimage_id', 'updated_at'];
    public $translatedAttributes = ['text', 'description'];

        "laravel/framework": "^9.19",
        "astrotomic/laravel-translatable": "^11.11",

and in app/Models/BannerTranslation.php :

<?php
namespace App\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

class BannerTranslation extends Model
{
    public $timestamps = false;
    protected $fillable = ['text', 'description', 'updated_at'];
}

Seems I did not miss any config options, but what is wrong ?

"astrotomic/laravel-translatable": "^11.11",
"laravel/framework": "^9.19",

Thanks in advance!

PetroGromovo commented 1 year ago

Looking at this docs : https://docs.astrotomic.info/laravel-translatable/package/scopes#translatedin-string-usdlocale-null

I see some examples, like : Post::translatedIn('en')->get(); and $post = Post::first(); But example :


 $banner = Banner::first();
 $bannerText = $banner->translate('fr')->text;

```also returns default value in ‘en’, not 'fr'

In my model         
app/Models/Banner.php

I added :
    protected $translationForeignKey = 'banner_id';

But it did not help and I got all text value in default  ‘en’.
I suppose I misconfigure something in my model definitions, but what ?
PetroGromovo commented 1 year ago

1) I found some strange for me decision as to work properly I need in file config/translatable.php to set 'locale' => null,

and with this option then getting data as

        $bannersInFrench = Banner
            ::translatedIn('fr')
            ->orderBy('id', 'asc')
            ->get();

I got french labels.

2) Why I got ‘translations’ subarray in resulting list of data ? That is not really what I need...

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days