Installation:
composer require derafu/translation
Add the service provider to config/app.php:
'providers' => [
// ...
Derafu\Translation\TranslationServiceProvider::class,
],
Basic Usage: Load translations via the facade:
use Derafu\Translation\Facades\Translation;
// Load a translation file (e.g., `resources/lang/en/messages.php`)
Translation::load('en', 'messages', require base_path('resources/lang/en/messages.php'));
// Translate a key
$translated = Translation::get('messages.welcome');
First Use Case:
$greeting = Translation::get('messages.greeting', ['name' => 'John']);
return view('welcome', compact('greeting'));
File-Based Translations:
resources/lang/{locale}/ (e.g., en/messages.php).return [
'welcome' => 'Welcome, :name!',
'errors' => [
'invalid' => 'The :attribute is invalid.',
],
];
Dynamic Loading:
$customTranslations = $this->fetchTranslationsFromDB();
Translation::load('en', 'custom', $customTranslations);
Fallback Chains:
Translation::setFallbacks(['en', 'es']);
Translation::get('messages.welcome'); // Falls back to 'en' if 'es' key is missing.
View Integration:
@lang directive in Blade:
@lang('messages.welcome', ['name' => $user->name])
Exception Handling:
Translation::enableExceptions();
Translation::get('nonexistent.key'); // Throws Derafu\Translation\TranslationException.
laravel-localization for route-based locale switching.Translation::load('en', 'validation', [
'required' => 'The :attribute field is required.',
]);
Translation::fake()->setTranslations('en', ['test' => 'Mocked']);
$this->assertEquals('Mocked', Translation::get('test'));
Missing Files:
resources/lang/{locale}/ doesn’t exist, create it or ensure files are loaded programmatically.Translation::load() explicitly for custom paths.Caching:
Cache::remember('translations-en', 60, function () {
return Translation::getAll();
});
Locale Switching:
app()->setLocale('es');
Translation::load('es', 'messages', ...);
Exception Overhead:
Translation::enableExceptions()) adds overhead. Disable in production:
if (app()->environment('production')) {
Translation::disableExceptions();
}
Missing Keys:
Translation::getAll() to inspect loaded translations.Fallback Issues:
Translation::setFallbacks(['en']);
Translation::get('es.messages.welcome'); // Falls back to 'en.messages.welcome'.
Custom Loaders:
Derafu\Translation\Loaders\LoaderInterface to support non-file sources (e.g., databases):
class DatabaseLoader implements LoaderInterface {
public function load(string $locale, string $group, array $translations) {
// Save to DB...
}
}
Macros:
Translation::macro('plural', function ($key, $count, array $replace = []) {
$key = $count === 1 ? "{$key}_singular" : "{$key}_plural";
return Translation::get($key, $replace);
});
Event Listeners:
TranslationLoaded):
Translation::addListener('TranslationLoaded', function ($locale, $group) {
Log::info("Loaded translations for {$locale}.{$group}");
});
Locale-Specific Logic:
public function handle($request, Closure $next) {
$locale = $request->segment(1);
Translation::load($locale, 'messages', ...);
return $next($request);
}
How can I help you explore Laravel packages today?