Installation Add the package via Composer:
composer require anisimov/translation-bundle
Register the bundle in config/app.php under providers:
Anisimov\TranslationBundle\TranslationBundle::class,
Database Configuration Publish the migration and config files:
php artisan vendor:publish --provider="Anisimov\TranslationBundle\TranslationBundle" --tag="migrations"
php artisan vendor:publish --provider="Anisimov\TranslationBundle\TranslationBundle" --tag="config"
Run the migration:
php artisan migrate
First Use Case: Importing Translations
Place your translation files (e.g., messages.en.yml, messages.fr.yml) in config/translations/.
Import them via CLI:
php artisan translation:import
Access the GUI at /admin/translations (ensure you have a route for the admin panel).
Importing Translations
php artisan translation:import to load YAML/JSON files into the database.Anisimov\TranslationBundle\Command\ImportCommand:
// app/Console/Commands/CustomImportCommand.php
use Anisimov\TranslationBundle\Command\ImportCommand;
class CustomImportCommand extends ImportCommand {
protected function getTranslationFiles() {
return [base_path('custom/translations/*.yml')];
}
}
Editing via GUI
/admin/translations.resources/views/vendor/translation/.Retrieving Translations in Code
__('messages.welcome'); // Falls back to DB if file-based translations are missing
$translations = \App\Models\Translation::where('locale', 'en')->get();
Integration with LexikTranslationBundle
// config/translation.php
'bundles' => [
'anisimov' => true,
'lexik' => false, // Disable Lexik if using Anisimov's fork
],
Route Conflicts
/admin/translations*) may clash with existing routes. Override them in routes/web.php:
Route::prefix('custom-translations')->group(function () {
// Include Anisimov's routes here
});
Locale Fallback
AppServiceProvider:
public function boot() {
app()->setLocale(config('app.fallback_locale'));
}
Translation Key Collisions
Performance with Large Datasets
$translations = \App\Models\Translation::where('locale', 'en')->paginate(20);
Check Database Structure
translations table has columns: id, locale, key, value, domain (if used).php artisan schema:dump to inspect the schema.Log Import Errors
protected function processFile($file) {
try {
// Import logic
} catch (\Exception $e) {
\Log::error("Failed to import $file: " . $e->getMessage());
}
}
Clear Cache After Changes
php artisan cache:clear
php artisan view:clear
Custom Translation Models
Translation model to add fields (e.g., priority, author):
// app/Models/CustomTranslation.php
class CustomTranslation extends \Anisimov\TranslationBundle\Entity\Translation {
protected $fillable = ['priority'];
}
Validation Rules
// app/Http/Controllers/TranslationController.php
use Anisimov\TranslationBundle\Controller\TranslationController as BaseController;
class TranslationController extends BaseController {
public function store(Request $request) {
$request->validate([
'value' => 'required|string|max:255',
]);
return parent::store($request);
}
}
Event Listeners
// app/Providers/EventServiceProvider.php
public function boot() {
\Anisimov\TranslationBundle\Event\TranslationUpdated::class => [
\App\Listeners\NotifyTranslators::class,
];
}
How can I help you explore Laravel packages today?