1. Go to this page and download the library: Download levgenij/translatable 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/ */
levgenij / translatable example snippets
use Levgenij\LaravelTranslatable\Translatable;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use Translatable;
}
Post::first();
$post->title; // title in the current locale
Post::translateInto('de')->first();
$post->title; // title in 'de' locale
Post::translateInto('de')->withFallback('en')->first();
$post->title; // title in 'de' if available, otherwise in 'en'
// ❌ May not work correctly - JOIN with translations affects the count comparison
Product::query()
->whereHas('categories', function ($query) use ($categoryIds) {
$query->whereIn('id', $categoryIds);
}, '=', count($categoryIds))
->get();
// ✅ Correct approach - disable translations for complex queries
Product::query()
->whereHas('categories', function ($query) use ($categoryIds) {
$query->withoutTranslations()->whereIn('categories.id', $categoryIds);
}, '=', count($categoryIds))
->get();
'providers' => [
// Other providers
Levgenij\LaravelTranslatable\TranslatableServiceProvider::class,
],
TranslatableConfig::currentLocaleGetter(function() {
// Return the current locale of the application
});
TranslatableConfig::fallbackLocaleGetter(function() {
// Return the fallback locale of the application
});
$user = $user->first();
foreach ($user->translations as $translation) {
echo "Title in {$translation->locale}: {$translation->title}";
}
$user = $user->first();
$user->translate('en')->title; // Title in EN
$user->translate('de')->title; // Title in DE
User::withAllTranslations()->get();
use Levgenij\FilamentTranslatable\Concerns\TranslatableResource;
use Levgenij\FilamentTranslatable\Concerns\HasTranslatableFields;
class CategoryResource extends Resource
{
use TranslatableResource;
// ... your form schema with translatable fields works automatically
}
class CreateCategory extends CreateRecord
{
use HasTranslatableFields;
}
class EditCategory extends EditRecord
{
use HasTranslatableFields;
}