先增加一个中间件app/Http/Middleware/App.php
<?php namespace AppHttpMiddleware; use Closure; class App { /** * Handle an incoming request. * * @param IlluminateHttpRequest $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([ AppHttpMiddlewareApp::class, // ... ]);
resource目录下增加lang文件夹:
resources/lang/cn/messages.php内容实例:
<?php return [ 'validation_failed' => '验证失败。', ];
代码里调用翻译:
use IlluminateSupportFacadesLang; // ... $output = Lang::get('messages.validation_failed');
然后url里加上lang=cn就可以显示翻译后的内容了。