PHP code example of dejodev / laravel-multi-language-strings

1. Go to this page and download the library: Download dejodev/laravel-multi-language-strings 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/ */

    

dejodev / laravel-multi-language-strings example snippets


    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->json('productName'); // Add this line
            $table->timestamps();
        });
    }



namespace App\Models;

use DeJoDev\MultiLanguageStrings\MultiLanguageString;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $casts = [
        'productName' => MultiLanguageString::class,
    ];
}

$product = new Product();

// Set the value for the current locale
$product->productName = 'Product name';

// Or use the set method
$product->productName->set('Product name');

// Set the value for a specific locale
$product->productName->set('Product name', 'nl'); 

// Set the value for all locales a once. (all existing locales will be cleared)
$product->productName->set([
    'en' => 'Product name',
    'nl' => 'Product naam',
]);

// Or set another MultiLanguageString as value
$product->productName = MultiLanguageString::create([
    'en' => 'Product name',
    'nl' => 'Product naam',
]);

// Unset the value for a specific locale
$product->productName->unset('nl');



$product = Product::find(1);

// Get the value for the current locale, if the value is not set for the
// current locale the value for the fallback locale will be returned.
$product->productName->get();

// Get the value for a specific locale
$product->productName->get('nl');

// Multilanguage strings implements Stringable and can be cast to a string.
$s = (string) $product->productName; 
print($product->productName); 

// Alias for get();
$product->productName->toString();


// Create a new `MultiLanguageString` instance.
$mls = MultiLanguageString::create([
    'en' => 'Hello World!',
    'nl' => 'Hallo Wereld!',
]);

// Create a new `MultiLanguageString` instance from a JSON string.
$mls = MultiLanguageString::fromJson('{"en":"Hello World!","nl":"Hallo Wereld!"}');

// Convert the `MultiLanguageString` instance to a JSON string.
$json = $mls->toJson();

// Set fallback option explicit upon instance creation().
$mls = MultiLanguageString::create([
    'en' => 'Hello World!',
    'nl' => 'Hallo Wereld!',
], useFallbackLocale: false);

// Set fallback option on existing instance.
$mls->setUseFallbackLocale(false);

// Get fallback option on existing instance. 
$default = $mls->getUseFallbackLocale();

// Set the default value for the `useFallbackLocale` property.
// This will be used for all new instances.
MultiLanguageString::setUseFallBackLocaleDefault(false);

// Get the default value for the `useFallbackLocale` property.
$default = MultiLanguageString::getUseFallBackLocaleDefault();

// Gets the locale that is used to get the value, using the fallback locale when enabled.
$locale = $mls->getUsedLocale(): string;
$locale = $mls->getUsedLocale('nl'): string;

// Gets an array of the locales stored in the MultiLanguageString.
$locales = $mls->getLocales(); // e.g. ['en', 'nl']