Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
AppLocale.inc.php
1 <?php
2 
17 import('lib.pkp.classes.i18n.PKPLocale');
18 
19 class AppLocale extends PKPLocale {
24  static function getSupportedLocales() {
25  static $supportedLocales;
26  if (!isset($supportedLocales)) {
27  if (defined('SESSION_DISABLE_INIT') || !Config::getVar('general', 'installed')) {
28  $supportedLocales = AppLocale::getAllLocales();
29  } elseif (($press = self::$request->getPress())) {
30  $supportedLocales = $press->getSupportedLocaleNames();
31  } else {
32  $site = self::$request->getSite();
33  $supportedLocales = $site->getSupportedLocaleNames();
34  }
35  }
36  return $supportedLocales;
37  }
38 
43  static function getSupportedFormLocales() {
44  static $supportedFormLocales;
45  if (!isset($supportedFormLocales)) {
46  if (defined('SESSION_DISABLE_INIT') || !Config::getVar('general', 'installed')) {
47  $supportedFormLocales = AppLocale::getAllLocales();
48  } elseif (($press = self::$request->getPress())) {
49  $supportedFormLocales = $press->getSupportedFormLocaleNames();
50  } else {
51  $site = self::$request->getSite();
52  $supportedFormLocales = $site->getSupportedLocaleNames();
53  }
54  }
55  return $supportedFormLocales;
56  }
57 
63  static function getLocale() {
64  static $currentLocale;
65  if (!isset($currentLocale)) {
66  if (defined('SESSION_DISABLE_INIT') || !Config::getVar('general', 'installed')) {
67  // If the locale is specified in the URL, allow
68  // it to override. (Necessary when locale is
69  // being set, as cookie will not yet be re-set)
70  $locale = self::$request->getUserVar('setLocale');
71  if (empty($locale) || !in_array($locale, array_keys(AppLocale::getSupportedLocales()))) $locale = self::$request->getCookieVar('currentLocale');
72  } else {
73  $sessionManager = SessionManager::getManager();
74  $session = $sessionManager->getUserSession();
75  $locale = self::$request->getUserVar('uiLocale');
76 
77  $press = self::$request->getPress();
78  $site = self::$request->getSite();
79 
80  if (!isset($locale)) {
81  $locale = $session->getSessionVar('currentLocale');
82  }
83 
84  if (!isset($locale)) {
85  $locale = self::$request->getCookieVar('currentLocale');
86  }
87 
88  if (isset($locale)) {
89  // Check if user-specified locale is supported
90  if ($press != null) {
91  $locales = $press->getSupportedLocaleNames();
92  } else {
93  $locales = $site->getSupportedLocaleNames();
94  }
95 
96  if (!in_array($locale, array_keys($locales))) {
97  unset($locale);
98  }
99  }
100 
101  if (!isset($locale)) {
102  // Use press/site default
103  if ($press != null) {
104  $locale = $press->getPrimaryLocale();
105  }
106 
107  if (!isset($locale)) {
108  $locale = $site->getPrimaryLocale();
109  }
110  }
111  }
112 
113  if (!AppLocale::isLocaleValid($locale)) {
114  $locale = LOCALE_DEFAULT;
115  }
116 
117  $currentLocale = $locale;
118  }
119  return $currentLocale;
120  }
121 
126  static function getLocalePrecedence() {
127  static $localePrecedence;
128  if (!isset($localePrecedence)) {
129  $localePrecedence = array(AppLocale::getLocale());
130 
131  $press = self::$request->getPress();
132  if ($press && !in_array($press->getPrimaryLocale(), $localePrecedence)) $localePrecedence[] = $press->getPrimaryLocale();
133 
134  $site = self::$request->getSite();
135  if ($site && !in_array($site->getPrimaryLocale(), $localePrecedence)) $localePrecedence[] = $site->getPrimaryLocale();
136  }
137  return $localePrecedence;
138  }
139 
144  static function getPrimaryLocale() {
145  static $locale;
146  if ($locale) return $locale;
147 
148  if (defined('SESSION_DISABLE_INIT') || !Config::getVar('general', 'installed')) return $locale = LOCALE_DEFAULT;
149 
150  $press = self::$request->getPress();
151 
152  if (isset($press)) {
153  $locale = $press->getPrimaryLocale();
154  }
155 
156  if (!isset($locale)) {
157  $site = self::$request->getSite();
158  $locale = $site->getPrimaryLocale();
159  }
160 
161  if (!isset($locale) || !AppLocale::isLocaleValid($locale)) {
162  $locale = LOCALE_DEFAULT;
163  }
164 
165  return $locale;
166  }
167 
172  static function installLocale($locale) {
173  parent::installLocale($locale);
174 
175  $press = self::$request->getPress();
176 
177  $genreDao = DAORegistry::getDAO('GenreDAO'); /* @var $genreDao GenreDAO */
178  $genreDao->installDefaults($press->getId(), array($locale));
179 
180  $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
181  $userGroupDao->installLocale($locale);
182  }
183 
188  static function uninstallLocale($locale) {
189  parent::uninstallLocale($locale);
190 
191  $genreDao = DAORegistry::getDAO('GenreDAO');
192  $genreDao->deleteSettingsByLocale($locale);
193 
194  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
195  $userGroupDao->deleteSettingsByLocale($locale);
196  }
197 
203  static function makeComponentMap($locale) {
204  $componentMap = parent::makeComponentMap($locale);
205  $baseDir = "locale/$locale/";
206  $componentMap[LOCALE_COMPONENT_APP_COMMON] = $baseDir . 'locale.xml';
207  $componentMap[LOCALE_COMPONENT_APP_MANAGER] = $baseDir . 'manager.xml';
208  $componentMap[LOCALE_COMPONENT_APP_SUBMISSION] = $baseDir . 'submission.xml';
209  $componentMap[LOCALE_COMPONENT_APP_EDITOR] = $baseDir . 'editor.xml';
210  $componentMap[LOCALE_COMPONENT_APP_ADMIN] = $baseDir . 'admin.xml';
211  $componentMap[LOCALE_COMPONENT_APP_DEFAULT] = $baseDir . 'default.xml';
212  return $componentMap;
213  }
214 }
215 
216 ?>
static & getDAO($name, $dbconn=null)
Mock implementation of the Locale class.
static getLocalePrecedence()
static installLocale($locale)
static getPrimaryLocale()
static getSupportedLocales()
Provides methods for loading locale data and translating strings identified by unique keys...
static & getAllLocales()
static getLocale()
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
static uninstallLocale($locale)
static isLocaleValid($locale)
static getSupportedFormLocales()
static makeComponentMap($locale)