定制 laraeast/laravel-locales 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laraeast/laravel-locales

Composer 安装命令:

composer require laraeast/laravel-locales

包简介

The package used to support multi locales in your application.

README 文档

README

Build Status Total Downloads Latest Stable Version License

The package used to support multi locales in your application.

Compatible with Laravel 9, 10, 11, 12 and 13.

Installation

  1. Install package

    composer require laraeast/laravel-locales
  2. 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,
  3. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 laraeast/laravel-locales 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 65.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 28
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-29