nia/translating

该组件提供多个接口和类,用于翻译和复数处理。

此包的官方仓库似乎已消失,因此该包已被冻结。

1.2.0 2017-05-08 19:40 UTC

This package is not auto-updated.

Last update: 2022-03-11 06:09:05 UTC


README

该组件提供多个接口和类,用于翻译和复数处理。

安装

使用Composer要求此包。

	composer require nia/translating

测试

要运行单元测试,请使用以下命令

$ cd /path/to/nia/component/
$ phpunit --bootstrap=vendor/autoload.php tests/

格式化器

要使用来自 nia/formatting 组件的格式化器,可以使用 nia/bridge-translating-formatting 组件,或者对于更具体的用例,只需实现 Nia\Translating\Formatter\FormatterInterface 接口。

如何使用

以下示例展示了如何使用 Nia\Translating\Translator\CollectionTranslator 与一个以 de_DE 为最高层,en_US 为最低层的语言层级。

	$germanCollection = new Collection('de_DE', [
	    'car-count' => new Map([
	        '0' => 'Du hast {{ value }} Auto',
	        '1' => 'Du hast {{ value }} Autos'
	    ])
	]);

	$englishCollection = new Collection('en_US', [
	    'car-count' => new Map([
	        '0' => 'You have {{ value }} car',
	        '1' => 'You have {{ value }} cars'
	    ]),
	    'message-count' => new Map([
	        '0' => 'You have {{ value }} new message',
	        '1' => 'You have {{ value }} new messages'
	    ])
	]);

	$translator = new CollectionTranslator([
	    'de_DE',
	    'en_US'
	], new PluralRuleSelector(), new ValueFormatterFilter([]), [
	    $germanCollection,
	    $englishCollection
	]);

	// de_DE: Du hast 1 Auto
	echo $translator->translate('car-count', 1, new Map(['value' => '1']));
	echo "\n";

	// de_DE: Du hast 123 Autos
	echo $translator->translate('car-count', 123, new Map(['value' => '123']));
	echo "\n";


	// de_DE (using en_US as next hierarchy level): You have 1 new message
	echo $translator->translate('message-count', 1, new Map(['value' => '1']));
	echo "\n";

	// de_DE (using en_US as next hierarchy level): You have 123 new messages
	echo $translator->translate('message-count', 123, new Map(['value' => '123']));
	echo "\n";