moss/locale

该包已被弃用且不再维护。未建议替代包。

moss locale

安装: 730

依赖: 1

建议者: 2

安全: 0

星标: 6

关注者: 2

分叉: 0

公开问题: 0

类型:i18n

v2.0 2015-03-27 10:10 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:41:51 UTC


README

Build Status Scrutinizer Code Quality Code Coverage

处理翻译、格式化等的工具。

地区

处理地区名称、时区和货币子单位的类。

	$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 = new Translator('en_GB', []);

翻译器使用字典作为翻译的来源。优先级较低的价值更好 - 0 表示最高优先级。

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

字典是键值对的列表,其中键是一个单词/句子/标识符,而翻译文本是值。例如,从 EN 到 DE

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

对于复数翻译,字典中使用了额外的语法来描述间隔和适当的翻译。复数翻译也支持占位符。间隔遵循 ISO 31-11 表示法

[, ]	[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

翻译器自带 MultiDictionary 类,允许将多个字典合并为一个。例如,当默认翻译来自文件,且它们可以在数据库中更改时。MultiDictionary 允许优先级排序字典。通常您通过构造函数传递字典,在这种情况下,第一个请求的翻译的字典获胜。但也有一些情况,字典是在实例化之后添加的,只需在添加新字典时提供其优先级。如果没有提供,它将作为最后一个添加。数字越小越好 - 0 是最高优先级。

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

格式化器

格式化器提供了一套格式化数字、货币和日期时间值的函数。

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

地区自带两种格式化器实现:需要扩展的 Intl 和普通的 PHP 格式化器。

PlainFormatter 可以配置以满足您的需求

   $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'
   ]);