PHP code example of wecansync / laravel-translations

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

    

wecansync / laravel-translations example snippets


Schema::create('languages', function (Blueprint $table) {
    $table->id();
    $table->string('title'); // e.g., "English"
    $table->string('code');  // e.g., "en"
    $table->timestamps();
});

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('sku')->unique();
    $table->decimal('price', 10, 2);
    // ... other non-translatable fields
    $table->timestamps();
});

Schema::create('products_translations', function (Blueprint $table) {
    $table->id();
    $table->foreignId('language_id')->constrained('languages')->cascadeOnDelete();
    $table->foreignId('product_id')->constrained('products')->cascadeOnDelete();
    $table->string('locale')->nullable(); // Auto-filled
    $table->string('name');               // Translatable field
    $table->string('slug')->nullable();   // Auto-generated
    $table->text('description')->nullable();
    $table->timestamps();

    $table->unique(['language_id', 'product_id']);
});

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use EbrahimHanna\TranslationManager\Traits\HasTranslations;

class Product extends Model
{
    use HasTranslations;

    protected $fillable = ['sku', 'price', 'stock'];

    protected $translation_model = [
        'model' => ProductTranslation::class,
        'owner_key' => 'product_id',
        'slug' => 'name', // Auto-generate slug from 'name' field
    ];
}

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ProductTranslation extends Model
{
    protected $fillable = [
        'product_id',
        'language_id',
        'locale',
        'name',
        'slug',
        'description',
    ];
}

// The trait automatically saves translations from the request
Product::create([
    'sku' => 'PROD-001',
    'price' => 99.99,
]);

// Request should contain:
// translations[1][name] = "English Product Name"
// translations[1][description] = "English description"
// translations[2][name] = "اسم المنتج بالعربية"
// translations[2][description] = "وصف باللغة العربية"

$product = Product::find(1);

// Get specific field translation
$englishName = $product->getTranslation(1, 'name');
// or by language code
$arabicName = $product->getTranslation('ar', 'name', 'code');

// Get all translations for a language
$translation = $product->getTranslationsByLanguage(1);
echo $translation->name;
echo $translation->description;
echo $translation->slug; // auto-generated

// Get all translations
$allTranslations = $product->getAllTranslations;

protected $translation_model = [
    // Required
    'model' => ProductTranslation::class,

    // Optional - Override global config
    'foreign_key' => 'language_id',              // FK to languages table
    'owner_key' => 'product_id',                 // FK to parent model
    'translations_data_key' => 'translations',   // Request array key
    'locale_column' => 'locale',                 // Column for language code
    'language_code_column' => 'code',            // Languages table code column

    // Optional - Advanced features
    'slug' => 'name',                            // Auto-generate slug from field
    'skip_routes' => ['products.update'],        // Skip auto-save on routes
];

return [
    'language_model' => 'App\Models\Language',
    'foreign_key' => 'language_id',
    'owner_key' => 'model_id',
    'translations_data_key' => 'translations',
    'language_code_column' => 'code',
    'locale_column' => 'locale', // Set to null to disable
];

// Create product and manually add translations
$product = Product::create([
    'sku' => 'PROD-001',
    'price' => 99.99,
]);

$product->withTranslations([
    1 => ['name' => 'English Name', 'description' => 'English description'],
    2 => ['name' => 'اسم عربي', 'description' => 'وصف عربي'],
]);

// Method chaining
Product::create($data)->withTranslations($translations);

// Delete all translations for a specific language
$product->clearTranslations(1); // By language ID
$product->clearTranslations('en', 'code'); // By language code

// Deleting the model cascades to translations
$product->delete(); // All translations are auto-deleted

// Get single field translation
$name = $product->getTranslation(1, 'name');
$name = $product->getTranslation('en', 'name', 'code');

// Get all fields for a language (returns model or null)
$translation = $product->getTranslationsByLanguage(1);
$translation = $product->getTranslationsByLanguage('en', 'code');

// Get all translations (returns HasMany relationship)
$allTranslations = $product->getAllTranslations;

// Access via Eloquent relationship
$product->translationRelation()->where('locale', 'en')->get();

protected $translation_model = [
    'model' => ProductTranslation::class,
    'owner_key' => 'product_id',
    'slug' => 'name',
    'locale_column' => 'locale',
];

protected $translation_model = [
    'model' => PostTranslation::class,
    'owner_key' => 'post_id',
    'slug' => 'title',
];

protected $translation_model = [
    'model' => CategoryTranslation::class,
    'owner_key' => 'category_id',
    'skip_routes' => ['categories.quick-update'], // Don't auto-save here
];
bash
php artisan vendor:publish --provider="EbrahimHanna\TranslationManager\PackageServiceProvider"