定制 jdz/language 二次开发

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

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

jdz/language

Composer 安装命令:

composer require jdz/language

包简介

JDZ Translator (i18n) and Inflector library based on Symfony components

README 文档

README

Language is a proxy to the symfony/translation component - a modular PHP package designed for managing and translating multilingual content in your web projects. This project simplifies the use of language files and provides a clear API for easily handling translations.

Features

  • Built on the symfony/translation component for robust translation capabilities.
  • Uses the symfony/string inflector for pluralization and singularization.
  • LanguageCode enum for type-safe language code handling.
  • Custom LanguageException for language-specific error handling.
  • Load default or application-specific language files.
  • Flexible management of translation keys.
  • Support for YAML format language files.
  • Easy integration with other frameworks or custom solutions.
  • Optimized for fast performance and maximum extensibility.
  • Inflector support for French, English & Spanish (introduced in symfony/string 7.2, requires PHP >= 8.2).

Installation

Add the package to your project using Composer:

composer require jdz/language

Requirements

  • PHP 8.2 or higher
  • symfony/translation
  • symfony/string
  • symfony/yaml

Usage

For a complete example, check the example folder in the repository.

Initialization

use JDZ\Language\Language;

$language = new Language(
    // app languages
    ['fr', 'en'],
    // default language
    'fr'
);

Load User Language

If not an available language, it falls back to the default language.

$language->load('fr');

Load Translations from Array

$language->loadArray([
    'welcome_message' => 'Hi !',
    'test' => [
        'key1' => 'Test 1 nested',
        'key2' => 'Test 1 nested 2',
    ],
    'test2' => 'Test 2',
]);

Load Translations from YAML File

$language->loadYamlFiles([
    __DIR__ . '/file1.yml',
    __DIR__ . '/file2.yml',
]);
$language->loadYamlFile(__DIR__ . '/file3.yml');

Example YAML file:

welcome_message: "Welcome"
goodbye_message: "Goodbye"

Setting Values

$language->set('custom.key', 'My custom value');

Getting Values

$welcomeMessage = $language->get('welcome_message');
// Hi !

$customMessage = $language->get('custom.key');
// My custom value

$notDefined = $language->get('test.me');
// test.me (returns key if not found)

$notDefinedButDefault = $language->get('test.me', [], 'Default value');
// Default value

Using LanguageCode Enum

use JDZ\Language\LanguageCode;

// Check if a language code is valid
if (LanguageCode::isValid('fr')) {
    // Valid language
}

// Get language from enum
$french = LanguageCode::FRENCH;
echo $french->value; // 'fr'

// Try to get enum from string
$lang = LanguageCode::tryFrom('en'); // Returns LanguageCode::ENGLISH or null

API Reference

Language Class Methods

Method Description
load(string $lang) Load a user language. Throws LanguageException if invalid.
loadYamlFiles(array $resources) Load translations from an array of YAML files.
loadYamlFile(string $resource) Load translations from a single YAML file.
loadArray(array $strings) Load translations from an array of key => value pairs.
set(string $key, mixed $value) Adds a translation.
get(string $key, array $parameters = [], ?string $default = null) Retrieves a translation at the specified path.
has(string $key) Checks if a translation exists at the specified path.
plural(string $key, int $count) Load a plural translation with count parameter.
pluralize(string $string) Uses the symfony/string inflector to pluralize a word.
singularize(string $string) Uses the symfony/string inflector to singularize a word.

LanguageCode Enum

Available language codes:

  • LanguageCode::FRENCH (value: 'fr')
  • LanguageCode::ENGLISH (value: 'en')
  • LanguageCode::SPANISH (value: 'es')

Methods:

  • LanguageCode::isValid(string $value): bool - Check if a string is a valid language code

LanguageException

Custom exception class for language-specific errors. Extends \Exception.

Testing

The package includes a comprehensive test suite with 38 tests covering all functionality.

To run the tests:

composer test

# For detailed test output:
composer test -- --testdox

Or directly with PHPUnit:

vendor/bin/phpunit --colors=always --testdox

License

This project is licensed under the MIT License - see the LICENSE file for details.

jdz/language 适用场景与选型建议

jdz/language 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 jdz/language 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-20