PHP code example of almoayad / laratrans
1. Go to this page and download the library: Download almoayad/laratrans 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/ */
almoayad / laratrans example snippets
return [
// Storage strategies
'storage' => [
'mode' => 'single_table', // or 'dedicated_tables'
'table_prefix' => 'trans_',
'table_name' => 'laratrans_translations',
],
// Caching
'cache' => [
'enabled' => true,
'duration' => 3600, // 1 hour
'prefix' => 'laratrans_',
],
// Locales
'locales' => [
'default' => 'en',
'supported' => ['en', 'es', 'fr', 'ar'],
'fallback_locale' => 'en',
'auto_fallback' => true,
],
// Models
'models' => [
'translation' => \Almoayad\LaraTrans\Models\Translation::class,
'dedicated_translation' => \Almoayad\LaraTrans\Models\DedicatedTranslation::class,
],
// Validation
'validation' => [
'default_rules' => [
'min' => 1,
'max' => 255,
'
use Almoayad\LaraTrans\Traits\HasTranslations;
class SomeModel extends Model
{
use HasTranslations;
}
// Method 1: Bulk creation via request
$model = SomeModel::create($request->all()); // Automatically handles 'translations' array in request
// Method 2: Bulk creation directly
$model = SomeModel::create($request->except('translations'));
$translations = [
['locale' => 'ar', 'property_name' => 'name', 'value' => 'أسود'],
['locale' => 'en', 'property_name' => 'name', 'value' => 'black'],
['locale' => 'fr', 'property_name' => 'name', 'value' => 'noir'],
];
$model->updateModelTranslations($translations);
// Method 3: Individual creation
$model->setTranslation('name', 'black', 'en');
// Get translation for current locale
$translation = $model->filterTranslation('name');
// Get translation for specific locale
$translation = $model->filterTranslation('name', 'fr');
// Get all translations for current locale
$translations = $model->localeTranslation()->get();
// Check if translation exists
$exists = $model->checkTranslationExists('name', 'en');
// Query models with translations
$models = SomeModel::withTranslation('name')
->whereTranslation('name', 'black')
->get();
'validation' => [
'default_rules' => [
'min' => 1,
'max' => 255,
'
'properties' => [
'title' => [
'min' => 3,
'max' => 100,
'
// Validation happens automatically
try {
$model->setTranslation('title', 'Too short', 'en');
} catch (ValidationException $e) {
// Handle validation error
}
// Bulk translations are also validated
$model->create([
'translations' => [
['locale' => 'en', 'property_name' => 'title', 'value' => 'Valid title'],
['locale' => 'es', 'property_name' => 'title', 'value' => 'Título válido']
]
]);
use Almoayad\LaraTrans\Traits\HasTranslations;
use Almoayad\LaraTrans\Traits\TranslationCache;
class Product extends Model
{
use HasTranslations, TranslationCache;
}
'cache' => [
'enabled' => true,
'duration' => 3600, // Cache duration in seconds
'prefix' => 'laratrans_', // Cache key prefix
],
// Get cached translation for current locale
$translation = $model->getCachedTranslation('title');
// Get cached translation for specific locale
$translation = $model->getCachedTranslation('title', 'es');
// Setting translations (automatically handles cache)
$model->setTranslation('title', 'New Title', 'en');
bash
php artisan vendor:publish --provider="Almoayad\LaraTrans\LaraTransServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Almoayad\LaraTrans\LaraTransServiceProvider" --tag="config"
bash
php artisan migrate