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/ */
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
];
// 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();