Localization - Automatically Generate Translation JSON Files

Localization - Automatically Generate Translation JSON Files

In this article we will discuss how to automatically generate translation JSON files in laravel 11.We will use `devaslanphp/auto-translate ` laravel package .This package generate JSON files using Google Translator and exports translations string from your source code.This package is the wrapper of two packages ( stichoza/google-translate-php , kkomelin/laravel-translatable-string-exporter ).

Localization is essential for reaching a global audience.Creating and updating translation files by hand can be slow and mistakes can happen easily. Automating this task saves your time and ensures that all translations in your app are consistent.

For more detailed information and to view the source code, visit GitHub repository

Installation and Setup

First you need install the spatie laravel package tool

composer require spatie/laravel-package-tools

Install the package using composer

composer require devaslanphp/auto-translate

After you successfully install, add the package provider into Bootstrap > providers.php

return [ App\Providers\AppServiceProvider::class, \Devaslanphp\AutoTranslate\AutoTranslateProvider::class, ];

 REQUIRED : You must publish the package's configuration file to modify the base_locale and the list of locales as needed:

php artisan vendor:publish --tag=auto-translate-config

config > auto-translate.php

return [ /* * * Locales managed by auto-translation package, will be used by the * command "auto:translate" to generate a JSON file for each of this * locales, and by the command "translate:missing" to generate their * missing translations * */ 'locales' => [ 'fr', 'ar', 'en' ], /* * * The base locale to use when using the command "translate:missing" to * generate missing translations for other JSON files * */ 'base_locale' => 'fr' ];

Usage

To generate translation JSON files, run this command:

php artisan auto:translate

This command will check your auto-translate.locales form config file and  generate JSON  files  based on your source code.This package supports both syntax @lang() and __() .

The package also offers a command to generate translations for missing keys based on a specific JSON file (auto-translate.base_locale.json). 

php artisan translate:missing

Generating JSON Files for Translation

Once everything is set up, you can start generating the translation content.

Multi-lang Files image in laravel translated in french translated in english
Tags
Laravel Localization Tools Automate Translation Laravel Localization Laravel 11 Automate Translation Laravel Localization Laravel Translation Package Laravel Localization Setup Laravel Translation Json Laravel Localization Tips Google Translator Laravel Laravel Multi Language Laravel Localization Strings Laravel Translation Best Practices Automatically Generate Translation Files Auto-Translate Laravel Translations