PHP code example of ricardosierra / translation

1. Go to this page and download the library: Download ricardosierra/translation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ricardosierra / translation example snippets



    
use Translation\Contracts\Translation;
use App\Http\Controllers\Controller;
use App\Http\Blog;

class BlogController extends Controller
{
    /**
     * @var Translation
     */
    protected $translation;
    
    /**
     * Constructor.
     *
     * @param Translation $translation
     */
    public function __construct(Translation $translation)
    {
        $this->translation = $translation;
    }
    
    /**
     * Returns all blog entries.
     *
     * @return Illuminate\View\View
     */
    public function index(Request $request)
    {
        $title = $this->translation->translate('My Blog');
        
        $entries = Blog::all();
        
        return view('pages.blog.index', compact('title', 'entries'));
    }
}

    
    use Translation\Traits\LocaleTrait;
    use Illuminate\Database\Eloquent\Model;
    
    class Locale extends Model
    {
        use LocaleTrait;
    
        /**
         * The locales table.
         *
         * @var string
         */
        protected $table = 'locales';
    
        /**
         * The fillable locale attributes.
         *
         * @var array
         */
        protected $fillable = [
            'code',
            'lang_code',
            'name',
            'display_name',
        ];
    
        /**
         * {@inheritdoc]
         */
        public function translations()
        {
            return $this->hasMany(Translation::class);
        }
    }


    use Translation\Observers\HasTranslations;
    use Illuminate\Database\Eloquent\Model;
    
    class Translation extends Model
    {
        use TranslationTrait;
    
        /**
         * The locale translations table.
         *
         * @var string
         */
        protected $table = 'translations';
    
        /**
         * The fillable locale translation attributes.
         *
         * @var array
         */
        protected $fillable = [
            'locale_id',
            'translation_id',
            'translation',
        ];
    
        /**
         * {@inheritdoc}
         */
        public function locale()
        {
            return $this->belongsTo(Locale::class);
        }
    
        /**
         * {@inheritdoc}
         */
        public function parent()
        {
            return $this->belongsTo(self::class);
        }
    }

     
    [
    /*
    |--------------------------------------------------------------------------
    | Locale Model
    |--------------------------------------------------------------------------
    |
    |  The locale model is used for storing locales such as `en` or `fr`.
    |
    */

    'locale' => App\Models\Locale::class,


    /*
    |--------------------------------------------------------------------------
    | Translation Model
    |--------------------------------------------------------------------------
    |
    |  The translation model is used for storing translations.
    |
    */

    'translation' => App\Models\Translation::class,
    ];

     
    Route::group(['prefix' => Translation::getRoutePrefix(), 'middleware' => ['locale']], function()
    {
        Route::get('home', function ()
        {
            return view('home');
        });
    });

     
    session("locale", 'fr');