Laravel
AI translation for Laravel with Lingo.dev CLI
What is Laravel?
Laravel is a PHP web application framework with expressive, elegant syntax. It provides built-in features for routing, authentication, sessions, and caching to build modern web applications.
What is Lingo.dev CLI?
Lingo.dev is an AI-powered translation platform. The Lingo.dev CLI reads source files, sends translatable content to large language models, and writes translated files back to your project.
About this guide
This guide explains how to set up Lingo.dev CLI in a Laravel application. You'll learn how to create a Laravel project with localization support, configure a translation pipeline, and implement language switching.
Step 1. Set up a Laravel project
-
Install PHP and Composer:
/bin/bash -c "$(curl -fsSL https://php.new/install/mac/8.4)"
-
Install Laravel globally:
composer global require laravel/installer
-
Create a new Laravel project:
laravel new example-app
When prompted:
- Select "None" for project type
- Select "SQLite" for database
-
Navigate into the project directory:
cd example-app
Step 2. Create source content
-
Publish Laravel's default language files:
php artisan lang:publish
This creates a
lang/en
directory with translation files likelang/en/auth.php
.
Step 3. Configure the CLI
In the root of the project, create an i18n.json
file:
{
"$schema": "https://lingo.dev/schema/i18n.json",
"version": 1.8,
"locale": {
"source": "en",
"targets": ["es"]
},
"buckets": {
"php": {
"include": ["lang/[locale]/*.php"]
}
}
}
This file defines:
- the files that Lingo.dev CLI should translate
- the languages to translate between
In this case, the configuration translates PHP language files from English to Spanish.
It's important to note that:
[locale]
is a placeholder that's replaced at runtime. It ensures that content is read from one location (e.g.,lang/en/auth.php
) and written to a different location (e.g.,lang/es/auth.php
).- Lingo.dev CLI will translate all PHP files in the language directory that match this pattern.
To learn more, see i18n.json configuration.
Step 4. Translate the content
-
Log in to Lingo.dev via the CLI:
npx lingo.dev@latest login
-
Run the translation pipeline:
npx lingo.dev@latest run
The CLI will create a
lang/es/
directory for storing the translated content and ani18n.lock
file for keeping track of what has been translated (to prevent unnecessary retranslations).
Step 5. Use the translations
-
Create middleware to set the locale from the route:
php artisan make:middleware SetLocaleFromRoute
-
Update the generated
app/Http/Middleware/SetLocaleFromRoute.php
file:<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\App; use Symfony\Component\HttpFoundation\Response; class SetLocaleFromRoute { /** * Handle an incoming request. * * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next */ public function handle(Request $request, Closure $next): Response { // 'locale' comes from the {locale} route parameter if ($locale = $request->route('locale')) { App::setLocale($locale); // sets the locale for this request } return $next($request); } }
-
Register the middleware in
bootstrap/app.php
:<?php use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware; use App\Http\Middleware\SetLocaleFromRoute; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/../routes/web.php', commands: __DIR__.'/../routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) { $middleware->alias([ 'setLocale' => SetLocaleFromRoute::class, ]); }) ->withExceptions(function (Exceptions $exceptions): void { // })->create();
-
Configure routes with locale prefixes in
routes/web.php
:<?php use Illuminate\Support\Facades\Route; Route::redirect('/', '/en'); // default to English Route::prefix('{locale}') ->whereIn('locale', ['en', 'es']) ->middleware('setLocale') ->group(function () { Route::get('/', function () { return view('welcome'); })->name('home'); });
-
Use the
__
helper function to display localized content in your views:<!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>{{ config('app.name', 'Laravel') }}</title> </head> <body> <nav> <a href="{{ route('home', ['locale' => 'en']) }}" class="underline mr-4">English</a> <a href="{{ route('home', ['locale' => 'es']) }}" class="underline">Español</a> </nav> <p class="mb-6">{{ __('auth.throttle') }}</p> </body> </html>
-
Start the development server:
npm install npm run build composer run dev
-
Navigate to the following URLs:
- http://localhost:8000/en for English content
- http://localhost:8000/es for Spanish content