How To Translate Laravel Language Files To Other Languages In a Minute?

İlyas Özkurt
Beyn Technology
Published in
4 min readJan 26, 2023

--

If you are working on a Laravel application that needs to support multiple languages, you may be wondering how to translate the language files to other languages. In this tutorial, I will show you how to quickly and easily translate Laravel language files to other languages using a command file.

Whether you are creating a new application or updating an existing one, this tutorial will provide you with the information you need to get started with translation in Laravel. By the end of this tutorial, you will have a solid way of translating your Laravel application to other languages quickly.

This method is for just translation files. This method helps you to translate all your language files with a single command with options like source, target, include files, exclude files… This article is not valuable for you if you’re looking for multi-language content management and structure.

Translation Management In Laravel

Translation management in Laravel involves coordinating the translation of text and content to support multiple languages. Some tools and techniques can make this process easier and more efficient, such as translation management software, translation middleware, and custom translation drivers. Developers can create user-friendly applications for a global audience by effectively managing translations.

There are different ways to manage Translations in Laravel. Here are the ones of most common:

  1. Laravel’s built-in translation functions and files: Laravel provides a number of built-in functions and methods for translating text and content within the application. These include the “trans” function, the “__” function, and the “Lang” facade. These functions allow developers to easily retrieve translated content from the language files and display it to users.
  2. Translation tools: There are a number of tools and services available that can help developers manage translations in Laravel. These tools often allow developers to easily extract text from the application, send it to translators, and import the translated content back into the application. Some popular tools include Phrase, Crowdin, and Transifex.
  3. Database-driven translations: Some developers choose to store translations in the database rather than in language files. This can be useful for applications that need to support dynamic translations or that need to allow users to edit translations directly. Laravel provides a number of packages and libraries that can be used to manage database-driven translations, such as Laravel Translatable and Laravel Translation Manager.

Laravel’s Built-in Translation Functions And Files

In Laravel, translation files are typically stored in the “resources/lang” or “lang/” directory of the application. Within this directory, there is a separate subdirectory for each supported language, with the name of the subdirectory corresponding to the language code.

For example, a Laravel application might have a “resources/lang/en” directory for English translations and a “resources/lang/fr” directory for French translations. Within each language directory, one or more translation files contain the key-value pairs for that language. These files are typically organized by topic or feature, with each file containing a set of related translations. For example, an application might have a “validation.php” file for validation-related translations and a “login.php” file for login-related translations.

The key-value pairs within each translation file use PHP arrays to store the translations, with the keys representing the original text and the values representing the translated text.

Here’s an example translation file preview:

lang/en/example.php

The Problem

When the application starts to have lots of language files, the problem becomes a nightmare… Adding a new key for all files, creating a new language from the default language…

Think, you have 25 languages and 30 files per language. Adding new keys becomes a stupid process…

Here’s a preview of the mid-sized application language directory:

A Preview From Mid-Size Project’s English Language Directory

The Solution

In the Laravel ecosystem, creating commands can be a helpful way to automate tasks and improve the efficiency of your application. Commands are essentially PHP classes that are run from the command line, and they can be used to perform a variety of tasks such as generating code, importing and exporting data, or running maintenance tasks. By creating custom commands in Laravel, developers can automate many of the repetitive or time-consuming tasks that are commonly performed as part of the development process. This can save time and improve the efficiency of the development process, and it can also help to ensure that tasks are performed consistently and correctly. Additionally, by using commands to automate tasks, developers can free up more time to focus on other important aspects of the application.

The Life-saver command to translate files from source to target language.

Click here to see the full code of the TranslateLangFiles.php command

Using the command outlined in this article, you should now have a solid solution for translating Laravel language files to other languages.

Whether creating a new application or updating an existing one, the technique and command discussed in this article will help you manage translations effectively and provide a better user experience for your global audience.

Thanks for reading, and thanks for your time!

--

--