ipl/i18n
Composer 安装命令:
composer require ipl/i18n
包简介
Icinga PHP Library - Internationalization
README 文档
README
ipl/i18n provides a translation suite built on PHP's native
gettext extension. It handles locale
negotiation from HTTP Accept-Language headers, wires a static
translator instance for use anywhere in an application, and exposes
convenient global helper functions and a trait for use in classes.
Installation
The recommended way to install this library is via Composer:
composer require ipl/i18n
ipl/i18n requires PHP 7.2 or later with the intl and gettext
extensions.
Translation catalogs are expected in gettext's standard directory layout:
/path/to/locales/<locale>/LC_MESSAGES/<domain>.mo
.mo files are compiled binary catalogs produced from human-editable .po
files via msgfmt. If you do not specify a domain, ipl/i18n uses the
default domain.
Usage
Setting Up the Translator
Bootstrap the translator once at application startup and assign it to
StaticTranslator so that the global helper functions and the Translation
trait resolve it automatically from anywhere in your application.
addTranslationDirectory() accepts an optional second argument to register a
named domain — see Working With Multiple Domains
for details:
use ipl\I18n\GettextTranslator; use ipl\I18n\StaticTranslator; $translator = (new GettextTranslator()) ->addTranslationDirectory('/path/to/locales') ->setLocale('de_DE'); StaticTranslator::$instance = $translator;
Global Helper Functions
Once StaticTranslator::$instance is set, import t() and tp() via
use function and call them anywhere in your application:
use function ipl\I18n\t; use function ipl\I18n\tp; echo t('Save changes'); // $count selects the plural form, printf substitutes %d. printf(tp('%d item', '%d items', $count), $count);
Both functions forward to the static translator instance, so they always reflect the currently active locale.
Translation Trait
Add the Translation trait to any class that needs to translate
messages. Set $translationDomain to scope translations to a specific
gettext domain, or leave it null (the default) to use the default domain:
use ipl\I18n\Translation; class MyModule { use Translation; protected ?string $translationDomain = 'mydomain'; public function getLabel(): string { return $this->translate('Dashboard'); } public function getItemLabel(int $count): string { // $count selects the plural form, sprintf substitutes %d. return sprintf( $this->translatePlural('%d item', '%d items', $count), $count ); } }
Working With Multiple Domains and Message Context
Register additional translation directories under named domains and use
the domain- and context-aware methods to resolve messages precisely.
Context disambiguates source strings that share the same English word
but require different translations — for example, "request" as an
HTTP request versus a change request:
use ipl\I18n\GettextTranslator; $translator = (new GettextTranslator()) ->addTranslationDirectory('/path/to/default/locales') ->addTranslationDirectory('/path/to/configuration/locales', 'configuration') ->setLocale('de_DE'); // Same source string, different translations depending on context: // 'http' → an HTTP request // 'change' → a change request echo $translator->translate('request', 'http'); echo $translator->translate('request', 'change'); // Resolve messages from the `configuration` domain. echo $translator->translateInDomain('configuration', 'configuration item'); printf( $translator->translatePluralInDomain( 'configuration', '%d job', '%d jobs', 2, 'deployment queue' ), 2 );
Detecting the User's Language
Browsers send an Accept-Language header with each request listing the
user's preferred languages in priority order (e.g.,
de-DE,de;q=0.9,en-US;q=0.8). Use Locale::getPreferred() to match
that list against the locales your application actually has available
and activate the best fit:
use ipl\I18n\Locale; $availableLocales = $translator->listLocales(); $preferredLocale = (new Locale())->getPreferred( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '', $availableLocales ); $translator->setLocale($preferredLocale);
If no exact or language-level match is available, locale selection falls
back to en_US by default. Use setDefaultLocale() on the Locale
instance to change the fallback.
Testing With NoopTranslator
In tests you typically do not want to depend on compiled .mo catalog
files being present on disk. NoopTranslator satisfies the translator
interface by returning every message unchanged, so your test suite runs
without any gettext setup while still exercising all code paths that
call t(), tp(), or the Translation trait:
use ipl\I18n\NoopTranslator; use ipl\I18n\StaticTranslator; StaticTranslator::$instance = new NoopTranslator();
Changelog
See CHANGELOG.md for a list of notable changes.
License
ipl/i18n is licensed under the terms of the MIT License.
ipl/i18n 适用场景与选型建议
ipl/i18n 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 97.73k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 05 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「i18n」 「translation」 「gettext」 「localization」 「internationalization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ipl/i18n 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ipl/i18n 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ipl/i18n 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
Gettext library to translate with i18n
Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.
A custom URL rule class for Yii 2 which allows to create translated URL rules
Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.
A Laravel Eloquent model trait for translatable resource
统计信息
- 总下载量: 97.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-06