I18N.php 3.16 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6
<?php

namespace yii\i18n;

use Yii;
use yii\base\Component;
7
use yii\base\InvalidConfigException;
Qiang Xue committed
8 9 10

class I18N extends Component
{
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	/**
	 * @var array list of [[MessageSource]] configurations or objects. The array keys are message
	 * categories, and the array values are the corresponding [[MessageSource]] objects or the configurations
	 * for creating the [[MessageSource]] objects. The message categories can contain the wildcard '*' at the end
	 * to match multiple categories with the same prefix. For example, 'app\*' matches both 'app\cat1' and 'app\cat2'.
	 */
	public $translations;

	public function init()
	{
		if (!isset($this->translations['yii'])) {
			$this->translations['yii'] = array(
				'class' => 'yii\i18n\PhpMessageSource',
				'sourceLanguage' => 'en_US',
				'basePath' => '@yii/messages',
			);
		}
		if (!isset($this->translations['app'])) {
			$this->translations['app'] = array(
				'class' => 'yii\i18n\PhpMessageSource',
				'sourceLanguage' => 'en_US',
				'basePath' => '@app/messages',
			);
		}
	}

Qiang Xue committed
37 38 39
	public function translate($message, $params = array(), $language = null)
	{
		if ($language === null) {
Qiang Xue committed
40
			$language = Yii::$app->language;
Qiang Xue committed
41 42
		}

43 44
		// allow chars for category: word chars, ".", "-", "/","\"
		if (strpos($message, '|') !== false && preg_match('/^([\w\-\\/\.\\\\]+)\|(.*)/', $message, $matches)) {
Qiang Xue committed
45 46 47 48 49 50
			$category = $matches[1];
			$message = $matches[2];
		} else {
			$category = 'app';
		}

51
		$message = $this->getMessageSource($category)->translate($category, $message, $language);
Qiang Xue committed
52

53 54 55
		if (!is_array($params)) {
			$params = array($params);
		}
Qiang Xue committed
56

57 58 59 60 61 62 63
		if (isset($params[0])) {
			$message = $this->getPluralForm($message, $params[0], $language);
			if (!isset($params['{n}'])) {
				$params['{n}'] = $params[0];
			}
			unset($params[0]);
		}
Qiang Xue committed
64

65
		return $params === array() ? $message : strtr($message, $params);
Qiang Xue committed
66 67
	}

Qiang Xue committed
68
	public function getMessageSource($category)
Qiang Xue committed
69
	{
70 71
		if (isset($this->translations[$category])) {
			$source = $this->translations[$category];
Qiang Xue committed
72
		} else {
73 74 75 76 77 78 79
			// try wildcard matching
			foreach ($this->translations as $pattern => $config) {
				if (substr($pattern, -1) === '*' && strpos($category, rtrim($pattern, '*')) === 0) {
					$source = $config;
					break;
				}
			}
Qiang Xue committed
80
		}
81 82
		if (isset($source)) {
			return $source instanceof MessageSource ? $source : Yii::createObject($source);
Qiang Xue committed
83
		} else {
84
			throw new InvalidConfigException("Unable to locate message source for category '$category'.");
Qiang Xue committed
85 86 87
		}
	}

88
	public function getLocale($language)
Qiang Xue committed
89
	{
90

Qiang Xue committed
91 92
	}

93
	protected function getPluralForm($message, $number, $language)
Qiang Xue committed
94 95 96 97 98 99 100
	{
		if (strpos($message, '|') === false) {
			return $message;
		}
		$chunks = explode('|', $message);
		$rules = $this->getLocale($language)->getPluralRules();
		foreach ($rules as $i => $rule) {
101
			if (isset($chunks[$i]) && $this->evaluate($rule, $number)) {
Qiang Xue committed
102 103 104 105 106 107 108 109 110 111 112 113 114
				return $chunks[$i];
			}
		}
		$n = count($rules);
		return isset($chunks[$n]) ? $chunks[$n] : $chunks[0];
	}

	/**
	 * Evaluates a PHP expression with the given number value.
	 * @param string $expression the PHP expression
	 * @param mixed $n the number value
	 * @return boolean the expression result
	 */
115
	protected function evaluate($expression, $n)
Qiang Xue committed
116 117 118 119
	{
		return @eval("return $expression;");
	}
}