dimsav / laravel-translatable

[Deprecated] A Laravel package for multilingual models
MIT License
1.95k stars 320 forks source link

When I change local I don't get the translation #582

Closed johnef closed 5 years ago

johnef commented 5 years ago

I used this package with L5.4 and package version 7. everything was working fine. when I upgraded to Laravel 5.8 and package version 9. no I don't see the translation when I debug the local I get 'en' but the translation coming is 'ar'.

Gummibeer commented 5 years ago

Could you please provide any more details? Code, Query, Settings and so on. Otherwise I can't help.

johnef commented 5 years ago

Yes of course Here is my model for product category

class ProductCategory extends Model {
    use Translatable;
    public $translatedAttributes = [ 'name' ];
    protected $fillable = [ 'image', 'name', 'soft_delete' ];

    public function categoryProduct() {
        return $this->hasMany( Product::class, 'product_category_id' );
    }
}

and here is the product category translations model

class ProductCategoryTranslation extends Model {
    public $timestamps = false;
    protected $fillable = [ 'name', 'product_category_id' ];
}

Now in controller

$productCategories = ProductCategory::all(); // local is English I get Arabic

Here is my translatable config

'locales' => [
    'ar',
    'en',
    /*'es' => [
        'MX', // mexican spanish
        'CO', // colombian spanish
    ],*/
],
'locale_separator' => '-',
'locale' => null,
'use_fallback' => true,
'use_property_fallback' => true,
'fallback_locale' => false,
'translation_suffix' => 'Translation',
'locale_key' => 'locale',
'to_array_always_loads_translations' => true,
johnef commented 5 years ago

@Gummibeer please if you'd like me to provide anything else let me know

Gummibeer commented 5 years ago

At all this seems all valid. Which part of the response is wrong/arabic? The attributes or do you have arabic listed in the translations relation?

johnef commented 5 years ago

@Gummibeer when I change my local to English everything return English as static translations but the translations coming from the Database not getting the English translation.

Even I try something like this with

->whereHas( 'translations', function ( $query ) {
                $query->where( 'locale', 'en' );
            } )->first()
public function menu() {
        $categories = ( new ProductCategory )
            ->where( 'soft_delete', 0 )
            ->whereHas( 'translations', function ( $query ) {
                $query->where( 'locale', 'en' );
            } )->first()
            ->with( 'categoryProduct' )
            ->whereHas( 'categoryProduct', function ( $query ) {
                $query/*->where( 'quantity', '>', 0 )*/
                ->where( 'product_category_id', '!=', null )
                ->where( 'soft_delete', 0 );
            } )
            ->withCount( 'categoryProduct' )
            ->orderBy( 'sort_categories', 'ASC' )
            ->orderBy( 'category_product_count', 'desc' )
            ->get();

        return $categories;
    }

This will return Arabic data

johnef commented 5 years ago

@Gummibeer My php version 7.1.9

dimsav commented 5 years ago

As announced here, the project has been deprecated. Please use astrotomic/laravel-translatable.