-1

ローカリゼーションを使用して多言語をサポートする Laravel で開発された Web サイトがあります。

まず、言語フォルダーとそのファイル /resources/lang/en/message.php を作成しました。

<?php

return [
    'page_title' => 'Welcome Page',
    'welcome_message' => 'Hi, Welcome to this page',
    'author_information' => 'My name is Sanjay. This blog is mine and we created this post for you to learn.',
];

/resources/lang/fr/messages.php

<?php

return [
    'page_title' => 'Pagina de bienvenida',
    'welcome_message' => 'Hola bienvenido a esta pagina',
    'author_information' => 'Mi nombre es Sanjay. Este blog es mío y creamos esta publicación para que aprendas.',
];

第二に、web.phpファイルにアプリケーションルートを作成しました

Route::get('/', [LocalizationController::class, "index"]);
Route::get('change/lang', [LocalizationController::class, "lang_change"])->name('LangChange');

3 番目: 言語の変更を管理する LocalizationController を作成しました

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class LocalizationController extends Controller
{
    public function index()
    {
        return view('language');
    }
    public function lang_change(Request $request)
    {
        App::setLocale($request->lang);
        session()->put('locale', $request->lang);
        return view('language');
    }
}

最後に: 言語は、LocalizationController によって管理されるドロップダウン リストを使用して変更できます。

<body>
    <div class="container">
        <div class="row" style="text-align: center;margin-top: 40px;">
            <h2>How to Create Multi Language Website in Laravel - Online Web Tutor Blog</h2><br>
            <div class="col-md-2 col-md-offset-3 text-right">
                <strong>Select Language: </strong>
            </div>
            <div class="col-md-4">
                <select class="form-control Langchange">
                    <option value="en" {{ session()->get('locale') == 'en' ? 'selected' : '' }}>English</option>
                    <option value="es" {{ session()->get('locale') == 'es' ? 'selected' : '' }}>Spanish</option>
                </select>
            </div>
            <h1 style="margin-top: 80px;">{{ __('message.page_title') }}</h1>
            <h2 style="margin-top: 80px;">{{ __('message.welcome_message') }}</h2>
            <h3 style="margin-top: 80px;">{{ __('message.author_information') }}</h3>
        </div>
    </div>
</body>

<script type="text/javascript">
    var url = "{{ route('LangChange') }}";
    $(".Langchange").change(function(){
        window.location.href = url + "?lang="+ $(this).val();
    });
</script>

ただし、ユーザーが Web サイトのフォームを使用してデータベースにデータを挿入すると、ユーザーが挿入した内容が Web サイトに正確に表示されます。Web サイトがユーザー入力を変換する方法はありますか?

4

1 に答える 1