empuxa/laravel-locale-via-api
Composer 安装命令:
composer require empuxa/laravel-locale-via-api
包简介
Easily provide your lang files via API.
关键字:
README 文档
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/enorlang/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.
Security Best Practices
-
Protect your routes - Always use authentication middleware on the locale API routes:
Route::middleware('auth:sanctum')->group(function () { Route::get('/locales', ListLocalesController::class); Route::get('/locales/{locale}', GetLocaleController::class); });
-
Restrict locale patterns - Use route constraints to limit valid locale values:
Route::get('/locales/{locale}', GetLocaleController::class) ->where('locale', '[a-z]{2}');
-
Protect your lang directory - This package uses
File::getRequire()which executes PHP code from locale files. Never allow untrusted users to write to yourlang/directory. -
Use vendor safelist - If you enable
load_vendor_files, consider using thevendor_safelistconfig option to restrict which vendor packages are exposed.
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.
empuxa/laravel-locale-via-api 适用场景与选型建议
empuxa/laravel-locale-via-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.36k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「i18n」 「marcoraddatz」 「empuxa」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 empuxa/laravel-locale-via-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 empuxa/laravel-locale-via-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 empuxa/laravel-locale-via-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
A custom URL rule class for Yii 2 which allows to create translated URL rules
Say goodbye to passwords and sign in via TOTP instead.
A smart translation manager for Laravel.
Say goodbye to passwords and sign in via PIN instead.
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
统计信息
- 总下载量: 9.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-11