elaborate-code/laravel-json-tongue

Use multiple JSON files per locale on Laravel

v0.3.0 2022-10-08 21:09 UTC

This package is auto-updated.

Last update: 2024-10-27 01:52:48 UTC


README

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

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.

Read more...

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:

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.