elaborate-code / laravel-json-tongue
Use multiple JSON files per locale on Laravel
Requires
- php: ^8.0
- elaborate-code/php-json-tongue: ^1.2
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
README
This package is built on top of PHP JSON tongue to enable the usage of multiple JSON files per locale on Laravel.
Introduction
Starting with Laravel docs:
Translation strings may be defined within JSON files that are placed within the lang directory. When taking this approach, each language supported by your application would have a corresponding JSON file within this directory. This approach is recommended for applications that have a large number of translatable strings.
Intuitively, many developers wonder why isn't it possible to have each language supported by their application have multiple corresponding JSON files. Multiple JSON files allows grouping strings by topic, and keeping the files small and clear.
Installation
Install the package via composer:
composer require elaborate-code/laravel-json-tongue
Requirements
- PHP 8.0 or higher.
Usage
File structure example:
The JSON files can co-exist with the PHP files without any conflicts!
The merge command
This command loads all the JSON files from /<locale>
folders within the /lang
folder and merge them per locale in new JSON files.
php artisan json-tongue:merge
Options:
JSON files that already exist in the root of the
lang
folder, can be old JSON files previously generated by the command, or files that you have created manually and populated manually.⚠️ In the second case be careful before instructing the command to remove existing JSON files!
Example
Before merging :
lang ┣ 📂es ┃ ┣ 📜animals.json ┃ ┣ 📜greetings.json ┃ ┗ 📜jobs.json ┗ 📂fr ┣ 📜animals.json ┣ 📜greetings.json ┗ 📜jobs.json
After merging:
lang ┣ 📂es ┃ ┣ 📜animals.json ┃ ┣ 📜greetings.json ┃ ┗ 📜jobs.json ┣ 📂fr ┃ ┣ 📜animals.json ┃ ┣ 📜greetings.json ┃ ┗ 📜jobs.json ┣ 📜es.json ⭐ Usable by Laravel ┗ 📜fr.json ⭐ Usable by Laravel
Testing
./vendor/bin/pest
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Help needed to add a watch command that refreshes the output JSON files with new translations added on the locale folders.
Credits
License
The MIT License (MIT). Please see License File for more information.