thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

Translation post and page #5065

Closed MISTERPOTE closed 4 years ago

MISTERPOTE commented 4 years ago

Version information

Hi everybody, I want translate my pages and posts. The config/app.php file have this : 'locale' => 'en', 'fallback_locale' => 'en',

The config/voyager.php file have this : ` 'multilingual' => [ /*

The vendor/tcg/voyager/src/Models/Page.php file have this : `<?php

namespace TCG\Voyager\Models;

use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Auth; use TCG\Voyager\Traits\Translatable;

class Page extends Model { use Translatable;

protected $translatable = ['title', 'slug', 'body'];

/**
 * Statuses.
 */
const STATUS_ACTIVE = 'ACTIVE';
const STATUS_INACTIVE = 'INACTIVE';

/**
 * List of statuses.
 *
 * @var array
 */
public static $statuses = [self::STATUS_ACTIVE, self::STATUS_INACTIVE];

protected $guarded = [];

public function save(array $options = [])
{
    // If no author has been assigned, assign the current user's id as the author of the post
    if (!$this->author_id && Auth::user()) {
        $this->author_id = Auth::user()->getKey();
    }

    parent::save();
}

/**
 * Scope a query to only include active pages.
 *
 * @param  $query  \Illuminate\Database\Eloquent\Builder
 *
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeActive($query)
{
    return $query->where('status', static::STATUS_ACTIVE);
}

} `

The app/Page.php files have : <?php

namespace App;

use Illuminate\Database\Eloquent\Model; use TCG\Voyager\Traits\Translatable;

class Page extends Model { use Translatable; protected $translatable = ['title', 'body'];

public static function findBySlug($slug)
{
    return static::where('slug', $slug)->first();
}

}

The vendor/tcg/voyager/src/Models/Post.php file have : `<?php

namespace TCG\Voyager\Models;

use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Auth; use TCG\Voyager\Facades\Voyager; use TCG\Voyager\Traits\Resizable; use TCG\Voyager\Traits\Translatable;

class Post extends Model { use Translatable; use Resizable;

protected $translatable = ['title', 'seo_title', 'excerpt', 'body', 'slug', 'meta_description', 'meta_keywords'];

const PUBLISHED = 'PUBLISHED';

protected $guarded = [];

public function save(array $options = [])
{
    // If no author has been assigned, assign the current user's id as the author of the post
    if (!$this->author_id && Auth::user()) {
        $this->author_id = Auth::user()->getKey();
    }

    parent::save();
}

public function authorId()
{
    return $this->belongsTo(Voyager::modelClass('User'), 'author_id', 'id');
}

/**
 * Scope a query to only published scopes.
 *
 * @param \Illuminate\Database\Eloquent\Builder $query
 *
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopePublished(Builder $query)
{
    return $query->where('status', '=', static::PUBLISHED);
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function category()
{
    return $this->belongsTo(Voyager::modelClass('Category'));
}

} `

And the app/Post.php file have : `<?php

namespace App;

use Illuminate\Database\Eloquent\Model; use TCG\Voyager\Traits\Translatable;

class Post extends Model { use Translatable;

protected $translatable = ['title', 'body'];

}`

In the voyager backend, only for a post, i have the english and french version but the slug can't to be change. It is the same for all language. On the frontend, if i change the language, the header and footer menus are change but not the translation of the current page/post. If i change the current english URL for the french URL, i have a 404 page.

Thank you for your futur help.

emptynick commented 4 years ago

As the issue template suggest, please ask questions in our Slack group.

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.