PHP code example of jeromejhipolito / laravel-translation-middleware
1. Go to this page and download the library: Download jeromejhipolito/laravel-translation-middleware 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/ */
jeromejhipolito / laravel-translation-middleware example snippets
return [
// HTTP header name for locale detection
'header' => env('TRANSLATION_HEADER', 'Accept-Language'),
// Default locale when no header is provided
'default' => env('TRANSLATION_DEFAULT', 'en'),
// Supported locales (empty array = accept all)
'supported_locales' => ['en', 'ja', 'ko', 'zh', 'es', 'fr', 'de', 'pt', 'it', 'ru'],
// Fallback when translation not available
'fallback' => env('TRANSLATION_FALLBACK', 'en'),
// Column suffix for translations (e.g., title_translations)
'column_suffix' => '_translations',
// Parse 'en-US' to 'en' (true) or keep full code (false)
'parse_language_only' => true,
// Routes to skip locale detection
'excluded_routes' => [],
];
use JeromeJHipolito\TranslationMiddleware\Middleware\SetLocaleMiddleware;
->withMiddleware(function (Middleware $middleware) {
$middleware->api(append: [
SetLocaleMiddleware::class,
]);
})
use Illuminate\Database\Eloquent\Model;
use JeromeJHipolito\TranslationMiddleware\Traits\HasTranslations;
class Article extends Model
{
use HasTranslations;
protected $fillable = [
'title',
'content',
'title_translations',
'content_translations',
];
protected $casts = [
'title_translations' => 'array',
'content_translations' => 'array',
];
// Define which attributes are translatable
protected array $translatable = ['title', 'content'];
}
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('title'); // Default value
$table->json('title_translations')->nullable(); // Translations
$table->text('content');
$table->json('content_translations')->nullable();
$table->timestamps();
});
$article = Article::create([
'title' => 'Hello World',
'content' => 'This is the content.',
]);
// Set individual translations
$article->setTranslation('title', 'ja', 'こんにちは世界');
$article->setTranslation('title', 'ko', '안녕하세요 세계');
$article->save();
// Or set multiple at once
$article->setTranslations('title', [
'ja' => 'こんにちは世界',
'ko' => '안녕하세요 세계',
'zh' => '你好世界',
]);
$article->save();
// Uses current app locale (set by middleware)
$title = $article->getTranslation('title');
// Specify locale explicitly
$title = $article->getTranslation('title', 'ja');
// Get all translations
$translations = $article->getTranslations('title');
// ['ja' => 'こんにちは世界', 'ko' => '안녕하세요 세계']
// Check if translation exists
if ($article->hasTranslation('title', 'fr')) {
// ...
}
// Get available locales
$locales = $article->getAvailableLocales('title');
// ['ja', 'ko', 'zh']
use Illuminate\Http\Resources\Json\JsonResource;
use JeromeJHipolito\TranslationMiddleware\Traits\TranslatableResource;
class ArticleResource extends JsonResource
{
use TranslatableResource;
public function toArray($request): array
{
return [
'id' => $this->id,
'title' => $this->translated('title'), // Auto-translated
'content' => $this->translated('content'), // Auto-translated
'available_translations' => $this->availableTranslations('title'),
'created_at' => $this->created_at,
];
}
}
bash
php artisan vendor:publish --tag=translation-config