XOOPS  2.6.0
Locale.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  */
11 
20 {
21  static $_defaultLocale = 'en_US';
22  static $_userLocales = array();
23 
32  public static function loadLanguage($name, $domain = '', $language = null)
33  {
34  if (empty($name)) {
35  return false;
36  }
38  // expanded domain to multiple categories, e.g. module:system, framework:filter, etc.
39  if ((empty($domain) || 'global' == $domain)) {
40  $path = '';
41  } else {
42  $path = (is_array($domain)) ? array_shift($domain) : "modules/{$domain}";
43  }
45  $fullPath = $xoops->path("{$path}/language/{$language}/{$name}.php");
46  if (!$ret = XoopsLoad::loadFile($fullPath)) {
47  $fullPath2 = $xoops->path("{$path}/language/english/{$name}.php");
48  $ret = XoopsLoad::loadFile($fullPath2);
49  }
50  return $ret;
51  }
52 
58  public static function loadLocale($domain = 'xoops')
59  {
61  // expanded domain to multiple categories, e.g. module:system, framework:filter, etc.
62  if ('xoops' == $domain) {
63  $path = '';
64  } else {
65  $path = (is_array($domain)) ? array_shift($domain) : "modules/{$domain}";
66  }
67  $locales = self::getUserLocales();
68  foreach ($locales as $locale) {
69  $fullPath = $xoops->path("{$path}/locale/{$locale}/locale.php");
70  $fullPath2 = $xoops->path("{$path}/locale/{$locale}/{$locale}.php");
71  if (XoopsLoad::fileExists($fullPath)) {
72  XoopsLoad::addMap(array($domain . 'locale' => $fullPath));
73  if (XoopsLoad::fileExists($fullPath2)) {
74  XoopsLoad::addMap(array(strtolower($domain . "locale{$locale}") => $fullPath2));
75  }
76  return true;
77  }
78  }
79  return false;
80  }
81 
87  public static function loadThemeLocale(XoopsTheme $theme)
88  {
90  $locales = self::getUserLocales();
91  foreach ($locales as $locale) {
92  $fullPath = $xoops->path($theme->resourcePath("locale/{$locale}/locale.php"));
93  $fullPath2 = $xoops->path($theme->resourcePath("locale/{$locale}/{$locale}.php"));
94  if (XoopsLoad::fileExists($fullPath)) {
95  XoopsLoad::addMap(array(strtolower($theme->folderName . 'ThemeLocale') => $fullPath));
96  if (XoopsLoad::fileExists($fullPath2)) {
97  XoopsLoad::addMap(array(strtolower($theme->folderName . "ThemeLocale{$locale}") => $fullPath2));
98  }
99  return true;
100  }
101  }
102  return false;
103  }
104 
108  public static function loadMailerLocale()
109  {
111  $locales = self::getUserLocales();
112  foreach ($locales as $locale) {
113  $fullPath = $xoops->path("locale/{$locale}/mailer.php");
114  if (XoopsLoad::fileExists($fullPath)) {
115  XoopsLoad::addMap(array(strtolower('XoopsMailerLocale') => $fullPath));
116  return true;
117  }
118  }
119  return false;
120  }
121 
128  public static function translate($key, $dirname = 'xoops')
129  {
130  $class = self::getClassFromDirname($dirname);
131  if (defined("$class::$key")) {
132  return constant("$class::$key");
133  } elseif (defined($key)) {
134  return constant($key);
135  }
136  return $key;
137  }
138 
145  public static function translateTheme($key, $dirname = '')
146  {
147  $class = self::getThemeClassFromDirname($dirname);
148 
149  if (defined("$class::$key")) {
150  return constant("$class::$key");
151  } elseif (defined($key)) {
152  return constant($key);
153  }
154  return $key;
155  }
156 
162  public static function getClassFromDirname($dirname)
163  {
164  return ucfirst($dirname) . 'Locale';
165  }
166 
172  public static function getThemeClassFromDirname($dirname = '')
173  {
174  if (!$dirname) {
175  $dirname = Xoops::getInstance()->theme()->folderName;
176  }
177  return ucfirst($dirname) . 'ThemeLocale';
178  }
179 
193  public static function getUserLocales()
194  {
195  if (empty(self::$_userLocales)) {
196  // reset user_lang array
197  $userLocales = array();
198 
199  // Highest priority: forced language
200  //if ($this->forcedLang != NULL) {
201  // $userLocales[] = $this->forcedLang;
202  //}
203 
204  // 2nd highest priority: GET parameter 'lang'
205  if (isset($_GET['lang']) && is_string($_GET['lang'])) {
206  $userLocales[] = $_GET['lang'];
207  }
208 
209  // 3rd highest priority: SESSION parameter 'lang'
210  if (isset($_SESSION['lang']) && is_string($_SESSION['lang'])) {
211  $userLocales[] = $_SESSION['lang'];
212  }
213 
214  // 4th highest priority: HTTP_ACCEPT_LANGUAGE
215  if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
216  foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $part) {
217  if (preg_match("/(.*);q=([0-1]{0,1}\.\d{0,4})/i", $part, $matches)) {
218  $userLocales[] = $matches[1];
219  } else {
220  $userLocales[] = $part;
221  }
222  }
223  }
224 
225  $userLocales[] = Xoops::getInstance()->getConfig('locale');
226 
227  // Lowest priority: fallback
228  $userLocales[] = static::$_defaultLocale;
229 
230  $availableLocales = XoopsLists::getLocaleList();
231  // remove not allowed characters
232  foreach ($userLocales as $key => $value) {
233  $value = preg_replace('/[^a-zA-Z0-9_\-]/', '', $value); // only allow a-z, A-Z and 0-9
234  if ($value && in_array($value, $availableLocales)) {
235  self::$_userLocales[$key] = str_replace('-', '_', $value);
236  }
237  }
238 
239 
240  // remove duplicate elements
241  self::$_userLocales = array_unique(self::$_userLocales);
242  }
243  return self::$_userLocales;
244  }
245 }
static getLegacyLanguage()
Definition: Abstract.php:76
$path
Definition: execute.php:31
$_SESSION['RF']["verify"]
Definition: dialog.php:4
static translateTheme($key, $dirname= '')
Definition: Locale.php:145
static loadLanguage($name, $domain= '', $language=null)
Definition: Locale.php:32
static getInstance()
Definition: Xoops.php:160
$_SERVER['REQUEST_URI']
static getClassFromDirname($dirname)
Definition: Locale.php:162
$xoops
Definition: admin.php:25
static loadThemeLocale(XoopsTheme $theme)
Definition: Locale.php:87
static fileExists($file)
Definition: xoopsload.php:506
static loadMailerLocale()
Definition: Locale.php:108
static translate($key, $dirname= 'xoops')
Definition: Locale.php:128
static getThemeClassFromDirname($dirname= '')
Definition: Locale.php:172
$dirname
Definition: backend.php:38
resourcePath($path)
Definition: theme.php:1009
static loadFile($file, $once=true)
Definition: xoopsload.php:454
$language
static loadLocale($domain= 'xoops')
Definition: Locale.php:58
static $_defaultLocale
Definition: Locale.php:21
static getUserLocales()
Definition: Locale.php:193
static $_userLocales
Definition: Locale.php:22
static addMap(array $map)
Definition: xoopsload.php:40