Laravel
使用 Lingo.dev CLI 为 Laravel 提供 AI 翻译
什么是 Laravel?
Laravel 是一个 PHP Web 应用框架,具有简洁、优雅的语法。它内置了路由、认证、会话和缓存等功能,便于构建现代 Web 应用。
什么是 Lingo.dev CLI?
Lingo.dev 是一个由 AI 驱动的翻译平台。Lingo.dev CLI 会读取源文件,将可翻译内容发送给大型语言模型,并将翻译后的文件写回到您的项目中。
关于本指南
本指南介绍如何在 Laravel 应用中配置 Lingo.dev CLI。您将学习如何创建支持本地化的 Laravel 项目、配置翻译流程,并实现语言切换。
步骤 1. 搭建 Laravel 项目
-
安装 PHP 和 Composer:
/bin/bash -c "$(curl -fsSL https://php.new/install/mac/8.4)" -
全局安装 Laravel:
composer global require laravel/installer -
创建新的 Laravel 项目:
laravel new example-app出现提示时:
- 项目类型选择 "None"
- 数据库选择 "SQLite"
-
进入项目目录:
cd example-app
步骤 2. 创建源内容
-
发布 Laravel 的默认语言文件:
php artisan lang:publish这会创建一个
lang/en目录,包含如lang/en/auth.php的翻译文件。
步骤 3. 配置 CLI
在项目根目录下创建一个 i18n.json 文件:
{
"$schema": "https://lingo.dev/schema/i18n.json",
"version": "1.10",
"locale": {
"source": "en",
"targets": ["es"]
},
"buckets": {
"php": {
"include": ["lang/[locale]/*.php"]
}
}
}
此文件定义了:
- Lingo.dev CLI 需要翻译的文件
- 需要互译的语言
在本例中,配置将 PHP 语言文件从英文翻译为西班牙文。
需要注意的是:
[locale]是一个在运行时替换的占位符。它确保内容从一个位置(如lang/en/auth.php)读取,并写入另一个位置(如lang/es/auth.php)。- Lingo.dev CLI 会翻译语言目录下所有匹配该模式的 PHP 文件。
如需了解更多信息,请参见 i18n.json 配置。
步骤 4. 翻译内容
-
通过 CLI 登录 Lingo.dev:
npx lingo.dev@latest login -
运行翻译流水线:
npx lingo.dev@latest runCLI 会创建一个
lang/es/目录用于存储翻译内容,并生成一个i18n.lock文件以记录已翻译内容(防止重复翻译)。
步骤 5. 使用翻译结果
-
创建中间件以根据路由设置 locale:
php artisan make:middleware SetLocaleFromRoute -
更新生成的
app/Http/Middleware/SetLocaleFromRoute.php文件:<?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); } } -
在
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(); -
在
routes/web.php中配置带有 locale 前缀的路由:<?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'); }); -
在视图中使用
__辅助函数显示本地化内容:<!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> -
启动开发服务器:
npm install npm run build composer run dev -
访问以下 URL:
- http://localhost:8000/en 用于 English 内容
- http://localhost:8000/es 用于 Spanish 内容