定制 rdcstarr/laravel-translations 二次开发

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

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

rdcstarr/laravel-translations

Composer 安装命令:

composer require rdcstarr/laravel-translations

包简介

This is my package laravel-translations

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A simple and elegant Laravel package for managing translations across multiple languages with database storage and caching.

Requirements

This package requires:

  • Laravel 10+
  • PHP 8.1+
  • rdcstarr/laravel-languages - for language management

Installation

You can install the package via composer:

composer require rdcstarr/laravel-translations

Automatic Installation (Recommended)

Run the install command to publish and run the migrations:

php artisan translations:install

Manual Installation

Alternatively, you can install manually:

  1. Publish the migrations:
php artisan vendor:publish --tag=translations-migrations
  1. Run the migrations:
php artisan migrate

Usage

Using the Helper Function

The package provides a convenient translations() helper function:

// Get a translation for the current language
translations('welcome'); // Returns the translation value

// Get a translation with a default value
translations('welcome', 'Welcome!');

// Get a translation for a specific language
translations('welcome', languageCode: 'ro'); // Returns Romanian translation
translations('greeting', 'Hello', 'de'); // Returns German translation with default

// Access the service directly
translations()->all(); // Get all translations for current language
translations()->has('welcome'); // Check if translation exists
translations()->set('new_key', 'New value'); // Set a translation

Using the Facade

use Rdcstarr\Translations\Facades\Translations;

// Get a translation
Translations::get('welcome');
Translations::get('greeting', 'Hello');

// Set a translation
Translations::set('welcome', 'Welcome!');
Translations::setMany([
    'hello' => 'Hello',
    'goodbye' => 'Goodbye',
]);

// Check if exists
Translations::has('welcome');

// Delete a translation
Translations::delete('old_key');

// Get all translations
Translations::all();

Using Dependency Injection

use Rdcstarr\Translations\TranslationsService;

class MyController extends Controller
{
    public function __construct(
        protected TranslationsService $translations
    ) {}

    public function index()
    {
        $welcome = $this->translations->get('welcome');

        // The service uses the current app locale by default
        // For a different language, create a new instance:
        $service = new TranslationsService('ro');
        $welcome = $service->get('welcome');
    }
}

Artisan Commands

The package includes several artisan commands for managing translations:

List Translations

# List all translations for current language
php artisan translations:list

# List translations for a specific language
php artisan translations:list ro

Get Translation

# Get a translation for current language
php artisan translations:get welcome

# Get a translation for a specific language
php artisan translations:get welcome --language=ro

Set Translation

# Set a translation for current language
php artisan translations:set welcome "Welcome to our app"

# Set a translation for a specific language
php artisan translations:set welcome "Bun venit" --language=ro

Delete Translation

# Delete a translation (with confirmation)
php artisan translations:delete old_key

# Delete for a specific language
php artisan translations:delete old_key --language=ro

# Skip confirmation
php artisan translations:delete old_key --force

Clear Cache

# Clear cache for current language
php artisan translations:clear-cache

# Clear cache for a specific language
php artisan translations:clear-cache --language=ro

# Clear cache for all languages
php artisan translations:clear-cache --all

# Skip confirmation
php artisan translations:clear-cache --all --force

API Reference

TranslationsService

all(): Collection

Get all translations for the current language as a key-value collection.

get(string $key, mixed $default = false): mixed

Get a translation value by key. Returns the default value if the key doesn't exist.

set(string $key, mixed $value = null): bool

Set or update a translation value. Returns true if successful.

setMany(array $translations): bool

Set multiple translations at once. Returns true if successful.

has(string $key): bool

Check if a translation key exists.

delete(string $key): bool

Delete a translation by key. Returns true if successful.

flushCache(): bool

Clear the cache for the current language. Returns true if successful.

flushAllCache(): bool (static)

Clear the cache for all languages. Returns true if successful.

Testing

composer test

📖 Resources

  • Changelog for more information on what has changed recently. ✍️

👥 Credits

📜 License

  • License for more information. ⚖️

rdcstarr/laravel-translations 适用场景与选型建议

rdcstarr/laravel-translations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 rdcstarr/laravel-translations 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-24