2017 © Pedro Peláez
 

library laravel-translate

Translate system for laravel project

image

vortgo/laravel-translate

Translate system for laravel project

  • Saturday, May 27, 2017
  • by vortgo
  • Repository
  • 1 Watchers
  • 4 Stars
  • 43 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 2 % Grown

The README.md

SensioLabsInsight, (*1)

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., (*2)

Installation

Require this package in your composer.json, (*3)

$ composer reqiure vortgo/laravel-translate

Add the service provider to you config file config/app.php, (*4)

Vortgo\Translate\ModelTranslateServiceProvider::class

Publish vendor, (*5)

    $ php artisan vendor:publish

Run migration to create table for your translatable content, (*6)

$ php artisan migrate

Add trait to your model which need to translate and setup your default language for your model, (*7)

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

Usage

You can create entity with translate as usually:, (*8)

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

For access the translate value you can use next variant:, (*9)

Determine when calling, (*10)

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

Use your app locale, (*11)

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

Get all translations attributes for current model, (*12)

$category->getTranslations('fr')

Bonuses

You can use relations for eager loader your model, (*13)

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

To array with relation, (*14)

    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, (*15)

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

        // Your code here

        return $array;
    }

The Versions

27/05 2017

dev-master

9999999-dev

Translate system for laravel project

  Sources   Download

MIT

The Requires

 

The Development Requires

by Viktor Pelykh

27/05 2017

1.0.0

1.0.0.0

Translate system for laravel project

  Sources   Download

MIT

The Requires

 

The Development Requires

by Viktor Pelykh