【问题标题】:How to change locale of a blade file如何更改刀片文件的语言环境
【发布时间】:2019-03-03 09:21:12
【问题描述】:

我们正在使用 IP2LOCATIONLARAVEL 来获取访问者的国家代码。如果访问者的IP来自伊朗(ISO国家代码=IR),blade.php中html标签的lang属性必须设置为'fa',否则设置为'en'。

首先,我们将这一行添加到我们的 config/app.php 中:

'locale' => 'en',
// we added:
'other_locale' => ['fa-IR'],

我们还创建了一个中间件,并在 app/Http/kernel.php 中正确添加。

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Foundation\Application;
use Ip2location\IP2LocationLaravel\Facade\IP2LocationLaravel;

class LocaleHandler
{
    public function __construct(Application $app, Request $request) {
        $this->app = $app;
        $this->request = $request;
    }

    public function handle($request, Closure $next)
    {
        if (app()->getLocale()=='fa'){
            if(in_array($request->segment(0), config('app.other_locale'))){
                $this->app->setLocale($request->segment(0));
            }else{
                $this->app->setLocale(config('app.locale'));
            }
        }

        return $next($request);
    }
}

然后我们更新了app/Providers/RouteServiceProvider.php:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;

class RouteServiceProvider extends ServiceProvider
{

    protected $namespace = 'App\Http\Controllers';

    public function boot()
    {
        parent::boot();
    }


    public function map(Request $request)
    {
        $this->mapApiRoutes();
        $this->mapWebRoutes($request);
    }


    protected function mapWebRoutes(Request $request)
    {
        if(in_array($request->segment(0), config('app.other_locale'))){
            $locale = $request->segment(0);

        }else{
            $locale = null;
        }

        Route::group([
            'middleware' => 'web',
            'namespace' => $this->namespace,
            'prefix' => $locale
        ], function ($router) {
            require base_path('routes/web.php');
        });

        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    }


    protected function mapApiRoutes()
    {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));
    }
}

localhost/laravel/public/fa 是 404 错误页面。我们如何更新

<html lang="{{ app()->getLocale() }}">

在我们的刀片文件中,如果访问者国家代码是 IR?

【问题讨论】:

  • 很难说出你真正想要达到的目标。您应该在您的 PHP 代码中设置有效的语言环境,在 Blade 中您只需按照您的显示方式显示它。
  • 我有 1 个刀片文件。如果访客来自伊朗,我希望 setLocale 为 fa。我有一个 if-else(用于检查访客国家代码)但它不起作用:setLocale.
  • 看看答案,可能是你的中间件从来没有应用过的问题
  • '/' 是 en 但 '/fa' 仍然是错误 404 而不是 'fa'
  • 注意:Route::get('/',function() { return view('welcome'); });

标签: php laravel


【解决方案1】:

你确定在文件 kernel.php 中添加了 web 中间件

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\LocaleHandler::class,
...]

【讨论】:

    猜你喜欢
    • 2013-04-27
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多