-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticDataTranslation.php
More file actions
117 lines (102 loc) · 2.84 KB
/
StaticDataTranslation.php
File metadata and controls
117 lines (102 loc) · 2.84 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace ymaker\data\statics;
use ymaker\configuration\translation\ConfigurationTranslation;
/**
* Class StaticDataTranslation
* @package ymaker\data\statics
* @author Ruslan Saiko <ruslan.saiko.dev@gmail.com>
*/
class StaticDataTranslation extends StaticData
{
private $language;
/**
* Return configuration
* @return array|string|object
*/
public function getConfiguration()
{
return 'translationConfig';
}
/**
* @return string get App language
*/
public static function getAppLanguage()
{
return \Yii::$app->language;
}
/**
* @return string language code
*/
public function getLanguage()
{
return $this->language;
}
/**
* @param string $language language code
*/
public function setLanguage($language)
{
$this->language = $language;
}
/**
* change language for model
* @param $language string language code
* @param bool $reload If true, then all attributes will be overwritten
*/
public function changeLanguage($language, $reload = true)
{
$this->setLanguage($language);
if ($reload) {
$this->reload();
}
}
/**
* load model
* @param bool $skipDefined If true, then the attributes of the set value will not be overwritten
*/
public function loadAttributes($skipDefined = true)
{
$code = $this->language ?: self::getAppLanguage();
/** @var ConfigurationTranslation $config */
$config = $this->configurationInit();
$attributes = $this->getAttributes();
if ($skipDefined) {
$attributes = array_filter($attributes, function ($value) {
return empty($value);
});
}
$keys = array_keys($attributes);
$keyList = [];
foreach ($keys as $key) {
$keyList[$this->getAttributeName($key)] = $key;
}
$values = $config->getMultiplyTranslation(array_keys($keyList), $code);
foreach ($values as $key => $value) {
$this->{$keyList[$key]} = $value;
}
}
/**
* save model
* @return bool
*/
public function save()
{
$code = $this->language;
$this->beforeSave();
if (!$this->validate()) {
return false;
}
/** @var ConfigurationTranslation $config */
$config = $this->configurationInit();
$attributes = $this->getAttributes();
$isSaved = true;
foreach ($attributes as $key => $value) {
if (!$config->setTranslation($this->getAttributeName($key), $value, $code)) {
$this->addError($key, 'Something went wrong. More details in the logs.');
$isSaved = false;
}
}
$this->afterSave();
return $isSaved;
}
}