Your Guide to Laravel Excellence

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.

Recommeded Posts

Laravel Localization: How to create a Multilingual Website in Laravel

Laravel Localization: How to create a Multilingual Website in Laravel

Laravel Localization: How to create a Multilingual Website in Laravel

3 months ago Read article →
Google Oauth Authentication from Scratch in Laravel

Google Oauth Authentication from Scratch in Laravel

Google Oauth Authentication from Scratch in Laravel

3 months ago Read article →
Many-to-Many relationship in Laravel - All you need to know

Many-to-Many relationship in Laravel - All you need to know

how to implement many-to-many relationships in Laravel, including pivot tables, syncing data, and advanced queries. Learn best practices for optimizing database relationships using Laravel Eloquent. Perfect guide for developers to master Laravel many-to-many relationship

3 months ago Read article →
How to Create XML from an Array Using Spatie(array-to-xml) in Laravel

How to Create XML from an Array Using Spatie(array-to-xml) in Laravel

Learn how to convert an array into XML format using the Spatie array-to-xml package in Laravel. This easy guide walks you through the steps to set up the package and create XML files from your data quickly.

3 months ago Read article →