1 | <?php |
||
2 | /** |
||
3 | * @link https://github.com/motion/yii2-language-provider |
||
4 | * @copyright Copyright (c) 2017-2018 Motion Web Production |
||
5 | * @license BSD 3-Clause License |
||
6 | */ |
||
7 | |||
8 | namespace motion\i18n; |
||
9 | |||
10 | use yii\base\BaseObject; |
||
11 | use yii\db\Connection; |
||
12 | use yii\db\Query; |
||
13 | use yii\di\Instance; |
||
14 | |||
15 | /** |
||
16 | * Database language provider. |
||
17 | * |
||
18 | * @author Vladimir Kuprienko <[email protected]> |
||
19 | * @since 1.0 |
||
20 | */ |
||
21 | class DbLanguageProvider extends BaseObject implements LanguageProviderInterface |
||
22 | { |
||
23 | /** |
||
24 | * Database connection instance. |
||
25 | * |
||
26 | * @var Connection|string|array |
||
27 | */ |
||
28 | public $db = 'db'; |
||
29 | /** |
||
30 | * Name of table with languages in database. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | public $tableName = 'language'; |
||
35 | /** |
||
36 | * Language locale field name in table. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | public $localeField = 'locale'; |
||
41 | /** |
||
42 | * Language name field name in table. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | public $labelField = 'label'; |
||
47 | /** |
||
48 | * Name of field in table with default language flag. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | public $defaultField = 'is_default'; |
||
53 | |||
54 | /** |
||
55 | * @var array Application languages list. |
||
56 | */ |
||
57 | protected $languages = []; |
||
58 | /** |
||
59 | * @var array Default application language. |
||
60 | */ |
||
61 | protected $defaultLanguage = []; |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Get database connection instance. |
||
66 | * |
||
67 | * @throws \yii\base\InvalidConfigException |
||
68 | */ |
||
69 | public function init() |
||
70 | { |
||
71 | $this->db = Instance::ensure($this->db, Connection::className()); |
||
0 ignored issues
–
show
|
|||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | public function getLanguages() |
||
78 | { |
||
79 | if (empty($this->languages)) { |
||
80 | $languages = (new Query()) |
||
81 | ->select([$this->localeField, $this->labelField]) |
||
82 | ->from($this->tableName) |
||
83 | ->all($this->db); |
||
84 | |||
85 | foreach ($languages as $language) { |
||
86 | $this->languages[] = [ |
||
87 | 'locale' => $language[$this->localeField], |
||
88 | 'label' => $language[$this->labelField] |
||
89 | ]; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | return $this->languages; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @inheritdoc |
||
98 | */ |
||
99 | public function getDefaultLanguage() |
||
100 | { |
||
101 | if (empty($this->defaultLanguage)) { |
||
102 | $language = (new Query()) |
||
103 | ->select([$this->localeField, $this->labelField]) |
||
104 | ->from($this->tableName) |
||
105 | ->where([$this->defaultField => true]) |
||
106 | ->one($this->db); |
||
107 | |||
108 | if (false !== $language) { |
||
109 | $this->defaultLanguage = [ |
||
110 | 'locale' => $language[$this->localeField], |
||
111 | 'label' => $language[$this->labelField] |
||
112 | ]; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | return $this->defaultLanguage; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @inheritdoc |
||
121 | */ |
||
122 | public function getLanguageLabel($locale) |
||
123 | { |
||
124 | foreach ($this->getLanguages() as $language) { |
||
125 | if ($language['locale'] == $locale) { |
||
126 | return $language['label']; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | return null; |
||
131 | } |
||
132 | } |
||
133 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.