How To Set The Multi-Languages In Laravel

  • Home
  • Blogs
  • How To Set The Multi-Languages In Laravel

The Laravel package is the primary way of adding functionality to Laravel. It allows you to manage your translations and offers you several types of packages.

Vimal Tarsariya

July 13, 2022

We develop several applications, but in the development process, we add multi-language support so that we can change the language in our blog or website on the front end. 

MultiLang is a very useful package to integrate multi-language functionality into Laravel. It uses a database for storing translations (or caches data in the production environment for improving performance). Learn more about this in our blog. 

Scaffolding The Package

The first thing you need to do when creating a PHP package of any kind is set up the repository, and most importantly the composer file. There’s no de-facto standard approach to organizing Composer packages; however, I will walk you through my approach.

I tend to start by installing a fresh version on Laravel using the installer. Using a test Laravel installation gives me a shiny new playground in which to work.

In the root of my newly installed Laravel project, I create a directory called packages. Within this directory, I create a directory structure for my package which aligns with thegit GitHub repository where the code will ultimately reside – in this case, joedixon/laravel-translation.

From within the package directory, I run git init to set up version control. Next, I run the composer init and follow the instructions which create my composer.json file for managing any dependencies I may require and for later submission to Packagist.

Next, I create an src directory at the root of my package which will contain all of the domain-specific logic of the package.

Step -1: How To Autoload My Package Namespace

In the composer.json file, I add the following PSR-4 configuration to tell Composer how to autoload my package namespace:

Finally, before I write a single line of code, I amend the composer.json file at the root of the Laravel application to tell it how to load my app.

Hopefully, the require section will look familiar to you. The repositories section, however, may not.

The repositories section tells Composer to symlink the package from the local installation. Using this approach allows us to work on and test the package locally without having to composer update anytime something changes.

When using the local file path approach, it’s worth noting the package can be anywhere on your machine; however, I like to keep it contained within the application structure for development.

How that the package has been bootstrapped and autoload configured, we can start writing some code.

Step - 2: Package Structure

When building a package, I try to mirror the Laravel application structure as much as possible. Everything typically found in my app directory such as controllers, console commands, event listeners, etc. go into my package src directory and things such as routes and resources live at the root of the package.

Step - 3: Service Provider

The service provider class is where we will bootstrap the package by performing actions such as binding services to the container, registering routes, publishing configuration, and just about anything else you can imagine that need tying into the Laravel app!

Step - 4: Tip

Typically, I will run PHP artisan make provider TranslationServiceProvider which will scaffold a service provider in the Providers directory of the Laravel application. I then move it to my package and update the namespace accordingly.

At this stage, I will register things like routes, config, views, and translations without necessarily fleshing out all of the details. I find doing it in this way makes things faster later on in development.

Step - 5: Testing

No good package would be complete without tests.

Setting up tests in a package can be quite tricky; mainly if you want to be able to access Laravel’s testing helpers. Fortunately, the orchestra/testbench package makes it possible to use all of Laravel’s native test helpers within your package's tests.

Run composer require-dev orchestra/testbench to install it as a development dependency. Running Composer should give you the following configuration in the composer.json file:

Before we’re done, let’s commit what we have to the master branch:


It was all about the package scaffolding. This step-by-step guide helps to develop applications with the best features in Laravel. You can try these easy learning steps and start developing your new projects with the best Laravel packages.


Vimal Tarsariya

Project Manager at Vasundhara Infotech, a leading software development company. His great planning and execution qualities led to several successful projects.


Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!


Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Latest 13 Web Development Trends To Expect In 2022
April 11, 2022 Category : company news

Revealing Vasundhara’s New Identity

Read More
Leave a Comment