Retrieving the translations from rainlab translate using API

77 Views Asked by At

I'm having some issues with getting localized content from API In October CMS. Currently we have rainlab translate plugin and we need to set up routes to use in VUE to get localized content but I’m having issues getting the translated content.

Here are the locales we have set:

Localizations I have set up

And here I’m trying to access them but it always defaults to default locale and I cannot access Latvian translations (lv). Example shows one field getting but at the end I would like for it to get all posts and get all fields in that locale but as a test its only the one field.

use RainLab\Translate\Models\Locale;
use RainLab\Translate\Classes\Translator;

Route::get('locale-test', function() {
    
    $user = Blog::first();
    $user->body;
    $user->translateContext('lv');

    return response()->json($user, 200, [], JSON_PRETTY_PRINT);
});

For a model I have set all fields as translatable using this .

public $implement = [
        \RainLab\Translate\Behaviors\TranslatableModel::class
    ];

    public $translatable = [];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        // Get all fields from the database table
        $tableColumns = $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());

        // Exclude non-translatable fields, such as primary key, timestamps, etc.
        $translatableFields = array_diff($tableColumns, ['id', 'created_at', 'updated_at']);

        // Assign translatable fields
        $this->translatable = $translatableFields;
    }
0

There are 0 best solutions below