Laravel localization

2020-05-07 05:47发布

问题:

I'm going crazy with Laravel's localization system, but I don't know what's wrong with my code.

I just wrote a language-chooser, in the Head of the document you'll see a function to get the Browser-default language, which shall get the default language.

The Language-Templates are saved as default in an array in the /resources/lang/*language*/messages.php-files.

I really need your help, because I can't see any errors.

<html>
  <head>

  <?php

    //get Browser default language
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    App::setlocale($lang);

  ?>

</head>
<body>

    <div class="container">
        @yield('content')
            <div class="well text-center">
                <h1><?php echo trans('Welcome') ?></h1>
            </div>
        @section('content.welcome')
            <div class="row text-center">
                <a href="<?php App::setlocale("de") ?>">Deutsch</a>
                <label>  |  </label>
                <a href="<?php App::setlocale("en") ?>">English</a>
                <label>  |  </label>
                <a href="<?php App::setlocale("it") ?>">Italiano</a>
                <label>  |  </label>
                <a href="<?php App::setlocale("es") ?>">Español</a>
                <label>  |  </label>
                <a href="<?php App::setlocale("fr") ?>">Français</a>
                <label>  |  </label>
                <a href="<?php App::setlocale("jp") ?>">日本人</a>
                <label>  |  </label>
                <a href="<?php App::setlocale("ru") ?>">Pусский</a>
            </div>
        @show
    </div>

    @section('footer')

        <nav>
            <a href="#"> &copy; <?php echo date("Y"); ?></a>
            <ul >
                <li >
                    <a href="#"><?php echo trans('Contact') ?></a>
                </li>

            </ul>
        </nav>

    @show

</body>

回答1:

As per my understand, you need to modify your current approach. So, here is the example you can utilize for project.

.Env

APP_LOCALE=en

config/app.php

'locale' => env('APP_LOCALE', 'en'),

HTML

<ul class="dropdown-menu" role="menu">
    <li>{!! link_to('lang/en', trans('menus.language-picker.langs.en')) !!}</li>
    <li>{!! link_to('lang/es', trans('menus.language-picker.langs.es')) !!}</li>
    <li>{!! link_to('lang/fr-FR', trans('menus.language-picker.langs.fr-FR')) !!}</li>
    <li>{!! link_to('lang/it', trans('menus.language-picker.langs.it')) !!}</li>
    <li>{!! link_to('lang/pt-BR', trans('menus.language-picker.langs.pt-BR')) !!}</li>
    <li>{!! link_to('lang/ru', trans('menus.language-picker.langs.ru')) !!}</li>
    <li>{!! link_to('lang/sv', trans('menus.language-picker.langs.sv')) !!}</li>
</ul>

Route

get('lang/{lang}', 'LanguageController@languageRoute');

LanguageController

namespace App\Http\Controllers;

class LanguageController extends Controller
{
    function languageRoute($lang)
    {
        session()->put('locale', $lang);
        return redirect()->back();
    }
}

LocaleMiddleware

namespace App\Http\Middleware;
use Closure;
class LocaleMiddleware
{
    protected $languages = ['en', 'es', 'fr-FR', 'it', 'pt-BR', 'ru', 'sv'];
    public function handle($request, Closure $next)
    {
        if(session()->has('locale') && in_array(session()->get('locale'), $this->languages))
        {
            app()->setLocale(session()->get('locale'));
        }
        return $next($request);
    }
}

add middleware to kernel.php

And Finally , my lang folder looks like

en
es
fr-FR
it
pt-BR
ru
sv

and each folder contains:

alerts.php  
auth.php    
crud.php    
labels.php  
menus.php   
navs.php    
pagination.php
passwords.php
roles.php   
strings.php 
validation.php

Hope, this example help you much, but however it's just a example still you can update as per your requirement. If still something confused please let me know, I will try to update question in further detail.