PHP code example of laravelarab / tarjama

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

    

laravelarab / tarjama example snippets


LaravelArab\Tarjama\TarjamaServiceProvider::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', 'fr', 'ar']
    */
   'locales' => ['ar', 'en', 'fr']

];

use Illuminate\Database\Eloquent\Model;
use LaravelArab\Tarjama\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', 'ar' => 'سيارة');

$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', 'ar'); // getTranslation($attribute, $language = null, $fallback = true)
$item->getTranslationsOf('name', ['ar', 'en']); // getTranslationsOf($attribute, array $languages = null, $fallback = true)

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