定制 palgoals/locale-package 二次开发

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

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

palgoals/locale-package

Composer 安装命令:

composer require palgoals/locale-package

包简介

A Laravel package for database-driven multilingual support with language switcher, translation management, and RTL/LTR auto-detection.

README 文档

README

A Laravel package for database-driven multilingual support with language management, translation CRUD, RTL/LTR auto-detection, and ready-to-use Blade components.

Features

  • ✅ Database-driven translations with t('key') helper
  • ✅ Auto-create missing translation keys in the DB
  • ✅ Cache per key/locale pair (configurable TTL)
  • ✅ Fallback locale chain
  • ✅ RTL / LTR auto-detection via current_dir()
  • ✅ Language switcher Blade components (frontend + dashboard)
  • ✅ Full CRUD admin panel for languages and translations
  • ✅ Supports Laravel 10, 11, 12

Requirements

  • PHP 8.1+
  • Laravel 10 / 11 / 12

Installation

Step 1 — Install via Composer

composer require palgoals/locale-package

Step 2 — Run the installer

php artisan locale:install

This single command will:

  • ✅ Publish all files (Models, Controllers, Middleware, Views, Routes, Config)
  • ✅ Run migrations (creates languages and translation_values tables)

Optional: Add --seed to seed default languages automatically:

php artisan locale:install --seed

Step 3 — Register the Helper

In your project's composer.json, add the helper to autoload:

"autoload": {
    "files": [
        "app/helpers_locale.php"
    ]
}

Then run:

composer dump-autoload

Step 4 — Register the Middleware

Laravel 11+ — in bootstrap/app.php:

->withMiddleware(function (Middleware $middleware) {
    $middleware->alias([
        'setLocale' => \App\Http\Middleware\SetLocale::class,
    ]);
})

Laravel 10 and below — in app/Http/Kernel.php:

protected $middlewareAliases = [
    'setLocale' => \App\Http\Middleware\SetLocale::class,
];

Step 5 — Add Routes

In routes/web.php:

Route::middleware(['setLocale'])->group(function () {
    require __DIR__ . '/lang.php';
    // ... your other routes
});

In your dashboard routes file (inside auth/admin group):

require __DIR__ . '/lang_dashboard.php';

Step 6 — Share Variables with All Views

In app/Providers/AppServiceProvider.php inside boot():

view()->composer('*', function ($view) {
    $view->with([
        'currentLocale'   => app()->getLocale(),
        'currentLanguage' => \App\Models\Language::where('code', app()->getLocale())->first(),
        'languages'       => \App\Models\Language::where('is_active', true)->get(),
    ]);
});

Usage

Translate text

{{ t('frontend.nav.home') }}
{{ t('frontend.hero.title', 'Welcome') }}
{!! t_html('frontend.about.description') !!}

Page direction (RTL / LTR)

<html dir="{{ current_dir() }}" lang="{{ app()->getLocale() }}">

Language Switcher (Frontend)

<x-lang.language-switcher />
<x-lang.language-switcher variant="topbar" />

Language Switcher (Dashboard)

<x-lang.language-switcher-dashboard />

CMS Page Slug (optional — requires App\Models\Page)

<a href="/{{ page_slug('about') }}">{{ t('frontend.nav.about') }}</a>

Configuration

After installation, edit config/palgoals-locale.php:

return [
    // Optional: resolve default language from your own settings model
    'default_language_resolver' => function () {
        return \App\Models\GeneralSetting::first()?->default_language_code
            ?? config('app.locale');
    },

    // Auto-create missing translation keys in the DB
    'auto_create' => env('TRANSLATION_AUTO_CREATE', true),

    // Cache duration in seconds
    'cache_ttl' => env('TRANSLATION_CACHE_TTL', 60),
];

.env options

TRANSLATION_AUTO_CREATE=true
TRANSLATION_CACHE_TTL=60
APP_LOCALE=ar
APP_FALLBACK_LOCALE=en

Views Note

The dashboard views (resources/views/dashboard/lang/) depend on your project's layout components:

  • <x-dashboard-layout> — your project's dashboard layout
  • <x-form.input> — your project's input component

Edit these views after installation to match your project's component names.

License

MIT — © PalGoals

palgoals/locale-package 适用场景与选型建议

palgoals/locale-package 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「i18n」 「translation」 「locale」 「laravel」 「multilingual」 「rtl」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 palgoals/locale-package 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 palgoals/locale-package 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-13