forked from kartik-v/yii2-krajee-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranslationTrait.php
60 lines (56 loc) · 1.67 KB
/
TranslationTrait.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
* @package yii2-krajee-base
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2016
* @version 1.8.5
*/
namespace kartik\base;
use Yii;
use yii\helpers\ArrayHelper;
/**
* Trait for all translations used in Krajee extensions
*
* @property array $i18n
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @since 1.8.5
*/
trait TranslationTrait
{
/**
* Yii i18n messages configuration for generating translations
*
* @param string $dir the directory path where translation files will exist
* @param string $cat the message category
*
* @return void
*/
public function initI18N($dir = '', $cat = '')
{
if (empty($cat) && empty($this->_msgCat)) {
return;
}
if (empty($cat)) {
$cat = $this->_msgCat;
}
if (empty($dir)) {
$reflector = new \ReflectionClass(get_class($this));
$dir = dirname($reflector->getFileName());
}
Yii::setAlias("@{$cat}", $dir);
$config = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => "@{$cat}/messages",
'forceTranslation' => true
];
$globalConfig = ArrayHelper::getValue(Yii::$app->i18n->translations, "{$cat}*", []);
if (!empty($globalConfig)) {
$config = array_merge($config, is_array($globalConfig) ? $globalConfig : (array) $globalConfig);
}
if (!empty($this->i18n) && is_array($this->i18n)) {
$config = array_merge($config, $this->i18n);
}
Yii::$app->i18n->translations["{$cat}*"] = $config;
}
}