impulsephp/translation 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

impulsephp/translation

最新稳定版本:v1.0.0-beta

Composer 安装命令:

composer require impulsephp/translation

包简介

Modular translation system for the ImpulsePHP framework with multilingual support, domain based files and automatic fallback.

README 文档

README

A modular translation system for the ImpulsePHP framework with multilingual support, domain based files and automatic fallback to English.

Requirements

  • PHP 8.2 or higher

Installation

Use Composer to install the package:

composer require impulsephp/translation

After installation the service provider Impulse\Translation\TranslatorProvider can be registered within your application container if your framework does not handle it automatically.

Usage

Translations are organized in domain files placed under a translations directory. Each locale has its own subdirectory. For example:

translations/
    en/
        messages.php
    fr/
        messages.php

Each file must return an associative array:

<?php
return [
    'hello' => 'Hello',
    'welcome' => 'Welcome {name}',
];

Create a Translator instance by providing the locale:

use Impulse\Translation\Translator;

$translator = new Translator('fr');

Basic translation

echo $translator->trans('messages.hello'); // Bonjour

Parameter replacement and fallback

echo $translator->trans('messages.welcome', ['name' => 'John']);
// Welcome John (falls back to English because the key is missing in French)

Changing locale

$translator->setLocale('en');

Namespaces

You can add additional translation paths using namespaces:

$translator->addNamespace('package', '/path/to/package/translations');

$translator->trans('package::messages.key');

Running Tests

Install development dependencies and run PHPUnit:

composer install
composer test

License

This project is released under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

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