1: <?php
2:
3: 4: 5: 6:
7:
8: function getLanguageArray() {
9: return array(
10: 'af' => gettext('Afrikaans'),
11: 'sq_AL' => gettext('Albanian'),
12: 'ar_AE' => gettext('Arabic (United Arab Emirates)'),
13: 'ar_BH' => gettext('Arabic (Bahrain)'),
14: 'ar_DZ' => gettext('Arabic (Algeria)'),
15: 'ar_EG' => gettext('Arabic (Egypt)'),
16: 'ar_IN' => gettext('Arabic (Iran)'),
17: 'ar_IQ' => gettext('Arabic (Iraq)'),
18: 'ar_JO' => gettext('Arabic (Jordan)'),
19: 'ar_KW' => gettext('Arabic (Kuwait)'),
20: 'ar_LB' => gettext('Arabic (Lebanon)'),
21: 'ar_LY' => gettext('Arabic (Libya)'),
22: 'ar_MA' => gettext('Arabic (Morocco)'),
23: 'ar_OM' => gettext('Arabic (Oman)'),
24: 'ar_QA' => gettext('Arabic (Qatar)'),
25: 'ar_SA' => gettext('Arabic (Saudi Arabia)'),
26: 'ar_SD' => gettext('Arabic (Sudan)'),
27: 'ar_SY' => gettext('Arabic (Syria)'),
28: 'ar_TN' => gettext('Arabic (Tunisia)'),
29: 'ar_YE' => gettext('Arabic (Yemen)'),
30: 'eu_ES' => gettext('Basque (Basque)'),
31: 'be_BY' => gettext('Belarusian'),
32: 'bn_BD' => gettext('Bengali'),
33: 'bg_BG' => gettext('Bulgarian'),
34: 'ca_ES' => gettext('Catalan'),
35: 'zh_CN' => gettext('Chinese (People’s Republic of China)'),
36: 'zh_HK' => gettext('Chinese (Hong Kong)'),
37: 'zh_TW' => gettext('Chinese (Taiwan)'),
38: 'hr_HR' => gettext('Croatian'),
39: 'cs_CZ' => gettext('Czech'),
40: 'km_KH' => gettext('Cambodian'),
41: 'da_DK' => gettext('Danish'),
42: 'nl_BE' => gettext('Dutch (Belgium)'),
43: 'nl_NL' => gettext('Dutch (The Netherlands)'),
44: 'en_AU' => gettext('English (Australia)'),
45: 'en_CA' => gettext('English (Canada)'),
46: 'en_GB' => gettext('English (United Kingdom)'),
47: 'en_IN' => gettext('English (India)'),
48: 'en_NZ' => gettext('English (New Zealand)'),
49: 'en_PH' => gettext('English (Philippines)'),
50: 'en_US' => gettext('English (United States)'),
51: 'en_ZA' => gettext('English (South Africa)'),
52: 'en_ZW' => gettext('English (Zimbabwe)'),
53: 'eo' => gettext('Esperanto'),
54: 'et_EE' => gettext('Estonian'),
55: 'fi_FI' => gettext('Finnish'),
56: 'fo_FO' => gettext('Faroese'),
57: 'fr_BE' => gettext('French (Belgium)'),
58: 'fr_CA' => gettext('French (Canada)'),
59: 'fr_CH' => gettext('French (Switzerland)'),
60: 'fr_FR' => gettext('French (France)'),
61: 'fr_LU' => gettext('French (Luxembourg)'),
62: 'gl_ES' => gettext('Galician'),
63: 'gu_IN' => gettext('Gujarati'),
64: 'el' => gettext('Greek'),
65: 'de_AT' => gettext('German (Austria)'),
66: 'de_BE' => gettext('German (Belgium)'),
67: 'de_CH' => gettext('German (Switzerland)'),
68: 'de_DE' => gettext('German (Germany)'),
69: 'de_LU' => gettext('German (Luxembourg)'),
70: 'he_IL' => gettext('Hebrew'),
71: 'hi_IN' => gettext('Hindi'),
72: 'hu_HU' => gettext('Hungarian'),
73: 'id_ID' => gettext('Indonesian'),
74: 'is_IS' => gettext('Icelandic'),
75: 'it_CH' => gettext('Italian (Switzerland)'),
76: 'it_IT' => gettext('Italian (Italy)'),
77: 'ja_JP' => gettext('Japanese'),
78: 'ko_KR' => gettext('Korean'),
79: 'lt_LT' => gettext('Lithuanian'),
80: 'lv_LV' => gettext('Latvian'),
81: 'mk_MK' => gettext('Macedonian'),
82: 'mn_MN' => gettext('Mongolian'),
83: 'ms_MY' => gettext('Malay'),
84: 'mg_MG' => gettext('Malagasy'),
85: 'nb_NO' => gettext('Norwegian (Bokmål)'),
86: 'no_NO' => gettext('Norwegian'),
87: 'ni_ID' => gettext('Nias'),
88: 'fa_IR' => gettext('Persian'),
89: 'pl_PL' => gettext('Polish'),
90: 'pt_BR' => gettext('Portuguese (Brazil)'),
91: 'pt_PT' => gettext('Portuguese (Portugal)'),
92: 'ro_RO' => gettext('Romanian'),
93: 'ru_RU' => gettext('Russian (Russia)'),
94: 'ru_UA' => gettext('Russian (Ukraine)'),
95: 'si_LK' => gettext('Sinhala'),
96: 'sk_SK' => gettext('Slovak'),
97: 'sl_SI' => gettext('Slovenian'),
98: 'es_AR' => gettext('Spanish (Argentina)'),
99: 'es_BO' => gettext('Spanish (Bolivia)'),
100: 'es_CL' => gettext('Spanish (Chile)'),
101: 'es_CO' => gettext('Spanish (Columbia)'),
102: 'es_CR' => gettext('Spanish (Costa Rica)'),
103: 'es_DO' => gettext('Spanish (Dominican Republic)'),
104: 'es_EC' => gettext('Spanish (Ecuador)'),
105: 'es_ES' => gettext('Spanish (Spain)'),
106: 'es_GT' => gettext('Spanish (Guatemala)'),
107: 'es_HN' => gettext('Spanish (Honduras)'),
108: 'es_MX' => gettext('Spanish (Mexico)'),
109: 'es_NI' => gettext('Spanish (Nicaragua)'),
110: 'es_PA' => gettext('Spanish (Panama)'),
111: 'es_PE' => gettext('Spanish (Peru)'),
112: 'es_PR' => gettext('Spanish (Puerto Rico)'),
113: 'es_PY' => gettext('Spanish (Paraguay)'),
114: 'es_SV' => gettext('Spanish (El Salvador)'),
115: 'es_US' => gettext('Spanish (United States)'),
116: 'es_UY' => gettext('Spanish (Uruguay)'),
117: 'es_VE' => gettext('Spanish (Venezuela)'),
118: 'es_LA' => gettext('Spanish (Latin America)'),
119: 'sr_YU' => gettext('Serbian'),
120: 'sr_RS' => gettext('Serbian'),
121: 'sv_FI' => gettext('Swedish (Finland)'),
122: 'sv_SE' => gettext('Swedish (Sweden)'),
123: 'ta_IN' => gettext('Tamil'),
124: 'te_IN' => gettext('Telugu'),
125: 'th_TH' => gettext('Thai'),
126: 'tr_TR' => gettext('Turkish'),
127: 'uk_UA' => gettext('Ukrainian'),
128: 'uz_UZ' => gettext('Uzbek'),
129: 'ur_PK' => gettext('Urdu (Pakistan)'),
130: 'vi_VN' => gettext('Vietnamese'),
131: 'cy' => gettext('Welsh')
132: );
133: }
134:
135: 136: 137: 138: 139: 140:
141: function generateLanguageList($all = false) {
142: global $_zp_active_languages, $_zp_all_languages;
143: if (is_null($_zp_all_languages)) {
144: $zp_languages = getLanguageArray();
145: $dir = @opendir(SERVERPATH . "/" . ZENFOLDER . "/locale/");
146: $_zp_active_languages = $_zp_all_languages = array();
147: if ($dir !== false) {
148: while ($dirname = readdir($dir)) {
149: if (is_dir(SERVERPATH . "/" . ZENFOLDER . "/locale/" . $dirname) && (substr($dirname, 0, 1) != '.')) {
150: if (isset($zp_languages[$dirname])) {
151: $language = $zp_languages[$dirname];
152: if (empty($language)) {
153: $language = $dirname;
154: }
155: } else {
156: $language = $dirname;
157: }
158: $_zp_all_languages[$language] = $dirname;
159: if (!getOption('disallow_' . $dirname)) {
160: $_zp_active_languages[$language] = $dirname;
161: }
162: }
163: }
164: closedir($dir);
165: }
166: ksort($_zp_all_languages);
167: ksort($_zp_active_languages);
168: }
169: if ($all) {
170: return $_zp_all_languages;
171: } else {
172: return $_zp_active_languages;
173: }
174: }
175:
176: 177: 178: 179: 180:
181: function setMainDomain() {
182: $locale = getUserLocale();
183: return setupCurrentLocale($locale);
184: }
185:
186: 187: 188: 189: 190: 191:
192: function gettext_th($string, $theme = Null) {
193: global $_zp_gallery;
194: if (empty($theme)) {
195: $theme = $_zp_gallery->getCurrentTheme();
196: }
197: setupDomain($theme, 'theme');
198: $translation = gettext($string);
199: setupDomain();
200: return $translation;
201: }
202:
203: 204: 205: 206: 207: 208: 209: 210:
211: function ngettext_th($msgid1, $msgid2, $n, $theme = NULL) {
212: global $_zp_gallery;
213: if (empty($theme)) {
214: $theme = $_zp_gallery->getCurrentTheme();
215: }
216: setupDomain($theme, 'theme');
217: $translation = ngettext($msgid1, $msgid2, $n);
218: setupDomain();
219: return $translation;
220: }
221:
222: 223: 224: 225: 226: 227:
228: function gettext_pl($string, $plugin) {
229: setupDomain($plugin, 'plugin');
230: $translation = gettext($string);
231: setupDomain();
232: return $translation;
233: }
234:
235: 236: 237: 238: 239: 240: 241: 242:
243: function ngettext_pl($msgid1, $msgid2, $n, $plugin) {
244: setupDomain($plugin, 'plugin');
245: $translation = ngettext($msgid1, $msgid2, $n);
246: setupDomain();
247: return $translation;
248: }
249:
250: 251: 252: 253: 254: 255:
256: function i18nSetLocale($locale) {
257: global $_zp_RTL_css;
258: $en1 = LOCAL_CHARSET;
259: $en2 = str_replace('ISO-', 'ISO', $en1);
260: $locale_hyphen = str_replace('_', '-', $locale);
261: $simple = explode('-', $locale_hyphen);
262: $try[$locale . '.UTF8'] = $locale . '.UTF8';
263: $try[$locale . '.UTF-8'] = $locale . '.UTF-8';
264: $try[$locale . '.@euro'] = $locale . '.@euro';
265: $try[$locale . '.' . $en2] = $locale . '.' . $en2;
266: $try[$locale . '.' . $en1] = $locale . '.' . $en1;
267: $try[$locale] = $locale;
268: $try[$simple[0]] = $simple[0];
269: $try['NULL'] = NULL;
270: $rslt = setlocale(LC_ALL, $try);
271: $_zp_RTL_css = in_array(substr($rslt, 0, 2), array('fa', 'ar', 'he', 'hi', 'ur'));
272: if (DEBUG_LOCALE) {
273: debugLog("setlocale(" . implode(',', $try) . ") returned: $rslt");
274: }
275: return $rslt;
276: }
277:
278: 279: 280: 281: 282:
283: function setupDomain($domain = NULL, $type = NULL) {
284: global $_zp_active_languages, $_zp_all_languages;
285: switch ($type) {
286: case "plugin":
287: $domainpath = getPlugin($domain . "/locale/");
288: break;
289: case "theme":
290: $domainpath = SERVERPATH . "/" . THEMEFOLDER . "/" . $domain . "/locale/";
291: break;
292: default:
293: $domain = 'zenphoto';
294: $domainpath = SERVERPATH . "/" . ZENFOLDER . "/locale/";
295: break;
296: }
297: bindtextdomain($domain, $domainpath);
298:
299: if (function_exists('bind_textdomain_codeset')) {
300: bind_textdomain_codeset($domain, 'UTF-8');
301: }
302: textdomain($domain);
303:
304: $_zp_active_languages = $_zp_all_languages = NULL;
305: }
306:
307: 308: 309: 310: 311: 312: 313:
314: function setupCurrentLocale($override = NULL) {
315: if (is_null($override)) {
316: $locale = getOption('locale');
317: } else {
318: $locale = $override;
319: }
320: if (getOption('disallow_' . $locale)) {
321: if (DEBUG_LOCALE)
322: debugLogBacktrace("setupCurrentLocale($override): $locale denied by option.");
323: $locale = getOption('locale');
324: if (empty($locale) || getOption('disallow_' . $locale)) {
325: $languages = generateLanguageList();
326: $locale = array_shift($languages);
327: }
328: }
329:
330: @putenv("LANG=$locale");
331: @putenv("LANGUAGE=$locale");
332: $result = i18nSetLocale($locale);
333: if (!$result) {
334: if (isset($_REQUEST['locale']) || is_null($override)) {
335: if (isset($_REQUEST['oldlocale'])) {
336: $locale = sanitize($_REQUEST['oldlocale'], 3);
337: setOption('locale', $locale, false);
338: zp_clearCookie('dynamic_locale');
339: }
340: }
341: }
342: if (DEBUG_LOCALE)
343: debugLogBacktrace("setupCurrentLocale($override): locale=$locale, \$result=$result");
344: setupDomain();
345: return $result;
346: }
347:
348: 349: 350: 351: 352: 353: 354: 355: 356: 357:
358: function parseHttpAcceptLanguage($str = NULL) {
359:
360: if (!$str) {
361: $str = @$_SERVER['HTTP_ACCEPT_LANGUAGE'];
362: }
363: if (!is_string($str)) {
364: return array();
365: }
366: $langs = explode(',', $str);
367:
368: $accepted = array();
369: foreach ($langs as $lang) {
370:
371:
372: if (preg_match('/([A-Za-z]{1,2})(-([A-Za-z0-9]+))?(;q=([0-9\.]+))?/', $lang, $found)) {
373:
374: $code = $found[1];
375:
376: $morecode = array_key_exists(3, $found) ? $found[3] : false;
377:
378: $fullcode = $morecode ? $code . '_' . $morecode : $code;
379:
380: $coef = sprintf('%3.1f', array_key_exists(5, $found) ? $found[5] : '1');
381:
382: if ($coef) {
383:
384: $accepted[$coef . '-' . $code] = array('code' => $code, 'coef' => $coef, 'morecode' => $morecode, 'fullcode' => $fullcode);
385: }
386: }
387: }
388:
389:
390: krsort($accepted);
391: if (DEBUG_LOCALE) {
392: debugLog("parseHttpAcceptLanguage($str)");
393: debugLogVar('parseHttpAcceptLanguage::$accepted', $accepted);
394: }
395: return $accepted;
396: }
397:
398: 399: 400: 401: 402:
403: function validateLocale($userlocale, $source) {
404: if (DEBUG_LOCALE)
405: debugLog("validateLocale($userlocale,$source)");
406: $userlocale = strtoupper(str_replace('-', '_', $userlocale));
407: $languageSupport = generateLanguageList();
408: $locale = NULL;
409: if (!empty($userlocale)) {
410: foreach ($languageSupport as $key => $value) {
411: if (strtoupper($value) == $userlocale) {
412: $locale = $value;
413: if (DEBUG_LOCALE)
414: debugLog("locale set from $source: " . $locale);
415: break;
416: } else if (@preg_match('/^' . $userlocale . '/', strtoupper($value))) {
417: $locale = $value;
418: if (DEBUG_LOCALE)
419: debugLog("locale set from $source (partial match): " . $locale);
420: break;
421: }
422: }
423: }
424: return $locale;
425: }
426:
427: 428: 429: 430: 431:
432: function getUserLocale() {
433: global $_zp_current_admin_obj, $_zp_current_locale;
434: if (!$_zp_current_locale) {
435: if (DEBUG_LOCALE)
436: debugLogBackTrace("getUserLocale()");
437: if (isset($_REQUEST['locale'])) {
438: if (isset($_POST['locale'])) {
439: $_zp_current_locale = validateLocale(sanitize($_POST['locale']), 'POST');
440: } else {
441: $_zp_current_locale = validateLocale(sanitize($_GET['locale']), 'URI string');
442: }
443: if ($_zp_current_locale) {
444: zp_setCookie('dynamic_locale', $_zp_current_locale);
445: }
446: if (DEBUG_LOCALE)
447: debugLog("dynamic_locale from URL: " . sanitize($_REQUEST['locale']) . "=>$_zp_current_locale");
448: } else {
449: $matches = explode('.', @$_SERVER['HTTP_HOST']);
450: if ($_zp_current_locale = validateLocale($matches[0], 'HTTP_HOST')) {
451: zp_clearCookie('dynamic_locale');
452: }
453: }
454:
455: if (!$_zp_current_locale && is_object($_zp_current_admin_obj)) {
456: $_zp_current_locale = $_zp_current_admin_obj->getLanguage();
457: if (DEBUG_LOCALE)
458: debugLog("locale from user: " . $_zp_current_locale);
459: }
460:
461: if (!$_zp_current_locale) {
462: $localeOption = getOption('locale');
463: $_zp_current_locale = zp_getCookie('dynamic_locale');
464:
465: if (DEBUG_LOCALE)
466: debugLog("locale from option: " . $localeOption . '; dynamic locale=' . $_zp_current_locale);
467: if (empty($localeOption) && empty($_zp_current_locale)) {
468: $languageSupport = generateLanguageList();
469: $userLang = parseHttpAcceptLanguage();
470: foreach ($userLang as $lang) {
471: $l = strtoupper($lang['fullcode']);
472: $_zp_current_locale = validateLocale($l, 'HTTP Accept Language');
473: if ($_zp_current_locale)
474: break;
475: }
476: } else {
477: if (empty($_zp_current_locale)) {
478: $_zp_current_locale = $localeOption;
479: }
480: }
481: }
482:
483: if (empty($_zp_current_locale)) {
484:
485: $languageSupport = generateLanguageList();
486: if (in_array('en_US', $languageSupport)) {
487: $_zp_current_locale = 'en_US';
488: } else {
489: $_zp_current_locale = array_shift($languageSupport);
490: }
491: } else {
492: setOption('locale', $_zp_current_locale, false);
493: }
494: if (DEBUG_LOCALE)
495: debugLog("getUserLocale Returning locale: " . $_zp_current_locale);
496: }
497: return $_zp_current_locale;
498: }
499:
500: 501: 502: 503: 504: 505: 506: 507:
508: function get_language_string($dbstring, $locale = NULL) {
509: $strings = getSerializedArray($dbstring);
510: $actual_local = getOption('locale');
511: if (is_null($locale))
512: $locale = $actual_local;
513: if (isset($strings[$locale])) {
514: return $strings[$locale];
515: }
516: if (isset($strings[$actual_local])) {
517: return $strings[$actual_local];
518: }
519: if (isset($strings['en_US'])) {
520: return $strings['en_US'];
521: }
522: return array_shift($strings);
523: }
524:
525: 526: 527: 528: 529:
530: function getTimezones() {
531: $cities = array();
532: if (function_exists('timezone_abbreviations_list')) {
533: $timezones = timezone_abbreviations_list();
534: foreach ($timezones as $key => $zones) {
535: foreach ($zones as $id => $zone) {
536: 537: 538: 539:
540: if (preg_match('/^(Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)\//', $zone['timezone_id'])) {
541: $cities[] = $zone['timezone_id'];
542: }
543: }
544: }
545:
546: $cities = array_unique($cities);
547:
548:
549: ksort($cities, SORT_LOCALE_STRING);
550: }
551: return $cities;
552: }
553:
554: 555: 556: 557: 558: 559: 560:
561: function timezoneDiff($server, $local) {
562: if (function_exists('timezone_abbreviations_list')) {
563: $timezones = timezone_abbreviations_list();
564: foreach ($timezones as $key => $zones) {
565: foreach ($zones as $id => $zone) {
566: if (!isset($offset_server) && $zone['timezone_id'] === $server) {
567: $offset_server = (int) $zone['offset'];
568: }
569: if (!isset($offset_local) && $zone['timezone_id'] === $local) {
570: $offset_local = (int) $zone['offset'];
571: }
572: if (isset($offset_server) && isset($offset_local)) {
573: return ($offset_server - $offset_local) / 3600;
574: }
575: }
576: }
577: }
578: return 0;
579: }
580:
581: 582: 583: 584: 585:
586: function getAllTranslations($text) {
587: $entry_locale = getUserLocale();
588: $result = array('en_US' => $text);
589: $languages = generateLanguageList();
590: foreach ($languages as $language) {
591: setupCurrentLocale($language);
592: $xlated = gettext($text);
593: if ($xlated != $text) {
594: $result[$language] = $xlated;
595: }
596: }
597: setupCurrentLocale($entry_locale);
598: if (count($result) == 1) {
599: return $text;
600: }
601: return serialize($result);
602: }
603:
604: if (function_exists('date_default_timezone_set')) {
605: $tz = getOption('time_zone');
606: if (!empty($tz)) {
607: $err = error_reporting(0);
608: date_default_timezone_set($tz);
609: @ini_set('date.timezone', $tz);
610: error_reporting($err);
611: }
612: unset($tz);
613: }
614: ?>