Menu

Laravel Localization: Multi-Language Website
πŸ–ΌοΈ

Laravel Localization: Multi-Language Website

DEV CommunityΒ·Rafli ZockyΒ·6 months ago
#Pei9MJqR
#laravel#php#backenddevelopment#webdev#locale
Reading 0:00
15s threshold

Auto-Translated (Content-Based)

Install Google Translate API PHP Package

composer require stichoza/google-translate-php

Enter fullscreen mode

Exit fullscreen mode

Language Switcher (View)

<div class= "flex items-center space-x-2" > <a href= "/lang/id" class= "text-gray-200 hover:text-white {{ session('locale') === 'id' ? 'border-b-2 border-yellow-500' : '' }}" > ID </a> <span class= "text-gray-200" > | </span> <a href= "/lang/en" class= "text-gray-200 hover:text-white {{ session('locale') === 'en' || !session('locale') ? 'border-b-2 border-yellow-500' : '' }}" > EN </a> </div>

Enter fullscreen mode

Exit fullscreen mode

Route for Language Switching

Route :: get ( '/lang/{lang}' , function ( $lang ) { session ([ 'locale' => $lang ]); return back (); });

Enter fullscreen mode

Exit fullscreen mode

Migration β€” add columns to store translations:

Schema :: table ( 'abouts' , function ( Blueprint $table ) { $table -> string ( 'title' ); $table -> text ( 'description' ); $table -> json (…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.