PHP code example of aegued / laravel-translations

1. Go to this page and download the library: Download aegued/laravel-translations 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/ */

    

aegued / laravel-translations example snippets


Aegued\LaravelTranslations\LaravelTranslationsServiceProvider::class,

return [

   /**
    * Default Locale || Root columns locale
    * We will use this locale if config('app.locale') translation not exist
    */
   'locale' => 'en',

   /**
    * Supported Locales e.g: ['en', 'es', 'fr']
    */
   'locales' => ['es', 'en', 'fr']

];

use Illuminate\Database\Eloquent\Model;
use Aegued\LaravelTranslations\Translatable;

class Item extends Model
{
    use Translatable;

    /**
      * The attributes that are Translatable.
      *
      * @var array
      */
    protected $translatable = [
        'name', 'color'
    ];
}

$item = new Item;
$data = array('en' => 'car', 'es' => 'coche');

$item->setTranslations('name', $data); // setTranslations($attribute, array $translations, $save = false)

// or save one translation
$item->setTranslation('name', 'en', 'car', true); // setTranslation($attribute, $locale, $value, $save = false)

// or just do
$item->name = 'car'; // note: this will save automaticaly unless it's the default locale

// This will save if (current locale == default locale OR $save = false)
$item->save();

$item = new Item::first();
// get current locale translation
$item->city
OR
$item->getTranslation('city');

// pass translation locales
$item->getTranslation('city', 'es'); // getTranslation($attribute, $language = null, $fallback = true)
$item->getTranslationsOf('name', ['es', 'en']); // getTranslationsOf($attribute, array $languages = null, $fallback = true)

$item = new Item::first();
$item->deleteTranslations(['name', 'color'], ['es', 'en']); // deleteTranslations(array $attributes, $locales = null)
bash
php artisan vendor:publish --provider="Aegued\LaravelTranslations\LaravelTranslationsServiceProvider"
bash
php artisan migrate