-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCurrentLibLanguage.php
More file actions
173 lines (155 loc) · 4.06 KB
/
CurrentLibLanguage.php
File metadata and controls
173 lines (155 loc) · 4.06 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* NetCommonsの機能に必要な情報を取得する内容をまとめたUtility
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('LibAppObject', 'NetCommons.Lib');
/**
* NetCommonsの機能に必要な情報(言語)を取得する内容をまとめたUtility
*
* @property Controller $_controller コントローラ
* @property Language $Language Languageモデル
* @property PluginsRole $PluginsRole PluginsRoleモデル
* @property Plugin $Plugin Pluginモデル
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\NetCommons\Utility
*/
class CurrentLibLanguage extends LibAppObject {
/**
* 使用するモデル
*
* @var array
*/
public $uses = [
'Language' => 'M17n.Language',
];
/**
* 言語データ
*
* これは、UnitTestで使用する。あれば、DBから取得せずにセットされている値を使用する。
*
* @var array|null
*/
private static $__language = null;
/**
* 言語ID
*
* @var string|null 数値型の文字列
*/
private $__langId = null;
/**
* インスタンスの取得
*
* @return CurrentLibLanguage
*/
public static function getInstance() {
return parent::_getInstance(__CLASS__);
}
/**
* インスタンスのクリア
*
* @return void
*/
public static function resetInstance() {
parent::_resetInstance(__CLASS__);
}
/**
* コントローラのセット
*
* @param Controller $controller コントローラ
* @return void
*/
public function initialize($controller = null) {
parent::initialize($controller);
if (empty($this->_controller->request->params['requested'])) {
if (isset($this->_controller->request->query['lang']) &&
! array_key_exists('search', $this->_controller->request->query)) {
$langCode = $this->_controller->request->query['lang'];
if (in_array($langCode, ['english', 'japanese'], true)) {
$langCode = substr($langCode, 0, 2);
}
Configure::write('Config.language', $langCode);
$this->_controller->Session->write('Config.language', $langCode);
} elseif ($this->_controller->Session->check('Config.language')) {
Configure::write('Config.language', $this->_controller->Session->read('Config.language'));
}
}
}
/**
* Configureにlanguageをセットする
*
* @param string $langCode コントローラ
* @return void
*/
public function setConfigure($langCode) {
if ($this->Language->useDbConfig !== 'test' &&
$langCode !== Configure::write('Config.language')) {
Configure::write('Config.language', $langCode);
if (!empty($this->_controller)) {
$this->_controller->Session->write('Config.language', $langCode);
}
}
}
/**
* 言語データを取得
*
* @return array
*/
public function findLanguage() {
//@codeCoverageIgnoreStart
if (isset(self::$__language)) {
return self::$__language;
}
//@codeCoverageIgnoreEnd
$langCode = Configure::read('Config.language');
$result = $this->Language->cacheRead('current', $langCode);
if ($result) {
self::$__language = $result;
return $result;
}
$language = $this->Language->getLanguage('first', array(
'fields' => [
'id', 'code', 'weight', 'is_active'
],
'conditions' => array(
'code' => $langCode,
)
));
if (! isset($language['Language'])) {
$language = $this->Language->getLanguage('first', array(
'fields' => [
'id', 'code', 'weight', 'is_active'
],
'order' => 'weight'
));
}
$this->Language->cacheWrite($language, 'current', $langCode);
return $language;
}
/**
* 言語IDの取得
*
* @return string|null 数値型の文字列
*/
public function getLangId() {
if (! $this->__langId) {
$language = $this->findLanguage();
$this->__langId = $language['Language']['id'];
}
return $this->__langId;
}
/**
* 言語データをクリアする
*
* @return void
*/
public static function clear() {
self::$__language = null;
}
}