ride/lib-i18n
Composer 安装命令:
composer require ride/lib-i18n
包简介
Internationalization library of the Ride framework
README 文档
README
Internationalization library of the PHP Ride framework.
What's In This Library
Locale
A Locale is the interface for a locale definition. This is a simple representation with a name, code and extra properties.
These locales are contained in a LocaleManager instance. This manager controls the current and default locale.
A LocaleIO feeds the LocaleManager with locales. The locales should be sorted in the order of importance for the system. The first locale is considered the default locale.
Negotiator
A Negotiator is used to detect the current locale. It's invoked when the current locale is not set.
You can use the ChainedNegotiator to implement multiple detection mechanisms in a chain.
Translator
A Translator is the interface to translate keys into a localized string. The interface supports a difference between singular and plural forms of a translation key.
The translators are managed by a TranslatorManager. Translator instances can be requested with the Locale instance.
I18n
The I18n class glues the different parts together into an easy facade.
Code Sample
Check this code sample to see the possibilities of this library:
<?php use ride\library\i18n\locale\io\LocaleIO; use ride\library\i18n\locale\negotiator\DefaultNegotiator; use ride\library\i18n\locale\GenericLocaleManager; use ride\library\i18n\translator\io\AbstractTranslationIO; use ride\library\i18n\translator\GenericTranslatorManager; use ride\library\i18n\I18n; /** * Dummy implementation of a locale data source */ class FooLocaleIO implements LocaleIO { public function getLocales() { return array( new GenericLocale('en', 'English', array( 'full' => 'en_GB.utf8', 'translator.script.plural': '$n != 1', )), new GenericLocale('nl', 'Nederlands', array( 'full' => 'nl_BE.utf8', 'translator.script.plural': '$n != 1', )), ); } } /** * Dummy implementation of a translation data source */ class FooTranslationIO extends AbstractTranslationIO { protected function readTranslations($localeCode) { switch ($localeCode) { case 'en': return array( 'label.name' => 'Name', 'label.email' => 'E-mail address', 'label.hello' => 'Hello %name%!', 'label.item.0' => 'We got 1 item.', 'label.item.1' => 'We got %n% items.', ); default: return array(); } } } // first we need to initialize our I18n instance $localeIO = new FooLocaleIO(); $negotiator = new DefaultNegotiator(); $translationIO = new FooTranslationIO(); $localeManager = new GenericLocaleManager($localeIO, $negotiator); $translatorManager = new GenericTranslatorManager($translationIO); $i18n = new I18n($localeManager, $translatorManager); // play with the locales $en = $i18n->getLocale(); // default language is English since it's provided first by the locale IO $nl = $i18n->getLocale('nl'); $i18n->setCurrentLocale($nl); $i18n->setCurrentLocale('nl'); $nl = $i18n->getLocale(); $i18n->hasLocale('fr'); // false // fetch some lists of the available locales $locales = $i18n->getLocales(); $localeList = $i18n->getLocaleList(); // array('en' => 'English', 'nl' => 'Nederlands') $localeCodeList = $i18n->getLocaleCodeList(); // array('en' => 'en', 'nl' => 'nl') // play with translations $translator = $i18n->getTranslator($en); // translate some keys $value = $translator->translate('label.name'); // Name $value = $translator->translate('label.hello', array('name' => 'world'); // Hello world! $value = $translator->translate('label.unexistant'); // [label.unexistant] $value = $translator->translatePlural(1, 'label.item'); // We got 1 item. $value = $translator->translatePlural(3, 'label.item'); // We got 3 items. // translation management $translations = $translator->getTranslations(); // array('label.name' => 'Name', 'label.email' => 'E-mail address', ...) $translation = $translator->getTranslation('label.hello'); // Hello %name%! $translator->setTranslation('label.foo', 'bar');
Implementations
For more examples, you can check the following implementations of this library:
Installation
You can use Composer to install this library.
composer require ride/lib-i18n
ride/lib-i18n 适用场景与选型建议
ride/lib-i18n 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.31k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 02 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ride/lib-i18n 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ride/lib-i18n 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 28
- 依赖项目数: 27
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-02-21