定制 moss/locale 二次开发

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

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

moss/locale

最新稳定版本:v2.0

Composer 安装命令:

composer require moss/locale

包简介

moss locale

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage

Basic tool for handling translations, formatting and stuff.

Locale

Class that handles locale name, timezone and currency sub unit.

	$locale = new Locale('en_GB', 'UTC', 100);
	
	echo $locale->locale(); // will print "en_GB" 
	$locale->locale('en_US'); // will change locale to en_US
	
	echo $locale->language(); // will print "en"
	echo $locale->territory(); // will print "GB"
	
	echo $locale->currencySubUnit(); // will print 100
	$locale->currencySubUnit(1000); // will change sub unit to 1000
	
	echo $locale->timezone(); // will print "UTC"
	$locale->timezone('Europe/Berlin'); will change default timezone (used by all date functions) to 'Europe/Berlin'

Translator

Translator translates simple texts, singular and plural, with optional placeholders.

	$translator = new Translator('en_GB', []);

Translator uses dictionaries as source of translations Lower priority value is better - 0 means highest priority.

	$dictionary = new ArrayDictionary('en_GB', ['dude' => 'laddy']);
	$translator = new Translator('en_GB', $dictionary);

Dictionaries are list of key-value pairs, where key is a word/sentence/identifier and is translated text. Eg. EN to DE:

	[
		'There be %placeholder%' => 'dort %placeholder%',
		'welcome.string' => 'Hallo %name%!'
	]
	echo $translator->trans('There be %placeholder%', ['placeholder' => 'Drachen'])
	// prints dort Drachen

For plural translation additional syntax is used in dictionaries to describe intervals with proper translations. Plural translations also support placeholders. Intervals follow ISO 31-11 notation:

[, ]	[a, b]	closed interval in ℝ from a (included) to b (included)
], ]	]a, b]	left half-open interval in ℝ from a (excluded) to b (included)
[, [	[a, b[	right half-open interval in ℝ from a (included) to b (excluded)
], [	]a, b[	open interval in ℝ from a (excluded) to b (excluded)
	[
		'apple.count' => '{0} There are no apples|{1} There is one apple|]1,19] There are %count% apples|[20,Inf] There are many apples'
	]
	echo $translator->transChoice('apple.count', $count)
	// prints There are no apples when $count = 0
	// prints There is one apple when $count = 1
	// prints There are %count% apples when $count > 1 && $count >= 19
	// prints There are many apples when $count >= 20

Translator comes with MultiDictionary class that allows for combining multiple dictionaries as one. For example, when default translations come from files, and they can be changed in database. MultiDictionary allows for prioritizing dictionaries. Usually you pass dictionaries trough constructor, and such case first dictionary with requested translation wins. But there are situations where dictionaries are added after instantiation, just when adding new dictionary provide its priority. If not - it will be added as last one. Lower number is better - 0 is highest priority.

	$multi = new MultiDictionary('en_GB');
	$multi->addDictionary($dictionary, 0);

Formatter

Formatter provides set of functions for formatting numbers, currencies and datetime values.

  • ::formatNumber($number)
  • ::formatCurrency($amount)
  • ::formatTime(\DateTime $datetime)
  • ::formatDate(\DateTime $datetime)
  • ::formatDateTime(\DateTime $datetime)

Locale comes with two formatter implementations: Intl that requires extension and plain php formatter.

PlainFormatter can be configured to meet your needs:

   $formatter = new PlainFormatter('en_GB', 100, 'UTC', [
       'number' = '#,##0.###',
       'currency' = '#,##0.##£',
       'date' = 'n/j/y',
       'time' = 'g:i A',
       'datetime' = 'n/j/y, g:i A'
   ]);

moss/locale 适用场景与选型建议

moss/locale 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 742 次下载、GitHub Stars 达 6, 最近一次更新时间为 2014 年 11 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-11-22