DEV Community

Dhiecoderweb
Dhiecoderweb

Posted on Originally published at dhiecoderweb.com

Build a Multi-Language App in Laravel (i18n)

Building an application that supports more than one language is an increasingly common need, especially for applications aimed at users from various countries. Laravel has a built-in Localization feature that makes it easy to manage text translations without having to change code in many places. In this tutorial, we'll build an application that can switch between Indonesian and English.

The Translation File Structure

Laravel stores translation files in the lang/ folder. Since Laravel 9, this folder is in the project root. If it doesn't exist, create it with:

php artisan lang:publish
Enter fullscreen mode Exit fullscreen mode

The folder structure we'll create:

lang/
├── en/
│   ├── messages.php
│   └── auth.php
└── id/
    ├── messages.php
    └── auth.php
Enter fullscreen mode Exit fullscreen mode

Creating PHP Translation Files

Create the file lang/en/messages.php:

<?php

return [
    'welcome'       => 'Welcome to :app!',
    'greeting'      => 'Hello, :name!',
    'nav' => [
        'home'      => 'Home',
        'about'     => 'About',
        'contact'   => 'Contact',
        'products'  => 'Products',
    ],
    'product' => [
        'title'     => 'Product List',
        'not_found' => 'No products found.',
        'count'     => '{0} No products|{1} One product|[2,*] :count products',
    ],
];
Enter fullscreen mode Exit fullscreen mode

Create the file lang/id/messages.php:

<?php

return [
    'welcome'       => 'Selamat datang di :app!',
    'greeting'      => 'Halo, :name!',
    'nav' => [
        'home'      => 'Beranda',
        'about'     => 'Tentang Kami',
        'contact'   => 'Kontak',
        'products'  => 'Produk',
    ],
    'product' => [
        'title'     => 'Daftar Produk',
        'not_found' => 'Tidak ada produk ditemukan.',
        'count'     => '{0} Tidak ada produk|{1} Satu produk|[2,*] :count produk',
    ],
];
Enter fullscreen mode Exit fullscreen mode

This is only part of the article. For the full discussion, with examples and step-by-step details, you can read it on the original source:

Build a Multi-Language App in Laravel (i18n)

More programming tutorials and developer tools are also available at dhiecoderweb.com.

Top comments (0)