Reputation: 2185
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 ?
ADDITIVE EXAMPLES:
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 ?
"astrotomic/laravel-translatable": "^11.11",
"laravel/framework": "^9.19",
Thanks in advance!
Upvotes: 0
Views: 1282
Reputation: 2185
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.
Upvotes: 1