laraeast/laravel-locales
Composer 安装命令:
composer require laraeast/laravel-locales
包简介
The package used to support multi locales in your application.
README 文档
README
The package used to support multi locales in your application.
Compatible with Laravel 9, 10, 11, 12 and 13.
Installation
-
Install package
composer require laraeast/laravel-locales
-
Edit config/app.php (Skip this step if you are using laravel 5.5+)
service provider:
Laraeast\LaravelLocales\Providers\LocalesServiceProvider::class,
class aliases:
'Locales' => Laraeast\LaravelLocales\Facades\Locales::class,
-
Configure your custom locales:
php artisan vendor:publish --tag="locales:config"
Usage
Locales selector dropdown:
<div class="dropdown"> <a class="btn btn-outline-secondary dropdown-toggle" href="#" role="button" id="languageDropdownMenuLink" data-bs-toggle="dropdown" aria-expanded="false"> {{ Locales::getSvgFlag(width: 25, height: 25) }} <span class="ms-2">{{ Locales::getName() }}</span> </a> <ul class="dropdown-menu" aria-labelledby="languageDropdownMenuLink"> @foreach(Locales::get() as $locale) <li> <a class="dropdown-item" href="{{ url('locale/'. $locale->getCode()) }}"> {{ $locale->getName() }} </a> </li> @endforeach </ul> </div>
API
Locales::get(); // array of supported locales Locales::set('en'); Locales::current(); // the current locale instance Locales::current()->getCode(); // or Locales::getCode(); // return : en Locales::current()->getName(); // or Locales::getName(); // return : English Locales::current()->getName(); // or Locales::getDir(); // return : ltr Locales::current()->getSvgFlag(); // or Locales::getSvgFlag(); // return : svg (html) Locales::from('en'); // return : \Laraeast\LaravelLocales\Enums\Language::EN Locales::codes(); // return array of configured languages codes: ['en', 'ar', 'fr'. '...etc'] Locales::names(); // return array of configured languages names: ['English', 'العربية', 'Français'. '...etc'] Locales::flags(width: 30, height: 30); // return array of configured languages svg flags with width=30 and height=30
Generate locales file in JS:
php artisan locales:generate-js
This command will generate javascript file contains the configured languages, So if you change the languages, You should rul this command to update the javascript file.
Example Generated file resources/js/data/supported-locales.ts:
export default [ { "name": "English", "code": "en", "dir": "ltr", "flag": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"30\" height=\"30\" viewBox=\"0 0 7410 3900\"><path fill=\"#b22234\" d=\"M0 0h7410v3900H0z\"/><path d=\"M0 450h7410m0 600H0m0 600h7410m0 600H0m0 600h7410m0 600H0\" stroke=\"#fff\" stroke-width=\"300\"/><path fill=\"#3c3b6e\" d=\"M0 0h2964v2100H0z\"/><g fill=\"#fff\"><g id=\"d\"><g id=\"c\"><g id=\"e\"><g id=\"b\"><path id=\"a\" d=\"m247 90 70.534 217.082-184.66-134.164h228.253L176.466 307.082z\"/><use xlink:href=\"#a\" y=\"420\"/><use xlink:href=\"#a\" y=\"840\"/><use xlink:href=\"#a\" y=\"1260\"/></g><use xlink:href=\"#a\" y=\"1680\"/></g><use xlink:href=\"#b\" x=\"247\" y=\"210\"/></g><use xlink:href=\"#c\" x=\"494\"/></g><use xlink:href=\"#d\" x=\"988\"/><use xlink:href=\"#c\" x=\"1976\"/><use xlink:href=\"#e\" x=\"2470\"/></g></svg>" }, { "name": "French", "code": "fr", "dir": "ltr", "flag": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"30\" height=\"30\" viewBox=\"0 0 900 600\"><path fill=\"#ED2939\" d=\"M0 0h900v600H0z\"/><path fill=\"#fff\" d=\"M0 0h600v600H0z\"/><path fill=\"#002395\" d=\"M0 0h300v600H0z\"/></svg>" } ];
It recommended to use locales-manager js plugin to works with javascript.
npm i locales-manager --save-dev
import Locales from "locales-manager"; import supportedLocales from "./data/supported-locales"; let locales = new Locales(supportedLocales); locales.setLocale('ar') console.log(locales.current().getName()) // Arabic
laraeast/laravel-locales 适用场景与选型建议
laraeast/laravel-locales 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 65.42k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 08 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 laraeast/laravel-locales 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laraeast/laravel-locales 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 laraeast/laravel-locales 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This module allows drag & drop grouping of items in a GridField
Adds text size controls and text-to-speech controls to Flarum discussion content.
Native Blade date, datetime, and date range pickers.
Symfony and Flysystem integration for the maintained KCFinder continuation.
Laravel integration for the maintained KCFinder continuation.
PHPStan rules shared across KnpLabs organization projects
统计信息
- 总下载量: 65.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-29