-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathTranslationTrait.php
74 lines (68 loc) · 2.09 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* @package yii2-krajee-base
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2022
* @version 3.0.5
*/
namespace kartik\base;
use ReflectionClass;
use ReflectionException;
use Yii;
use yii\helpers\ArrayHelper;
/**
* TranslationTrait manages methods for all translations used in Krajee extensions
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
*/
trait TranslationTrait
{
/**
* @var array the the internalization configuration for this widget.
*
* @see [[\yii\i18n\I18N]] component for understanding the configuration details.
*/
public $i18n = [];
/**
* @var string translation message file category name for i18n.
*
* @see [[\yii\i18n\I18N]]
*/
protected $_msgCat = '';
/**
* Yii i18n messages configuration for generating translations
*
* @param string $dir the directory path where translation files will exist
* @param string $cat the message category
*
* @throws ReflectionException
*/
public function initI18N($dir = '', $cat = '')
{
if (empty($cat) && empty($this->_msgCat)) {
return;
}
if (empty($cat)) {
$cat = $this->_msgCat;
}
if (empty($dir)) {
$class = get_class($this);
$reflector = new ReflectionClass($class);
$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;
}
}