empuxa / laravel-locale-via-api
A smart translation manager for Laravel.
Fund package maintenance!
marcoraddatz
Installs: 6 387
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- spatie/laravel-package-tools: ^1.0
Requires (Dev)
- laravel/pint: ^1.13
- orchestra/testbench: ^7.37|^8.0|^9.0
- pestphp/pest: ^1.23
- roave/security-advisories: dev-latest
- slevomat/coding-standard: ^8.14
This package is auto-updated.
Last update: 2024-12-08 05:19:07 UTC
README
This package lets you share your local translations via API, so that any app can use them. To do so, it converts your PHP lang files to JSON.
Requirements
- You must provide your lang files within folders, such as
lang/en
orlang/de
. - The lang files must be in PHP format, such as `lang/en/validation.php.
- Must secure the API routes and its patterns on your own.
Installation
This package requires Laravel 9.33+. You can install the package via composer:
composer require empuxa/laravel-locale-via-api
Afterward, you might want to copy the vendor files:
php artisan vendor:publish --provider="Empuxa\LocaleViaApi\LocaleViaApiServiceProvider"
Remember to also update the config file config/locale-via-api.php
to your needs!
Usage
This package provides two controllers to access your translations via API.
You must add them to your routes/api.php
file manually.
Empuxa\LocaleViaApi\Http\Controllers\ListLocalesController
With this controller, you can list any locale that is available in your app. For security reasons, it only returns the array from your config and doesn't read the actual files.
Route::get('/locales', ListLocalesController::class);
By adding ?flatten=true
to the URL, you can get a flat array of all available locales.
You can also change the default behavior in the config file.
Non-flattened response
{ "data": { "api": { "error": { "401": "Unauthenticated.", "403": "Forbidden.", "404": "Not Found.", "422": "Unprocessable Entity." } } } }
Flattened response
{ "data": { "api.error.401": "Unauthenticated.", "api.error.403": "Forbidden.", "api.error.404": "Not Found.", "api.error.422": "Unprocessable Entity." } }
Empuxa\LocaleViaApi\Http\Controllers\GetLocaleController
This controller returns the contents of a locale directory as JSON. If the directory does not exist, it will return an error 404.
Route::get('/locales/{locale}', GetLocaleController::class);
Security
Since this package reads your lang files (and could theoretically read any other files as well), it is important to protect your API routes. Make sure that you only share the locales that you want to share. Add route pattern to do so.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
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.