I18NTest.php 2.98 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yiiunit\framework\i18n;

10
use yii\base\Model;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
use yii\i18n\I18N;
use yii\i18n\PhpMessageSource;
use yiiunit\TestCase;

/**
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 * @group i18n
 */
class I18NTest extends TestCase
{
	/**
	 * @var I18N
	 */
	public $i18n;

	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
Alexander Makarov committed
31 32 33
		$this->i18n = new I18N([
			'translations' => [
				'test' => new PhpMessageSource([
34
					'basePath' => '@yiiunit/data/i18n/messages',
Alexander Makarov committed
35 36 37
				])
			]
		]);
38 39 40 41 42
	}

	public function testTranslate()
	{
		$msg = 'The dog runs fast.';
43 44 45 46 47

		// source = target. Should be returned as is.
		$this->assertEquals('The dog runs fast.', $this->i18n->translate('test', $msg, [], 'en'));

		// exact match
48
		$this->assertEquals('Der Hund rennt schnell.', $this->i18n->translate('test', $msg, [], 'de-DE'));
49 50 51 52 53 54

		// fallback to just language code with absent exact match
		$this->assertEquals('Собака бегает быстро.', $this->i18n->translate('test', $msg, [], 'ru-RU'));

		// fallback to just langauge code with present exact match
		$this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
55 56 57 58 59
	}

	public function testTranslateParams()
	{
		$msg = 'His speed is about {n} km/h.';
Alexander Makarov committed
60
		$params = ['n' => 42];
61 62
		$this->assertEquals('His speed is about 42 km/h.', $this->i18n->translate('test', $msg, $params, 'en-US'));
		$this->assertEquals('Seine Geschwindigkeit beträgt 42 km/h.', $this->i18n->translate('test', $msg, $params, 'de-DE'));
63
	}
64

65 66 67 68 69
	public function testTranslateParams2()
	{
		if (!extension_loaded("intl")) {
			$this->markTestSkipped("intl not installed. Skipping.");
		}
70
		$msg = 'His name is {name} and his speed is about {n, number} km/h.';
Alexander Makarov committed
71
		$params = [
72 73
			'n' => 42,
			'name' => 'DA VINCI', // http://petrix.com/dognames/d.html
Alexander Makarov committed
74
		];
75 76
		$this->assertEquals('His name is DA VINCI and his speed is about 42 km/h.', $this->i18n->translate('test', $msg, $params, 'en-US'));
		$this->assertEquals('Er heißt DA VINCI und ist 42 km/h schnell.', $this->i18n->translate('test', $msg, $params, 'de-DE'));
77 78
	}

79 80 81 82
	public function testSpecialParams()
	{
		$msg = 'His speed is about {0} km/h.';

83 84 85 86
		$this->assertEquals('His speed is about 0 km/h.', $this->i18n->translate('test', $msg, 0, 'en-US'));
		$this->assertEquals('His speed is about 42 km/h.', $this->i18n->translate('test', $msg, 42, 'en-US'));
		$this->assertEquals('His speed is about {0} km/h.', $this->i18n->translate('test', $msg, null, 'en-US'));
		$this->assertEquals('His speed is about {0} km/h.', $this->i18n->translate('test', $msg, [], 'en-US'));
87 88 89

		$msg = 'His name is {name} and he is {age} years old.';
		$model = new ParamModel();
90
		$this->assertEquals('His name is peer and he is 5 years old.', $this->i18n->translate('test', $msg, $model, 'en-US'));
91 92 93 94 95 96 97
	}
}

class ParamModel extends Model
{
	public $name = 'peer';
	public $age = 5;
98
}