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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jdz/language 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
inflect - a memoizing inflector for php
Multilingual inflector that transforms words from singular to plural, underscore to camel case, and more.
inflect - a memoizing inflector for php
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Easy to use i18n translation PHP class for multi-language websites
Package for convenient work with Laravel's localization features
统计信息
- 总下载量: 32
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-20