lumen增加多语言功能
先增加一个中间件app/Http/Middleware/App.php
<?php namespace App\Http\Middleware; use Closure; class App { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { app('translator')->setLocale($request->input('lang')); return $next($request); } }
然后在bootstrap/app.php里注册为全局中间件
$app->middleware([ App\Http\Middleware\App::class, // ... ]);
resource目录下增加lang文件夹:
resources/lang/cn/messages.php内容实例:
<?php return [ 'validation_failed' => '验证失败。', ];
代码里调用翻译:
use Illuminate\Support\Facades\Lang; // ... $output = Lang::get('messages.validation_failed');
然后url里加上lang=cn就可以显示翻译后的内容了。