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.8,
"locale": {
"source": "en",
"targets": ["es"]
},
"buckets": {
"php": {
"include": ["lang/[locale]/*.php"]
}
}
}
该文件定义了:
- Lingo.dev CLI 应翻译的文件
- 翻译的语言
在此配置中,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 run
CLI 将创建一个
lang/es/
目录用于存储翻译内容,并生成一个i18n.lock
文件以跟踪已翻译的内容(以防止不必要的重复翻译)。
第 5 步. 使用翻译内容
-
创建中间件以从路由中设置语言环境:
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 { /** * 处理传入的请求。 * * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next */ public function handle(Request $request, Closure $next): Response { // 'locale' 来自 {locale} 路由参数 if ($locale = $request->route('locale')) { App::setLocale($locale); // 为此请求设置语言环境 } 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
中配置带有语言环境前缀的路由:<?php use Illuminate\Support\Facades\Route; Route::redirect('/', '/en'); // 默认跳转到英文 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 用于英文内容
- http://localhost:8000/es 用于西班牙文内容