定制 beliven-it/laravel-i18n 二次开发

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

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

beliven-it/laravel-i18n

Composer 安装命令:

composer require beliven-it/laravel-i18n

包简介

A small package for scan / import / export translations

README 文档

README



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

A powerful Laravel package for managing translations with ease. Scan your codebase for translation keys, export them to Excel for translators, and import them back seamlessly. Supports both PHP array files and JSON translations.

Installation

You can install the package via composer:

composer require beliven-it/laravel-i18n

Features

  • 🔍 Scan - Automatically scan your codebase for translation keys (__(), trans(), @lang())
  • 📤 Export - Export all translations to Excel format for easy editing by translators
  • 📥 Import - Import translations back from Excel with merge or overwrite modes
  • 🌐 Dual Format Support - Handles both PHP array files and JSON translations
  • 🎯 Smart Detection - Automatically distinguishes between structured (PHP) and simple (JSON) translations
  • 📊 Excel Integration - Uses PhpSpreadsheet for reliable Excel file handling

How It Works

The package intelligently handles two types of translations:

PHP Array Files (Structured Translations)

Keys with dots are stored in PHP array files with nested structure:

__('messages.welcome')        // → lang/en/messages.php
__('clinic/detail.title')     // → lang/en/clinic/detail.php

JSON Files (Simple String Translations)

Keys without dots are stored in JSON files:

__('Welcome')                 // → lang/en.json
__('My Translation')          // → lang/en.json

Usage

Scanning for Translation Keys

Scan your application code to find all translation keys:

# Scan and add missing keys to all locales
php artisan i18n:manage scan

# Scan for a specific locale only
php artisan i18n:manage scan --locale=en

The scanner will:

  • Find all __(), trans(), and @lang() calls in your code
  • Create appropriate translation files (PHP or JSON)
  • Add missing keys to existing files
  • Show a summary of created files and added keys

Example output:

Scanning for translation keys...
  ✓ Created en/messages.php -> welcome
  + Added en/clinic.php -> detail.title
  
Scan complete!
┌────────────────────────┬───────┐
│ Metric                 │ Count │
├────────────────────────┼───────┤
│ Total keys found       │ 45    │
│ Files created          │ 2     │
│ Keys added             │ 12    │
│ Keys already existing  │ 33    │
└────────────────────────┴───────┘

Exporting Translations

Export all translations to an Excel file for translators:

# Export to default location (storage/app/translations.xlsx)
php artisan i18n:manage export

# Export to custom location
php artisan i18n:manage export --output=/path/to/translations.xlsx

The Excel file will contain:

  • Locale - The language code (en, it, fr, etc.)
  • Path - The file path (lang/en/messages.php or lang/en.json)
  • Key - The translation key
  • Value - The translation value

Importing Translations

Import translations back from Excel:

# Import with merge mode (default - keeps existing translations)
php artisan i18n:manage import --input=/path/to/translations.xlsx

# Import with overwrite mode (replaces entire files)
php artisan i18n:manage import --input=/path/to/translations.xlsx --overwrite

Example output:

Importing translations from: /path/to/translations.xlsx (mode: merge)

Import complete!
┌────────────────┬───────┐
│ Metric         │ Count │
├────────────────┼───────┤
│ Files created  │ 1     │
│ Keys added     │ 15    │
│ Keys updated   │ 8     │
└────────────────┴───────┘

Programmatic Usage

You can also use the services directly in your code:

Scanning

use Beliven\I18n\Services\TranslationScanner;
use Beliven\I18n\Services\TranslationFileManager;

$scanner = app(TranslationScanner::class);
$fileManager = app(TranslationFileManager::class);

// Scan for translation keys
$foundKeys = $scanner->scan(['app', 'resources']);

// Parse a key to determine its type
$parsed = $scanner->parseKey('messages.welcome');
// Returns: ['type' => 'php', 'file' => 'messages', 'key' => 'welcome']

$parsed = $scanner->parseKey('Welcome');
// Returns: ['type' => 'json', 'file' => null, 'key' => 'Welcome']

Managing Translation Files

use Beliven\I18n\Services\TranslationFileManager;

$manager = app(TranslationFileManager::class);

// PHP files
$manager->addKey('en', 'messages', 'welcome', 'Welcome!');
$translations = $manager->loadFile('en', 'messages');

// JSON files
$manager->addJsonKey('en', 'Welcome', 'Welcome!');
$jsonTranslations = $manager->loadJsonFile('en');

Exporting

use Beliven\I18n\Services\TranslationExporter;

$exporter = app(TranslationExporter::class);
$result = $exporter->export('/path/to/output.xlsx');

// Returns:
// [
//     'locales' => 3,
//     'rows' => 150,
//     'file' => '/path/to/output.xlsx'
// ]

Importing

use Beliven\I18n\Services\TranslationImporter;

$importer = app(TranslationImporter::class);

// Merge mode
$stats = $importer->import('/path/to/input.xlsx', false);

// Overwrite mode
$stats = $importer->import('/path/to/input.xlsx', true);

// Returns:
// [
//     'files_created' => 2,
//     'keys_added' => 25,
//     'keys_updated' => 10
// ]

Workflow Example

Here's a typical workflow for managing translations:

  1. Develop - Write your code using translation functions:

    echo __('Welcome');                    // Simple string
    echo __('messages.greeting', ['name' => $user->name]);  // Structured
  2. Scan - Find all translation keys in your codebase:

    php artisan i18n:manage scan
  3. Export - Export to Excel for your translators:

    php artisan i18n:manage export --output=translations.xlsx
  4. Translate - Send the Excel file to translators

  5. Import - Import the translated file back:

    php artisan i18n:manage import --input=translated.xlsx
  6. Done - Your application now has all translations!

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

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.

beliven-it/laravel-i18n 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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