vortgo / laravel-translate by vortgo

Translate system for laravel project
94
7
2
Package Data
Maintainer Username: vortgo
Maintainer Contact: vortgo@gmail.com (Viktor Pelykh)
Package Create Date: 2017-04-16
Package Last Update: 2017-05-27
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-30 15:09:37
Package Statistics
Total Downloads: 94
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 7
Total Watchers: 2
Total Forks: 1
Total Open Issues: 0

SensioLabsInsight

Translate entity

About package

This package is intended for adding multi language support to your models. If your application already be working and you need to add one or more aditional languages to your content in database it's be easy.

Installation

Require this package in your composer.json

$ composer reqiure vortgo/laravel-translate

Add the service provider to you config file config/app.php

Vortgo\Translate\ModelTranslateServiceProvider::class

Publish vendor

    $ php artisan vendor:publish

Run migration to create table for your translatable content

$ php artisan migrate

Add trait to your model which need to translate and setup your default language for your model

    class Category extends Model
    {
        use Translate;
        protected $defaultLocale = 'en';
    }

Usage

You can create entity with translate as usually:

        Category::create([
            'name' => 'name',
            'ru' => [
                'name' => 'название'
            ],
            'fr' => [
                'name' => 'fr name'
            ]
        ]);

For access the translate value you can use next variant:

Determine when calling

    $category->translate('fr')->name

Use your app locale

    App()->setLocale('fr')
    $category->name

Get all translations attributes for current model

$category->getTranslations('fr')

Bonuses

You can use relations for eager loader your model

    App()->setLocale('fr');
    $item = Item::with('category', 'category.rTranslate')->first();
    $item->category->name;

To array with relation

    App()->setLocale('fr');
    $item = Item::with('category', 'category.rTranslate')->first();
    $item->toArray();
    Result = [
        'id' =>1,
        'item_name' => 'name',
        'category' => [
            'name' => 'fr name'
        ]
    ];

If you want to override function toArray() use translateToArray() in your model

    public function toArray()
    {
        $array = $this->translateToArray(); //parent::toArray()

        // Your code here

        return $array;
    }