-
Notifications
You must be signed in to change notification settings - Fork 0
/
GettextMessageSource.php
42 lines (35 loc) · 1023 Bytes
/
GettextMessageSource.php
1
2
3
4
5
6
7
8
9
10
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
37
38
39
40
41
42
<?php
namespace industi\yii2\gettext;
use yii\i18n\GettextMessageSource as GettextMessageSourceBase;
/**
* Extended GettextMessageSource - added scope to message category
*
* @author Krzysztof Kurkowski <krzysztof.kurkowski@industi.com>
*/
class GettextMessageSource extends GettextMessageSourceBase {
/**
* @var string
*/
public $scope = NULL;
/**
* @var array
*/
protected $scopeCache = array();
/**
* @inheritdoc
*/
protected function loadMessages($category, $language)
{
if ( !isset($this->scopeCache[$category]) ) {
if ( !is_null($this->scope) ) {
if ( strripos( $category, $this->scope ) === FALSE ) {
throw new \Exception("Invalid translation category $category, not in scope: " . $this->scope );
}
$this->scopeCache[$category] = substr($category, strlen($this->scope));
} else {
$this->scopeCache[$category] = $category;
}
}
return parent::loadMessages($this->scopeCache[$category], $language);
}
}