PHP code example of rahmanramsi / laravel-translatable

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

    

rahmanramsi / laravel-translatable example snippets


return [
    'model' => Translate::class,
];

use Illuminate\Database\Eloquent\Model;
use RahmanRamsi\LaravelTranslatable\HasTranslations;

class Post extends Model
{
    use HasTranslations;

    public array $translatable = ['title'];
}

$post->title = 'hello';

public function setTranslation(string $key, string $locale, string $value)

$translations = ['en' => 'hello', 'es' => 'hola'];

$post->setTranslations('title', $translations);

$post->title;

public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true)

$post->getTranslations();

$post->translations

$post->getTranslations('title'); 
// returns ['en' => 'hello', 'es' => 'hola']

public function getTranslations(string $attributeName)

$translations = [
    'en' => 'Hello',
    'fr' => 'Bonjour',
    'de' => 'Hallo',
];

$newsItem->setTranslations('hello', $translations);
$newsItem->getTranslations('hello', ['en', 'fr']); // returns ['en' => 'Hello', 'fr' => 'Bonjour']

$translations = ['en' => 'hello', 'es' => 'hola'];
$post->name = $translations;

$post->locales(); // returns ['en', 'es']
bash
php artisan vendor:publish --tag="translatable-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="translatable-config"