先增加一个中间件app/Http/Middleware/LangMiddleware.php

    1. <?php
    2. namespace App\Http\Middleware;
    3. use Closure;
    4. class LangMiddleware
    5. {
    6. /**
    7. * 多语言处理中间件
    8. * @param $request
    9. * @param Closure $next
    10. * @return mixed
    11. */
    12. public function handle($request, Closure $next)
    13. {
    14. $host = $request->getHost();
    15. switch ($host){
    16. case 'zh.baidu.io':
    17. $lang = 'zh';
    18. break;
    19. case 'en.baidu.io':
    20. $lang = 'en';
    21. break;
    22. default:
    23. $lang = 'zh';
    24. }
    25. app('translator')->setLocale($lang);
    26. return $next($request);
    27. }
    28. }

    然后在bootstrap/app.php里注册为全局中间件

    1. <?php
    2. $app->middleware([
    3. # App\Http\Middleware\CrossHttp::class,
    4. App\Http\Middleware\LangMiddleware::class,
    5. # App\Http\Middleware\ExampleMiddleware::class
    6. ]);

    语言定义文件在 resource/lang目录下:
    lumen 多语言支持 - 图1

    resources/lang/zh/message.php内容实例:

    1. <?php
    2. return [
    3. 'validation_failed' => '验证失败。',
    4. ];

    代码里调用翻译:

    1. use Illuminate\Support\Facades\Lang;
    2. $output = Lang::get('message.validation_failed');