PHP code example of firstpoint-ch / eloquent-translatable
1. Go to this page and download the library: Download firstpoint-ch/eloquent-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/ */
firstpoint-ch / eloquent-translatable example snippets
Schema::create('products', function ($table) {
$table->id();
$table->translatable('name'); // Outputs $table->json('name');
$table->string('category'); // Will be used as a dictionary
$table->decimal('price');
$table->timestamps();
})
use FirstpointCh\Translatable\Casts\Dictionary;
use FirstpointCh\Translatable\Casts\Localized;
use FirstpointCh\Translatable\Traits\Translatable;
class Product extends Model
{
use Translatable;
protected $casts = [
// Database
'name' => Localized::class,
'description' => Localized::class,
// Dictionary:
//get value from /resources/lang/[locale].json
'category' => Dictionary::class,
// or get value from /resources/lang/[locale]/categories.php
'category' => Dictionary::class.':categories',
];
}
app()->setLocale('en');
// Set translation in the current locale
$product = Product::create(['name' => 'Product name']);
// Get translation in the current locale
echo $product->name; // Product name
// Get array in the current locale
dd($product->toArray()); // ['name' => 'Product name']
// Update the current locale
$product->update(['name' => 'New name']);
// Update a specific locale
$product->update(['name->fr' => 'Nom du produit']);
// Force a locale
echo $product->in('fr')->name; // Nom du produit
// Get raw value
dd($product->raw('name')); // ['en' => 'Product name', 'fr' => 'Nom du produit']
dd($product->in('*')->toArray()); // ['name' => ['en' => 'Product name', 'fr' => 'Nom du produit']]
/resources/lang/[locale]/[dictionary].php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.