-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppLocale.inc.php
200 lines (169 loc) · 5.76 KB
/
AppLocale.inc.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* @file classes/i18n/AppLocale.inc.php
*
* Copyright (c) 2014-2017 Simon Fraser University
* Copyright (c) 2003-2017 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class AppLocale
* @ingroup i18n
*
* @brief Provides methods for loading locale data and translating strings identified by unique keys
*
*/
import('lib.pkp.classes.i18n.PKPLocale');
class AppLocale extends PKPLocale {
static $currentLocale;
/**
* Get all supported UI locales for the current context.
* @return array
*/
static function getSupportedLocales() {
static $supportedLocales;
if (!isset($supportedLocales)) {
if (defined('SESSION_DISABLE_INIT') || !Config::getVar('general', 'installed')) {
$supportedLocales = AppLocale::getAllLocales();
} elseif (($journal = self::$request->getJournal())) {
$supportedLocales = $journal->getSupportedLocaleNames();
} else {
$site = self::$request->getSite();
$supportedLocales = $site->getSupportedLocaleNames();
}
}
return $supportedLocales;
}
/**
* Get all supported form locales for the current context.
* @return array
*/
static function getSupportedFormLocales() {
static $supportedFormLocales;
if (!isset($supportedFormLocales)) {
if (defined('SESSION_DISABLE_INIT') || !Config::getVar('general', 'installed')) {
$supportedFormLocales = AppLocale::getAllLocales();
} elseif (($journal = self::$request->getJournal())) {
$supportedFormLocales = $journal->getSupportedFormLocaleNames();
} else {
$site = self::$request->getSite();
$supportedFormLocales = $site->getSupportedLocaleNames();
}
}
return $supportedFormLocales;
}
/**
* Return the key name of the user's currently selected locale (default
* is "en_US" for U.S. English).
* @return string
*/
static function getLocale() {
if (!isset(self::$currentLocale)) {
if (defined('SESSION_DISABLE_INIT') || !Config::getVar('general', 'installed')) {
// If the locale is specified in the URL, allow
// it to override. (Necessary when locale is
// being set, as cookie will not yet be re-set)
$locale = self::$request->getUserVar('setLocale');
if (empty($locale) || !in_array($locale, array_keys(AppLocale::getSupportedLocales()))) $locale = self::$request->getCookieVar('currentLocale');
} else {
$sessionManager = SessionManager::getManager();
$session = $sessionManager->getUserSession();
$locale = self::$request->getUserVar('uiLocale');
$journal = self::$request->getJournal();
$site = self::$request->getSite();
if (!isset($locale)) {
$locale = $session->getSessionVar('currentLocale');
}
if (!isset($locale)) {
$locale = self::$request->getCookieVar('currentLocale');
}
if (isset($locale)) {
// Check if user-specified locale is supported
if ($journal != null) {
$locales = $journal->getSupportedLocaleNames();
} else {
$locales = $site->getSupportedLocaleNames();
}
if (!in_array($locale, array_keys($locales))) {
unset($locale);
}
}
if (!isset($locale)) {
// Use journal/site default
if ($journal != null) {
$locale = $journal->getPrimaryLocale();
}
if (!isset($locale)) {
$locale = $site->getPrimaryLocale();
}
}
}
if (!AppLocale::isLocaleValid($locale)) {
$locale = LOCALE_DEFAULT;
}
self::$currentLocale = $locale;
}
return self::$currentLocale;
}
/* Set locale key */
static function setLocale ($locale) {
$sessionManager = SessionManager::getManager();
$session = $sessionManager->getUserSession();
self::$request->setCookieVar('currentLocale', $locale);
$session->setSessionVar('currentLocale', $locale);
self::$currentLocale = $locale;
}
/**
* Get the stack of "important" locales, most important first.
* @return array
*/
static function getLocalePrecedence() {
static $localePrecedence;
if (!isset($localePrecedence)) {
$localePrecedence = array(AppLocale::getLocale());
$journal = self::$request->getJournal();
if ($journal && !in_array($journal->getPrimaryLocale(), $localePrecedence)) $localePrecedence[] = $journal->getPrimaryLocale();
$site = self::$request->getSite();
if ($site && !in_array($site->getPrimaryLocale(), $localePrecedence)) $localePrecedence[] = $site->getPrimaryLocale();
}
return $localePrecedence;
}
/**
* Retrieve the primary locale of the current context.
* @return string
*/
static function getPrimaryLocale() {
static $locale;
if ($locale) return $locale;
if (defined('SESSION_DISABLE_INIT') || !Config::getVar('general', 'installed')) return $locale = LOCALE_DEFAULT;
$journal = self::$request->getJournal();
if (isset($journal)) {
$locale = $journal->getPrimaryLocale();
}
if (!isset($locale)) {
$site = self::$request->getSite();
$locale = $site->getPrimaryLocale();
}
if (!isset($locale) || !AppLocale::isLocaleValid($locale)) {
$locale = LOCALE_DEFAULT;
}
return $locale;
}
/**
* Make a map of components to their respective files.
* @param $locale string
* @return array
*/
static function makeComponentMap($locale) {
$componentMap = parent::makeComponentMap($locale);
$baseDir = "locale/$locale/";
$componentMap[LOCALE_COMPONENT_APP_COMMON] = $baseDir . 'locale.xml';
$componentMap[LOCALE_COMPONENT_APP_AUTHOR] = $baseDir . 'author.xml';
$componentMap[LOCALE_COMPONENT_APP_SUBMISSION] = $baseDir . 'submission.xml';
$componentMap[LOCALE_COMPONENT_APP_EDITOR] = $baseDir . 'editor.xml';
$componentMap[LOCALE_COMPONENT_APP_MANAGER] = $baseDir . 'manager.xml';
$componentMap[LOCALE_COMPONENT_APP_ADMIN] = $baseDir . 'admin.xml';
$componentMap[LOCALE_COMPONENT_APP_DEFAULT] = $baseDir . 'default.xml';
return $componentMap;
}
}
?>