Skip to content

Laravel Localization package that auto generate json locale files

License

Notifications You must be signed in to change notification settings

Mahmoud217TR/auto-files-localizer

Repository files navigation

Auto Files Localizer for Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Easy and Efficient Localiztion

A Laravel Localization package that auto generate json locale files

logo

Table of Contents

Features

  • Supports Laravel (8.X, 9.X, 10.X, 11.X).
  • 2 Modes (Dynamic Mode, Extraction Mode).
  • Fast and reliable.

Installation

You can install the package via composer:

composer require mahmoud217tr/auto-files-localizer

Config

To publish the package configuration file config/auto-localizer.php use the following command:

php artisan vendor:publish --tag="auto-files-localizer-config"

Usage

It will automatically generate the locale .json file where the localized new words are sorted alphabetically.

There are 2 modes for the package

Dynamic Mode

In this mode the translations will be generated dynamically (On Request) which means on upon calling the translation function the package will dynamically detect the current language and add the translations to the corresponding JSON file.

Notes

  • Note that this mode shouldn't work on production for performance purposes, but if you want to change this behavior via the configuration file.

  • This mode is not recommended using with vite development mode. If you want to use it please build your assets first using npm run build then activate the auto translation mode. OTHERWISE YOUR APP WILL STUCK IN RELOAD LOOP

Activiation

To activate the dynamic mode on non-production environment just add the following line to the .env file:

AUTO_LOCALIZER_ENABLED=true

And that's about it the autotranslator will add transaltions to there files sorted alphabetically. To deactivate the dynamic mode just set the previous value to false.

Configurations

This mode have the following configurations:

'dynamic' => [
    # Controls if the dynamic mode should work or not on non-production environments
    'enabled' => (bool) env('AUTO_LOCALIZER_ENABLED', false), 
    # Controls if the dynamic mode should work or on production environment (compined with the previous option)
    'production' => false,
],

Extraction Mode

In this mode the translations will be extracted automatically from views or php files detecting translation functions adding the translations to the corresponding JSON file.

Command

To extract the translations for a specific locale (ar for example) run the following command:

php artisan localizer:extract ar

The previous command will translate all non-localized phrases and all ar-localized phrases only.

Configurations

This mode have the following configurations:

'extraction' => [
    # The directories where the translation scanner will scan
    'directories' => [
        'resources/views',
    ],
    # The file patterns that the translation scanner will scan
    'patterns' => [
        '*.php'
    ],
    # The translation functions or directives that scanner will detect (you should add your custom functions here)
    'functions' => [
        '__',
        'trans',
        '@lang',
    ],
],

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.