DEV Community

Cover image for Multi Language App in Professional Way with Laravel !
Salma
Salma

Posted on • Edited on

Multi Language App in Professional Way with Laravel !

Aloha There !
In This article we talk about make app with Mutli language.

  1. Using mcamara laravel localization package to make direction in laravel app .
  2. Using Laravel Spatie Transtable To make dynamic json Columns
  3. Using Laravel Sptaie Sluggable To Create Slug
  4. Describe how we can make Multi slug with lang

Point 2,3,4 in the next level of this article

*When Create A new project laravel as we know
*

composer create-project --prefer-dist laravel/laravel MultiLangApp

Then

We begin with mcamara laravel-localization package
this package contain many features Some of it

  • Detect language from browser
  • Smart redirects (Save locale in session/cookie)
  • Smart routing (Define your routes only once, no matter how many languages you use)
  • Translatable Routes
  • Supports caching & testing
  • Option to hide default locale in url


Install the package via composer:
composer require mcamara/laravel-localization
*In order to edit the default configuration you may execute:
*

php artisan vendor:publish --provider="Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider"

*You may register the package middleware in the app/Http/Kernel.php file:
*


<?php namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel {
    /**
    * The application's route middleware.
    *
    * @var array
    */
    protected $routeMiddleware = [
        /**** OTHER MIDDLEWARE ****/
        'localize'                => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
        'localizationRedirect'    => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
        'localeSessionRedirect'   => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
        'localeCookieRedirect'    => \Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect::class,
        'localeViewPath'          => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class
    ];
}

Enter fullscreen mode Exit fullscreen mode

*Add the following to your routes file:
*

// routes/web.php

Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
    Route::get('/', function () {
        return view('welcome');
    });
});
Enter fullscreen mode Exit fullscreen mode

in config in laravelLocalization.php file we can uncomment for the language we need to use in the application .

we can make test now add translate to words then we can the translation and redirect make perfectly with this package

you can see the code in this repo
https://github.com/salmazz/Multi-Langague-Example-Laravel-
and continue in the next article
how can we make dynamic columns with multi lang for slug.
bye bye !

Top comments (1)

Collapse
 
dostogirmir profile image
MIr Dostogir Ahmed • Edited

As my analyses i find this some important point.

using spatie/laravel-translatable stores translations in a JSON format within a single column. This approach can have some performance implications, particularly:

Slower Queries: Retrieving data with JSON columns can be slower than standard text columns because of the additional processing required to parse the JSON.

Indexing Limitations: JSON columns have limited indexing capabilities, which may impact query performance when filtering or sorting by translated fields.

Increased Complexity: Working with JSON columns can add complexity to your queries, especially if you need to filter or order by specific language values.