PHP code example of solutionforest / laravel-translatable

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

    

solutionforest / laravel-translatable example snippets


$newsItem = new NewsItem; // This is an Eloquent model
$newsItem
   ->setTranslation('name', 'en', 'Name in English')
   ->setTranslation('name', 'nl', 'Naam in het Nederlands')
   ->save();
   
$newsItem->name; // Returns 'Name in English' given that the current app locale is 'en'
$newsItem->getTranslation('name', 'nl'); // returns 'Naam in het Nederlands'

app()->setLocale('nl');

$newsItem->name; // Returns 'Naam in het Nederlands'

$newsItem->name;

public function getTranslation(string $attributeName, string $locale) : string

$newsItem->getTranslations();

$yourModel->translations

$newsItem->name = 'New translation';

$newsItem->setTranslation('name', 'en', 'Updated name in English');

$newsItem->save();

/** @var \Illuminate\Database\Eloquent\Model */
public $model;

/** @var string  */
public $attributeName;

/** @var string  */
public $locale;

public $oldValue;
public $newValue;

NewsItem::create([
   'name' => [
      'en' => 'Name in English',
      'nl' => 'Naam in het Nederlands'
   ],
]);
 php
public function forgetAllTranslations(string $locale)
 php
public function getTranslations(string $attributeName): array
 php
public function setTranslations(string $attributeName, array $translations)
 php
$translations = [
   'en' => 'Name in English',
   'nl' => 'Naam in het Nederlands'
];

$newsItem->setTranslations('name', $translations);